Создание ListView с флажками ... Android - PullRequest
0 голосов
/ 17 января 2010

Я пытаюсь создать список, в котором есть флажок рядом с каждым элементом. (Я следую за кодом из книги 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>

1 Ответ

0 голосов
/ 10 апреля 2010

Я читаю ту же книгу (Apress Android 2 Pro, исходный код на http://www.apress.com/book/downloadfile/4529 - пример / Ch4 / Listing4-17)

Я обнаружил, что когда я копировал / вставлял его в проект eclipse, он не работал по той простой причине, что я не установил имя пакета вверху вставленного исходного файла.

Попробуйте поставить:

пакет asdf.adsf;

... (с вашим реальным именем пакета ;-), в верхней части файла Java, возможно, это поможет. Проблема заключалась в том, что он не использовал специфичный для проекта класс ресурсов 'R'.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...