Michael
Michael

Reputation: 13

Getting text from a clicked listview item

My ListView is a list of recipes pulled from my database. I am trying to get the text of a clicked item in my ListView. The ListView is populated through a database call and a cursoradapter. I want to use the text of the selected item to make another database call in another activity. Here is the code chunk

listView.setClickable(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            final String text = ((TextView)v).getText().toString();
            recipeid = myDBAdapter.getRecipeID(text);
            Intent intent = new Intent(ListAllRecipes.this, DisplayRecipe.class);
            intent.putExtra("recipeid", recipeid);
            startActivity(intent);

        }

    });

When I run my code, I get

04-22 14:08:37.022: E/AndroidRuntime(25206): FATAL EXCEPTION: main
04-22 14:08:37.022: E/AndroidRuntime(25206): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
04-22 14:08:37.022: E/AndroidRuntime(25206):    at com.example.ketorecipes.ListAllRecipes$1.onItemClick(ListAllRecipes.java:47)

when I click on an item in the ListView.

Here is the Activity in one chunk:

public class ListAllRecipes extends Activity{
private DBAdapter myDBAdapter;
private ListView listView;
private int recipeid;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_all);



    myDBAdapter = new DBAdapter(this);
    myDBAdapter.openToRead();
    Cursor c = myDBAdapter.getValues();

    listView = (ListView)findViewById(R.id.listView1);
    String[] from = new String[] {"_id"};
    int[] to = new int[] {R.id.name_entry};
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_entry, c, from, to);
    listView.setAdapter(cursorAdapter);



    listView.setClickable(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            final String text = ((TextView)v).getText().toString();
            recipeid = myDBAdapter.getRecipeID(text);
            Intent intent = new Intent(ListAllRecipes.this, DisplayRecipe.class);
            intent.putExtra("recipeid", recipeid);
            startActivity(intent);

        }

    });
}

}

Upvotes: 1

Views: 1422

Answers (3)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

You associate _id with R.id.name_entry, so your list consists of whatever the values of _id are.

String[] from = new String[] {"_id"};
int[] to = new int[] {R.id.name_entry};  

If that is so you can get the name which is _id by

Cursor c = cursorAdapter.getCursor();
String text = c.getString(c.getColumnIndex("_id"));

Upvotes: 1

Eluvatar
Eluvatar

Reputation: 2295

I'd recommend using the position Field onItemClick to get the item from parent.getItemAtPosition(position) and that will return the entry that represents the item you just clicked (may need to cast to the proper type), that should be what you want, no need to manually get the value from the UI element.

Upvotes: 1

Gladi
Gladi

Reputation: 425

Im pretty sure that the returned view (v) isn't a textview. your list items are customized views, so you need to get the view by finding the view by id.

Upvotes: 0

Related Questions