George Aristov
George Aristov

Reputation: 31

ListView setOnItemClickListener doesn't work

I do not have enough knowledge to solve my problem. Before I ask, I have been looking for an answer and tried various options.

I have ListView and setOnItemClickListener:

New version, just ListItem and setOnItemClickListener activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin">


    <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
    android:id="@+id/listView">



    </ListView>
</RelativeLayout>

item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:descendantFocusability="blocksDescendants">

    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:ems="10"
            android:id="@+id/editText"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:ems="10"
            android:id="@+id/editText1"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>
    <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>


</LinearLayout>

mainActivity, NoteItems, NoteItemsAdapter

    public class MainActivity extends Activity {


        ListView listView;
        List<NotesItems> list;
        private NoteItemsAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            list= new ArrayList<NotesItems>();
            list.add(new NotesItems(0, "",",",",", ""));
            listView= (ListView) findViewById(R.id.listView);
            adapter = new NoteItemsAdapter(this, list);
            listView.setAdapter(adapter);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> adapter, View itemClicked, int position, long id) {
                    Toast.makeText(itemClicked.getContext(), "Work", Toast.LENGTH_LONG).show();
                }

            });

        }
    }

public class NotesItems {

    private long id;
    private String title;
    private String description;
    private String fileName;
    private String packageName;




    public NotesItems(int id, String title, String description, String fileName, String packageName){
        this.description = description;
        this.title = title;
        this.id = id;
        this.fileName=fileName;
        this.packageName = packageName;

    }



    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPackageName(){return  packageName;}

    public String getNoteTitle() {
        return title;
    }

    public String getFileName() {return fileName;}





    public void setFileName(String fileName) {this.fileName = fileName;}

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

public class NoteItemsAdapter  extends BaseAdapter {

    Context context;
    private LayoutInflater layoutInflater;
    private List<NotesItems> items;

    public NoteItemsAdapter(Context view, List<NotesItems> list){
        this.context = view;
        this.items = list;
        this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
            return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        View view = convertView;
        if(view == null){
            view = layoutInflater.inflate(R.layout.item, viewGroup, false);
        }

        final NotesItems notesItems = getNoteItem(i);
        EditText editText1= (EditText)view.findViewById(R.id.editText);

        editText1.setText(notesItems.getDescription());


        EditText editText2= (EditText)view.findViewById(R.id.editText1);

        editText2.setText(notesItems.getNoteTitle());
        return view;
    }

    private  NotesItems getNoteItem(int position){
        return (NotesItems) getItem(position);
    }
}

i added TextView for test: if click on EditText - nothing happen, if click on TextView - toast appear.

I need to make EditText looks like TextView - when you click anywhere on listItem toast must appear.

Upvotes: 0

Views: 665

Answers (3)

Haggai
Haggai

Reputation: 474

To my mind it is because you are explicitly setting an onClick listeners to subviews. What is the meaning of having onClick listeners to sub view inside a List view item? It creates an ambiguous state.

Upvotes: 0

Saritha G
Saritha G

Reputation: 2608

You are performing onClick on editText view which is child Item in your listview. when you use onClick on child items of list view, onItemClick won't b work because the focus of the item is completely focused on the onClick item which editText in your case.

Please refer the below link:

Focusable EditText in the ListView and onItemClick

Upvotes: 0

Emanuel
Emanuel

Reputation: 8106

Try adding this to your Textviews and all other clickable elements to let your Adapter hook all the clicks on the whole item.

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

Upvotes: 0

Related Questions