我创建了一个包含 CheckedTextView 的 ListView。我正在通过自定义 ArrayAdapter 提供此 ListView 中的元素。列表显示正常,但是当我选择列表项时复选框没有响应
代码:
MainActivity 类:
public class MainActivity extends ListActivity {
ArrayList<String> m_list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncHandler(this).execute();
}
public class AsyncHandler extends AsyncTask {
Context context;
public AsyncHandler(Context c) {
context = c;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT)
.show();
}
@Override
protected Object doInBackground(Object... arg0) {
getList();
return null;
}
@Override
protected void onPostExecute(Object result) {
// super.onPostExecute(result);
setListAdapter(new ElementAdapter(context, m_list));
}
private void getList() {
m_list = new ArrayList<String>();
m_list.add("Ele 1");
m_list.add("Ele 2");
m_list.add("Ele 3");
m_list.add("Ele 4");
}
}
}
适配器类
public class ElementAdapter extends ArrayAdapter implements OnClickListener {
ArrayList<String> items = new ArrayList<String>();
Context context;
CheckedTextView tvElement;
public ElementAdapter(Context c, ArrayList<String> elements) {
super(c, R.layout.row, elements);
this.items = elements;
this.context = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row, parent, false);
tvElement = (CheckedTextView) view
.findViewById(R.id.ctvElements);
tvElement.setText(items.get(position));
tvElement.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.ctvElements:
if(!tvElement.isChecked()){
tvElement.setChecked(true);
tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
}else{
tvElement.setChecked(false);
tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
}
notifyDataSetChanged();
}
}
}
行布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal">
<CheckedTextView android:id="@+id/ctvElements"
android:paddingLeft="20dip" android:paddingRight="20dip"
android:paddingTop="10dip" android:paddingBottom="10dip"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:checkMark="@android:drawable/checkbox_off_background"
android:focusable="false" />
<!-- <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false" /> <TextView
android:id="@+id/tvElement" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Element" android:textSize="25sp" /> -->
</LinearLayout>