Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on list item of a ListView doesn't respond

I am implementing ListView in my code. But When I click on the items, it does not respond respond to the click in any way. Could someone help me please? Thanks in advance .

Here is the code .

public class ListaActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.provacomunicazione.MESSAGE";
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.lsta);
        Resources res = getResources();
        String[] Authors = res.getStringArray(R.array.Lista_Nomi_E_Cognomi_Autori);
        ArrayList<String> Autori = new ArrayList<String>();
        for (String temp:Authors) {
            Autori.add(temp);
        }
        Collections.sort(Autori);
        ArrayList<String> AutoriLetteraSelezionata = new ArrayList<String>();
        for (String temp:Autori) {
            if (temp.charAt(0)=='A') {
                AutoriLetteraSelezionata.add(temp);
            }
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.textviewitem, AutoriLetteraSelezionata);
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);
        listView.setClickable(true);
        listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

            CharSequence autore = "("+((TextView)view).getText()+")";

            Intent i = new Intent(ListaActivity.this, SecondaryActivity.class);
            i.putExtra(EXTRA_MESSAGE, autore);
            startActivity(i);  
        });
    }
}    
like image 356
Domenico Pacecca Avatar asked Aug 27 '13 20:08

Domenico Pacecca


People also ask

How can you react to click events on an item of a ListView?

How can you react to click events on an item of a ListView? Place an empty linearlayout below the listview by setting an appropriate height to the listview. Place an onClick() method to that linear layout. That must do it.

Which code is used when the list item is picked from the ListView?

Which method is used to obtain the selected option from a ListView? An API is used to get selected items from the list items. This is called as the getSelectedItems method.

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

How check ListView is empty or not in android?

just check if (cartlist. size()<0) then yours list is Empty.!


2 Answers

You should define on all of the child objects in the item listview (TextView, ImageView etc.):

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

And for the root item RelativeLayout / LinearLayout and so, define:

android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

If you won't define those, they will "catch" the click event. And if you have a Custom listView adapter, just check that you override:

@Override
public boolean isEnabled(int position)
{
    return true;
}
like image 184
Woody Avatar answered Oct 29 '22 23:10

Woody


In my case a problem was in fact that a ListView contained HorizontalScrollViews.

HSV consumes clicks over items and doesn't return OnItemClick to the ListView.

I solved the problem when wrote an OnClickListener inside an adapter that returns a callback to the ListView. See here: https://stackoverflow.com/a/43653085/2914140.

like image 2
CoolMind Avatar answered Oct 29 '22 23:10

CoolMind