简体   繁体   中英

Android listview: Resource Not Found Exception

I've got problem with Listview in Android. Then I try to set adapter to a listview I got Resource Not Found Exception.

My code:

public class MyActivity extends Activity {
    ArrayList<String> list;

    public void onCreate(Bundle savedInstanceState) {
        list = new ArrayList<String>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list.add("first");

        listView = (ListView)findViewById(R.id.CheckpointList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.CheckpointList, list);
        listView.setAdapter(adapter);
    }
}

And in my main.xml I've got:

<ListView android:id="@+id/CheckpointList" android:layout_height="wrap_content" android:layout_width="fill_parent"></ListView>

I've tried to clean and refesh project - no effects...

How to solve this problem?

Cheers.

The resource you hand over to the ArrayAdapter should not be the id of the ListView. It should be the textViewResourceId - which is basically which TextView-layout-id you want your items, in the list, to be rendered as.

One of the standards is eg android.R.layout.simple_list_item_1 .

Here's an example of a simple ListView:

public class ListviewExample extends Activity
{
    private ListView listView;
    private String listView_data[] = {"Android","iPhone","BlackBerry","AndroidPeople"};

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        listView = (ListView) findViewById(R.id.ListView1);
        // By using setAdapter method in listview we add the string array to the ListView.
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , listView_data));
    }
}

The ressource you hand over to the ArrayAdapter should not be the id of the listview. It should be the layout ressource of the textview in the listview. Look at the documentation: ArrayAdapter(Context context, int textViewResourceId, List objects)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM