Domanda

I know this may be very simple but ...

I am handling listview clicks according to the answer in my android app. Here is the code that use ListEntry class

ListEntry entry = (ListEntry) parent.getItemAtPosition(position);

But Eclipse can't detect this class and says "ListEntry cannot be resolved to a type" I google the "ListEntry" class and can't find anything about this, so where is my mistake?

Sorry for poor English. Thanks

Edit: here is my onLoadFinished method"

    @Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    // TODO Auto-generated method stub


    arg1.moveToFirst();
    simpleCursorAdapter = new SimpleCursorAdapter(
            getApplicationContext(),
            android.R.layout.simple_list_item_1,
            arg1,
            new String[] {  "wname" }, 
            new int[] { android.R.id.text1 },
            CursorAdapter.IGNORE_ITEM_VIEW_TYPE);

    listview.setAdapter(simpleCursorAdapter);
    simpleCursorAdapter.swapCursor(arg1);



}
È stato utile?

Soluzione

To start with ListView, declare it in your XML layout:

You can directly populate a list in the XML itself giving it a string array. Or if you want to populate it dynamically for eg. read database, you need to do it in the corresponding Activity. This you achieve by Adapters. An adapter manages your data and adapts it to the rows of the list view.

MyListAdapter is adapter class you need to create extending BaseAdapter. You need to give a list to adapter which it will display in your view.

String[] values = new String[] { "Apple", "Banana", "Cherry" };

final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
  list.add(values[i]);
}

myListAdapter = new MyListAdapter(this, list);
listView.setAdapter(myListAdapter);

You need to create a custom adapter if you want to modify the data i.e. display icon etc or reformat the string. You can use number of adapters provided by android framework for common uses like ArrayAdapter.

You can add listener to detect click on the list items:

listView.setOnItemClickListener(new OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {   
      // do something here
 }
}); 

There is this tutorial which you may found useful. http://www.vogella.com/tutorials/AndroidListView/article.html

Hope it helps. Please come back for more queries.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top