-2

I'am trying to click a button on my list view to update that row first of all I tried to click on the item in the list view to show toast.

I did setOnItemClickListener of the ListView

public class Medlistview extends Activity{
DB db = new DB(this);
ImageButton updateimagebutton = (ImageButton)findViewById(R.id.editbuttonimage);

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medlistview);
    populatemedlistview();
}



public void populatemedlistview(){
    Cursor cursor = db.getMEDINF();

    String[] medinfo = new String[]{
            DB.col1,DB.col2,DB.col3,DB.col4,DB.rowID
    };
    int[] mappingmed = new int[]{
            R.id.MEDid,R.id.MedName,R.id.Medpurpose,R.id.medNotaplet,R.id.ROWID
    };

    SimpleCursorAdapter medcursoradapter = new SimpleCursorAdapter(this,R.layout.medlistviewxml,cursor,medinfo,mappingmed);
    ListView medlistview = (ListView)findViewById(R.id.listViewmed);
    medlistview.setAdapter(medcursoradapter);
    medlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(Medlistview.this,"clicked",Toast.LENGTH_LONG).show();
        }
    });
}








}

I made sure that my list view is clickable and focusable but still not working

One more thing I already define image button on layout item not on ListVew layout every time I run the app, is crashing and showing that null reference message in the log-cat do I need to define the button also on the ListView layout.

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

My xml layout file.

<?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:weightSum="1">

<ListView
  android:layout_width="match_parent"
    android:layout_height="282dp"
    android:id="@+id/listViewmed"
    android:layout_weight="0.40"
    android:background="#c7c0c0"
    android:clickable="true"
    android:focusable="true" />
  </LinearLayout>

Here is my list item xml layout file.

<?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">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/MEDid"
    android:layout_marginTop="40dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/MedName"
    android:layout_below="@+id/MEDid"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/Medpurpose"
    android:layout_below="@+id/MedName"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/medNotaplet"
    android:layout_below="@+id/Medpurpose"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="10dp" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editbuttonimage"
    android:layout_above="@+id/Medpurpose"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:src="@drawable/editicon" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/ROWID"
    android:layout_alignBottom="@+id/medNotaplet"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

Thanks in Advance.

OWA
  • 45
  • 6
  • 1
    Can you show us the logcat? – Emil Nov 18 '15 at 10:36
  • 1
    Log is very important for us to help you to find the problem. – ifeegoo Nov 18 '15 at 10:38
  • 3
    remove `android:clickable="true" android:focusable="true"` – M D Nov 18 '15 at 10:39
  • 1
    about clicks on list, while you have buttons at list_item look here: http://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview-android/14372750#14372750 Whats about error? Show your code, where you set onClickToButton and error logcat. – lewkka Nov 18 '15 at 10:42
  • 1
    Did you instantiate adapter ?? – Jas Nov 18 '15 at 10:43
  • is this inflated layout or normal layout – MurugananthamS Nov 18 '15 at 10:44
  • 1
    You can not click item in listView list with OnItemClickListener. See my answer below. – Vladimir Markeev Nov 18 '15 at 10:46
  • 1
    @vmarkeev Thats not correct. we can do that. – Emil Nov 18 '15 at 10:57
  • @OWA you can only call this method `findViewById` only after `setContentView`. But you are calling before that, thats your `NullPointerException`. And if its already crashing how can you click on `ListView`? – Emil Nov 18 '15 at 10:58
  • @Boss, with OnItemClickListener you can click only on all list item, to set action on view in list item you should use OnClickListener in adapter. please follow this link - http://stackoverflow.com/questions/6200604/set-onclicklistener-for-items-in-listview-with-2-views – Vladimir Markeev Nov 18 '15 at 11:03
  • @vmarkeev we can click on ListView item with `ListView.OnItemClickListener`, we need to implement click listener inside the adapter only if we want to click any views inside a single item. And what I don't understand is that How can he click on listview if its already crashing? – Emil Nov 18 '15 at 11:05
  • @Boss, according to "I'am trying to click a button on my list view", OWA want to click on view inside listView, so I recommended to use OnClickListener in adapter. – Vladimir Markeev Nov 18 '15 at 11:12
  • @vmarkeev yeah, I get your point. – Emil Nov 18 '15 at 11:14

4 Answers4

0

Set

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

for all UIs controls in your custom layout file.

Piyush
  • 18,895
  • 5
  • 32
  • 63
0

Implement View.OnClickListener() to your ImageButton in getView() method in your adapter, not in Medlistview activity. And use callback or eventBus to work with click action in your activity.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.recycler_view_item, parent, false);
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tvName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Button was clicked
        }
    });

    return convertView;
}
Vladimir Markeev
  • 654
  • 10
  • 25
0

use android:descendantFocusability="blocksDescendants" inside listview, check out this answere: can't click on listview row with imagebutton

Community
  • 1
  • 1
Touf
  • 106
  • 1
  • 6
0

Set the parent layout of the item

android:descendantFocusability="blocksDescendants"

and set

android:clickable="false"
    android:focusable="false">

on all your widgets.

Kendra
  • 769
  • 1
  • 20
  • 34