Я пытаюсь создать список, в котором есть флажок рядом с каждым элементом. (Я следую за кодом из книги Android), но я не могу заставить его работать, каждый раз, когда я запускаю его, он падает. Так как я очень новичок в этом штате Android, я понятия не имею, что делать, чтобы заставить его работать, любая помощь приветствуется. Код ниже.
* Я создал два файла макета с именами list и list_item.
код для основной деятельности:
public class ListDemoActivity extends ListActivity {
/** Called when the activity is first created. */
String[] listItems = {"exploring", "android","list", "activities"};
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.lists);
//setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems));
//setContentView(R.layout.lists);
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
String[] cols = new String[]{People.NAME};
int[] names = new int[]{R.id.row_tv};
adapter = new SimpleCursorAdapter(this,R.layout.list_item,c,cols,names);
this.setListAdapter(adapter);
}
}
содержимое файла списков:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:stackFromBottom="true"
android:transcriptMode="normal"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit Selection"/>
</LinearLayout>
</LinearLayout>
и содержимое файла list_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_chbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/row_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>