我有这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"/>
</LinearLayout>
行布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/rowTextView"
style="@style/HeaderTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:textSize="16sp" >
</TextView>
<CheckBox android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
在我的活动中,我能够显示列表值,并且能够选择和取消选择它们,但是,我无法获得所选的列表值。
以下是我正在做的事情:
private ListView listView;
public class MyActivity extends BaseActivity {
protected void onCreate (Bundle instance1) {
listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
new MyTask().execute(); //loads the list from server
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
int totalItems = 0;
SparseBooleanArray checked = listView.getCheckedItemPositions();
Log.d("checked size: ", checked.size()+"");
for (int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i) == true) {
Tag tag = (Tag) listView.getItemAtPosition(checked.keyAt(i));
Log.i("xxxx", i + " " + tag);
totalItems++;
}
}
Toast.makeText(this, "Got click: " + totalItems, Toast.LENGTH_SHORT).show();
}
return true;
}
}
总项目始终为零,日志中checked size
也始终为0。
我做错了什么?
我的任务在onPostExecute()
listAdapter = new MyCustomAdapter(getApplicationContext(), myList);
listView.setAdapter(listAdapter);
答案 0 :(得分:0)
你的onCreate方法下面的代码行在哪里?
super.onCreate(instance1);
setContentView(R.layout.yourlayout);