我自定义了一个ListView布局,想在里面添加一个CheckBox按钮,我遇到了下面的第二个问题,虽然有解决方法但不知道具体怎么做,最好能给个完整的例子,谢谢大家了!
1、ListView item中加入checkbox后onListItemClick 事件无法触发。
原因:checkbox的优先级高于ListItem于是屏蔽了ListItem的单击事件。
解决方案:设置checkbox的android:focusable="false"
2、选择其中的checkbox,当滚动ListView的时候,会出现一些Checkbox选择错位的现象,
原因:为记住Checkbox的选择状态
解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置
1、ListView item中加入checkbox后onListItemClick 事件无法触发。
原因:checkbox的优先级高于ListItem于是屏蔽了ListItem的单击事件。
解决方案:设置checkbox的android:focusable="false"
2、选择其中的checkbox,当滚动ListView的时候,会出现一些Checkbox选择错位的现象,
原因:为记住Checkbox的选择状态
解决方案:当选择Checkbox的时候,记下其状态,然后在getView方法中进行设置
解决方案 »
- 如何获取ListView里Item中的控件呢?
- 联通3g gsm基站定位 总jason parsing error
- layout文件夹里面的xml布局文件能分以下类吗?
- 短信接收的程序,老是出错。。。
- 关于launcher上appwidget在手机重启后加载失败的问题
- 安卓平台对讲技术相关
- Genymotion安卓模拟器如何访问data文件和sdcard文件?
- 如何让人物跑起来。改了两天还是有错误
- android中webview加载网页或网页切换时出现白屏现象
- idea新建android项目出现的问题
- 新手 关于listview 获取控件的问题
- 求解决方法,软件登录后切换到后台,再次点击软件图标,软件偶尔会再次进入登录界面
public class checkBtnView extends ListActivity {
private List<Map<String,Object>> hashMap;
//CheckBox box1 = (CheckBox)findViewById(R.id.checkBox1);
//CheckBox box2 = (CheckBox)findViewById(R.id.checkBox2);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hashMap = getData();
Log.i("debug", "start debuging");
MyAdapter adapter = new MyAdapter(this);
Log.i("debug", "MyAdapter successed");
setListAdapter(adapter);
Log.i("debug", "setListAdapter successed");
}
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "NO.1");
//map.put("text", "No.2");
list.add(map);
// TODO Auto-generated method stub
return list;
} public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("nice")
.setMessage("good day")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
}
}).show();
}
public final class ViewHolder{
public TextView title;
public CheckBox box1;
public CheckBox box2;
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.mInflater = LayoutInflater.from(context);
} @Override
public int getCount()
{
// TODO Auto-generated method stub
// this is some debug;
return hashMap.size();
} @Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder = null;
if (holder == null)
{
Log.i("MyAdapter debug", "start");
holder = new ViewHolder();
Log.i("MyAdapter", "set convertView");
convertView = mInflater.inflate(R.layout.main, null);
holder.title = (TextView)convertView.findViewById(R.id.textView1);
holder.box1 = (CheckBox)convertView.findViewById(R.id.checkBox1);
holder.box2 = (CheckBox)convertView.findViewById(R.id.checkBox2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
Log.i("MyAdapter", "run wrong");
}
Log.i("MyAdapter", "setClick");
holder.title.setText((String)hashMap.get(position).get("title"));
Log.i("MyAdapter", "TX normal");
holder.box1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
showInfo();
}
});
return convertView;
}
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<CheckBox
android:text="选择一"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>
<CheckBox
android:text="选择二"
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox></LinearLayout>
这是xml文件 希望对楼主有用
问题1很好解决:设置 checkbox 为:android:focusable="false 就行
<CheckBox android:id="@+id/list_size"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false" android:clickable="false" />问题2 就不太好说,你可以用一个对象保存每一个listitem的信息,其中就可以保存一项 ischecked
在你的adapter的getview中每次读取的时候设置保存的ischecked 。在listitem的单击事件中也要保存ischecked(如果是通过单击改变checkbox的状态的话) 不知道说明白了没有。
2.在getview中加入checkbox 的事件,在把checkbox的状态存到数据库中最好的方法以下代码仅供参考
package com.jftt.dice.activity;import com.jftt.dice.date.DataBase;import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;public class ModelActivity extends Activity {
private ListView listview;
private DataBase mDateBase;
private Cursor mCursor;
private String modelname;
private String strOpt;
private ImageButton ImageButton01;
private String one;
private String two;
private String three;
private String four;
private String five;
private String six;
private String time; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
strOpt = dm.widthPixels + "*" + dm.heightPixels;
if (strOpt.equals("480*800") || strOpt.equals("600*1024")) {
setContentView(R.layout.main);
}
if (strOpt.equals("320*480")) {
setContentView(R.layout.mainone);
}
ImageButton01 = (ImageButton) findViewById(R.id.ImageButton01);
ImageButton01.setOnClickListener(new Listener());
mDateBase = new DataBase(this);
mCursor = mDateBase.select();// 查询数据库
startManagingCursor(mCursor);
if (strOpt.equals("480*800") || strOpt.equals("600*1024")) {
listview = (ListView) findViewById(R.layout.user);
}
if (strOpt.equals("320*480")) {
listview = (ListView) findViewById(R.layout.userone);
}
listview.setAdapter(new MyAdapter(this));// 加入适配器
listview.setOnItemClickListener(new ListListrner());
} class ListListrner implements OnItemClickListener {
private boolean c = true; public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 <= 7) {
c = false;
}
mCursor.moveToPosition(arg2);
modelname = mCursor.getString(1);
AlertDialog.Builder dialogBuilder = new Builder(ModelActivity.this);
dialogBuilder.setTitle(modelname);
String[] strarrStrings = new String[] {
getString(R.string.add_title),
getString(R.string.model_change),
getString(R.string.model_delete), getString(R.string.no) };
dialogBuilder.setItems(strarrStrings,
new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intent = new Intent();
intent.setClass(ModelActivity.this,
AddActivity.class);
startActivity(intent);
break;
case 1:
if (c == false) {
Toast.makeText(ModelActivity.this,
getString(R.string.toast_nochange),
Toast.LENGTH_SHORT).show();
break;
}
modelname = mCursor.getString(1);
one = mCursor.getString(2);
two = mCursor.getString(3);
three = mCursor.getString(4);
four = mCursor.getString(5);
five = mCursor.getString(6);
six = mCursor.getString(7);
time = mCursor.getString(9);
Bundle bundle = new Bundle();
bundle.putString("mianone", one);
bundle.putString("miantwo", two);
bundle.putString("mianthree", three);
bundle.putString("mianfour", four);
bundle.putString("mianfive", five);
bundle.putString("miansix", six);
bundle.putString("miantime", time);
bundle.putString("modelname", modelname);
Intent intent2 = new Intent();
intent2.putExtras(bundle);
intent2.setClass(ModelActivity.this,
Change.class);
startActivity(intent2);
break;
case 2:
if (c == false) {
Toast.makeText(ModelActivity.this,
getString(R.string.toast_nodelete),
Toast.LENGTH_SHORT).show();
break;
}
if (mCursor.getInt(8) == 2) {
mDateBase.update(getString(R.string.dice_jingdian), 2);
}
Message message = new Message();
message.what = 2;
handler.sendMessage(message);
Toast.makeText(ModelActivity.this,
getString(R.string.toast_delete),
Toast.LENGTH_SHORT).show();
break;
case 3:
break;
default:
break;
}
}
});
dialogBuilder.show();
}
} class Listener implements OnClickListener { public void onClick(View v) {
if (v == ImageButton01) {
Intent intent = new Intent();
intent.setClass(ModelActivity.this, AddActivity.class);
startActivity(intent);
}
} } public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private View myView; public MyAdapter(Context c) {
this.inflater = LayoutInflater.from(c);
} public int getCount() {
return mCursor.getCount();
} public Object getItem(int position) {
return null;
} public long getItemId(int position) {
return 0;
} public View getView(int position, View convertView, ViewGroup parent) {
if (strOpt.equals("480*800") || strOpt.equals("600*1024")) {
myView = inflater.inflate(R.layout.user, null);
}
if (strOpt.equals("320*480")) {
myView = inflater.inflate(R.layout.userone, null);
}
mCursor.moveToPosition(position);
final TextView textView = (TextView) myView
.findViewById(R.id.TextView_model); final TextView textView01 = (TextView) myView
.findViewById(R.id.TextView01);
final CheckBox CheckBox01 = (CheckBox) myView
.findViewById(R.id.CheckBox01);
if (position == 0) {
textView01.setText(R.string.ps_jiawu);
}
if (position == 1) {
textView01.setText(R.string.ps_hejiu);
}
if (position == 2) {
textView01.setText(R.string.ps_zhoumo);
}
if (position == 3) {
textView01.setText(R.string.ps_youxi);
}
if (position == 4) {
textView01.setText(R.string.ps_yundong);
}
if (position == 5) {
textView01.setText(R.string.ps_qinglv);
}
if (position == 6) {
textView01.setText(R.string.ps_duicuo);
}
if (position == 7) {
textView01.setText(R.string.ps_jingdian);
}
if (position > 7) {
textView01.setText(R.string.ps_ziji);
}
int a = mCursor.getInt(8);
textView.setText(mCursor.getString(1));
if (a == 2) {
CheckBox01.setChecked(true);
}
if (a == 1) {
CheckBox01.setChecked(false);
}
CheckBox01.setFocusable(false);
CheckBox01.setOnClickListener(new OnClickListener() { public void onClick(View v) {
for (int num = 0; num < mCursor.getCount(); num++) {
if (mCursor.moveToPosition(num)
&& mCursor.getInt(8) == 2) {
mDateBase.update(mCursor.getString(1), 1);
}
}
modelname = textView.getText().toString();
mDateBase.update(modelname, 2);
Toast.makeText(ModelActivity.this,
modelname + getString(R.string.toast_defult),
Toast.LENGTH_SHORT).show();
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
});
return myView;
}
} public Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
listview.invalidateViews();
mCursor.deactivate();
break;
case 2:
mDateBase.delete(modelname);
mCursor = mDateBase.select();
listview.invalidateViews();
break;
}
super.handleMessage(msg);
}
}; public void onDestroy() {
super.onDestroy();
if (mDateBase != null) {
mDateBase.close();
mDateBase = null;
}
}
}