Skip to content Skip to sidebar Skip to footer

Android Listview Itemclick

I'm trying to make a ListView elements clickable but I'm getting ClassCastException. Here is the code: public class MenuActivity extends Activity { @Override protected void onCrea

Solution 1:

Use like this, you can not directly get TextView from itemClicked

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            publicvoidonItemClick(AdapterView<?> parent, View itemClicked, int 
                                                    position, long id) {

                TextView textView = (TextView) itemClicked.findViewById(R.id.textView_List_Item);;                 
                String strText = textView.getText().toString();

                if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_igraj)))    
                    {
                    startActivity(new Intent(MenuActivity.this, GameActivity.class));
                } elseif 
                    (strText.equalsIgnoreCase(getResources().getString(R.string.menu_pomoc))) {
                    startActivity(new Intent(MenuActivity.this, HelpActivity.class));
                } elseif 
                   (strText.equalsIgnoreCase(getResources().getString(R.string.menu_postavke))) {
                    startActivity(new Intent(MenuActivity.this, SettingsActivity.class));
                } elseif 
                  (strText.equalsIgnoreCase(getResources().getString(R.string.menu_rezultati))) {
                    startActivity(new Intent(MenuActivity.this, ScoresActivity.class));
                } elseif 
                    (strText.equalsIgnoreCase(getResources().getString(R.string.menu_izlaz))) {
                    exit();
                }

            }
        });

}

Solution 2:

That's because your clicked item is not of a TextView class; your cast is wrong in that line, you need to get the TextView from the item's view that has been clicked, that view is being passed to you through the click callback.

Solution 3:

Why don't you just get the position from the ListView that was selected and retrieve the String from your ListViewAdapter data.

For example:

publicvoidonItemClick(AdapterView<?> parent, 
                        View itemClicked,
                        int position, 
                        long id) {

            StringselectedItem= items[position];//you may need to make String[] items final for this, or make it a global variable to the class//do stuff with String
}

This way, you don't have to worry about shakey class castings. Hope this helps!

Solution 4:

1) Change your customAdapter class's getItem method

// CustomAdapter @OverridepublicObjectgetItem(int arg0) {
    // return arg0;return m_items[arg0];
}

2) Change your onItemClicked logic;

list.setOnItemClickListener(newAdapterView.OnItemClickListener() {
     publicvoidonItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
          CustomAdaptercustomAdapter= (CustomAdapter)parent.getAdapter();
          StringselectedText= (String)customAdapter.getItem(position);
          // Use your selected text here
     }
}

This will solve your problem.

Post a Comment for "Android Listview Itemclick"