我在 Activity 中有带有自定义 SimpleCursorAdapter 的 ListView:
public class MyListActivity extends Activity { ... }
mCursorAdapter = new MyCursorAdapter(mContext,
R.layout.listview_item01, mCursor, from, to);
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
mListView = new ListView(mContext);
mListView.setAdapter(mCursorAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
Toast.makeText(getApplicationContext(),
"You Selected Item " + Integer.toString(position),
Toast.LENGTH_LONG).show();
Log.v("onItemClick", "CLICK!");
}
});
mainLayout.addView(mListView);
我的简单适配器:
private class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
我的列表视图项目:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/item_radiobutton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/item_textview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/item_radiobutton01"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/item_radiobutton01"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
我所看到的:
我的 ListView 看起来不错,但是如果我单击任何项目,那么什么都不会发生。为什么?请帮帮我