0

I set up a Listview in Android Studio but need help with coding a OnItemClickListner.

I have tried the code, but doesn't seem to work.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.list_view);

        ArrayList<Object> list = new ArrayList<>();
 list.add(new LTCItem("30.06 Sign Violations","Submit A Complaint To Texas Attorney General",R.drawable.gavel));
        list.add(new LTCItem("U.S. & Texas LawShield","Legal Defense For Self Defense",R.drawable.lawshield));

        listView.setAdapter(new LTCAdapter(this, list));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });

    }
}

Below is my list_view file. Where in the file do block descendantFocusability as suggested? Do I put it under listView? Sorry I am learning .

<?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:alwaysDrawnWithCache="true"
    android:background="#000000"
    android:padding="8dp">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/itemListViewImgIcon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@+id/itemListViewImgIcon"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/itemListViewTxtTopicName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

    <TextView
        android:id="@+id/itemListViewTxtTopicSubtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/itemListViewTxtTopicName"

</RelativeLayout>

Ok I added the adapter code which is a Java Class item. Where do I add the code here?

public class LTCAdapter extends BaseAdapter {
    ArrayList<Object> list;
    private static final int LTC_Item = 0;
    private static final int HEADER = 1;
    private LayoutInflater inflater;


    public LTCAdapter(Context context, ArrayList<Object> list) {
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position) instanceof LTCItem) {
            return LTC_Item;
        } else {
            return HEADER;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            switch (getItemViewType(i)) {
                case LTC_Item:
                    view = inflater.inflate(R.layout.item_list_view, null);
                    break;
                case HEADER:
                    view = inflater.inflate(R.layout.item_listview_header, null);
                    break;
            }
        }

        switch (getItemViewType(i)) {
            case LTC_Item:
                ImageView image = (ImageView) view.findViewById(R.id.itemListViewImgIcon);
                TextView name = (TextView) view.findViewById(R.id.itemListViewTxtTopicName);
                TextView subtitle = (TextView) view.findViewById(R.id.itemListViewTxtTopicSubtitle);

                image.setImageResource(((LTCItem) list.get(i)).getImage());
                name.setText(((LTCItem) list.get(i)).getName());
                subtitle.setText(((LTCItem) list.get(i)).getSubtitle());
                break;
            case HEADER:
                TextView title = (TextView) view.findViewById(R.id.itemListViewHeader);
                title.setText(((String) list.get(i)));
                break;
        }
        return view;
    }
}

RKRK
  • 1,284
  • 5
  • 14
  • 18
blaw
  • 35
  • 4
  • maybe you can check this question about listview click problem [link](https://stackoverflow.com/a/13415566/4593755), and you also can use [RecyclerView](https://developer.android.com/guide/topics/ui/layout/recyclerview) instead – Jeffery Ma May 16 '19 at 02:50
  • @blaw you can simply put listView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS); and also it will be better to use recyclerView instead of listView – Mudassir Khan May 16 '19 at 03:31
  • 1
    You are not adding any code inside `onItemClick`. – John Joe May 16 '19 at 03:37
  • 1
    As @JohnJoe mentioned above, nothing will happen since you are not handing the click events inside `onItemClick`. – HB. May 16 '19 at 03:45
  • You should use different layout for your activity and your adapter list_item. As per your code, it seems you are using both `ListView` and items. Please check this it might be helpful: https://stackoverflow.com/questions/55878139/single-row-shows-insight-recyclerview/55878396#55878396 – Ajay Mehta May 16 '19 at 05:37

2 Answers2

0

As your list view contains focusable elements, you need to write this piece of code in parent layout in your list view item xml file

android:descendantFocusability="blocksDescendants"
Karthik
  • 1,088
  • 6
  • 17
  • I added my view item xml file. Where do I write this piece of code? Thanks – blaw May 16 '19 at 03:09
  • I want you to update the piece of code in xml file which is associated with LTCAdapter. Not the activity xml file. – Karthik May 16 '19 at 03:15
  • I added the java class adapter file. Where do I add the code? or do you mean activity_main? – blaw May 16 '19 at 03:21
  • I was looking for LTCAdapter.java not LTCItem.java and LTCItem is a pojo class not Adapter. – Karthik May 16 '19 at 03:24
  • I just added the Adapter file. Where would I add the code? thanks – blaw May 16 '19 at 03:29
  • Open your In the layout item_list_view and in the for the parent layout, Add the above line. – Karthik May 16 '19 at 03:33
  • My list view is included above, however if I add the code nothing happens. would it be activity_main.xml? – blaw May 16 '19 at 03:36
  • That is not this view, if I am not wrong. item_list_view is a different file as above. hover on R.layout.item_list_view, ctrl + click on item_list_view. That is the view we need. – Karthik May 16 '19 at 03:38
  • I have (3) xml files: (1)activity_main;(2)item_list_view and (3)item_listview_header – blaw May 16 '19 at 03:41
  • Do you have list view inside a list view. – Karthik May 16 '19 at 03:42
  • there is a – blaw May 16 '19 at 03:44
  • I just wanted to figure out what you really want. You dont need to have list view in item_list_view when you are not using it. So, you are telling that didnt work out. Let me get back to you. I will create a new project and I will work this out. – Karthik May 16 '19 at 03:48
  • Ok, my email is bernard.lawson@gmail.com if needed. Thanks – blaw May 16 '19 at 03:50
0

Your ListView element doesn't have an ID, you should add android:id="@+id/list_view" to it in the XML file.

Here is an example:

<ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
Titus
  • 22,031
  • 1
  • 23
  • 33