0

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);



}
Community
  • 1
  • 1
Amir
  • 652
  • 9
  • 19
  • 2
    I believe ListEntry in the answer was a custom class used to store data. You pass a List(ArrayList) to the adapter attached to ListView. What exactly you are looking for here? Populating lists? Or getting clicks on the list items? – Keya Mar 28 '14 at 07:11
  • ListEntry is a custom class to show your list items. – Ashfaque Mar 28 '14 at 07:18
  • @Keya, Thanks, I want to get the item of listview that clicke. I have used default android listview with "android.R.layout.simple_list_item_1" layout – Amir Mar 28 '14 at 08:39

1 Answers1

4

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.

Keya
  • 859
  • 3
  • 9
  • 17
  • That is clear answer about listviews, now if i have populate my listview with database using simpleCursorAdapter. Now what is the best practice for getting the text of clicked item? ..... you write: "do something here" – Amir Mar 28 '14 at 09:30
  • You can do something like this: parent.getItemAtPosition(position); Or if you have TextView as child view you can do something like: ((TextView) view).getText().toString(); – Keya Mar 28 '14 at 09:37
  • yeah, can you please explain more this two ways? – Amir Mar 28 '14 at 09:41
  • Can you please share the code of populating your adapter i.e. what kind of data there is and what are you doing in the adapter class? If I know what you have set in the adapter I will be able to help with exact problem you are facing. – Keya Mar 28 '14 at 09:45
  • You can make your Activity extend ListActivity and override onListItemClick. Refer: [link]http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick You can get cursor using: Cursor cursor = adapter.getCursor(); cursor.moveToPosition(position); Once you have cursor, you can fetch the data from the columns. – Keya Mar 28 '14 at 10:21