3

OnItemClick & OnItemLongClick not work when just Button put in listview

when i am taking just button in listview
at that time i am shock to see OnItemClick & OnItemLongClick not work . Please help...

dbshow.java

public class dbshow extends Activity implements OnItemClickListener,
        OnItemLongClickListener {

    private ListView lv_database;
    private DAtahelper mhelper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        init();

        showdataquery();

    }

    private void init() {
        // TODO Auto-generated method stub
        lv_database = (ListView) findViewById(R.id.lv_database);
        lv_database.setOnItemClickListener(this);
        lv_database.setOnItemLongClickListener(this);

    }

    @SuppressLint("NewApi")
    private void showdataquery() {
        // TODO Auto-generated method stub

        mhelper = new DAtahelper(this);
        db = mhelper.getReadableDatabase();

        String columns[] = { "_id,name,city,phone" };

        Cursor c = db.query("vishal", columns, null, null, null, null, null);
        c.moveToFirst();

        String[] from = { "name", "city", "phone" };
        int[] to = { R.id.tv_name, R.id.tv_city, R.id.tv_phone };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(), R.layout.showdata, c, from, to, 0) {

        };
        lv_database.setAdapter(adapter);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        Log.e("", "long");
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        Log.e("", "item");

    }
}

listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_database"
        android:layout_width="match_parent"

        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

showdata.xml

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

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/tv_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/black" />

</LinearLayout>

image of listview

enter image description here

Vishal Patel
  • 2,931
  • 3
  • 24
  • 60

2 Answers2

1

Please take a look at this tutorial. http://examples.javacodegeeks.com/android/core/ui/listview/android-multitouch-listview-example/

Basically, you have to create your custom ArrayAdapter, and on its xml and getView to have something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Name:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textName"
        android:layout_marginTop="5dp"
        android:text="Age:"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btnEdit"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#99CC"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Edit"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnEdit"
        android:layout_marginTop="3dp"
        android:background="#99CC"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Delete"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/textAddr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textAge"
        android:layout_marginTop="5dp"
        android:text="Address:" />

</RelativeLayout>

And for the custom adapter:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View item = convertView;
        StudentWrapper StudentWrapper = null;

        if (item == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            item = inflater.inflate(layoutResourceId, parent, false);
            StudentWrapper = new StudentWrapper();
            StudentWrapper.name = (TextView) item.findViewById(R.id.textName);
            StudentWrapper.age = (TextView) item.findViewById(R.id.textAge);
            StudentWrapper.address = (TextView) item.findViewById(R.id.textAddr);
            StudentWrapper.edit = (Button) item.findViewById(R.id.btnEdit);
            StudentWrapper.delete = (Button) item.findViewById(R.id.btnDelete);
            item.setTag(StudentWrapper);
        } else {
            StudentWrapper = (StudentWrapper) item.getTag();
        }

        Student student = students.get(position);
        StudentWrapper.name.setText(student.getName());
        StudentWrapper.age.setText(student.getAge());
        StudentWrapper.address.setText(student.getAddress());

        StudentWrapper.edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Edit", Toast.LENGTH_LONG).show();
            }
        });

        StudentWrapper.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Delete", Toast.LENGTH_LONG).show();
            }
        });

        return item;

    }

And in your main Activity, you can also enable click on the listview items:

listview.setOnItemClickListener(new OnItemClickListener() {

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

    Toast.makeText(MainActivity.this,
      "List Item Clicked:" + position, Toast.LENGTH_LONG)
      .show();
   }
  });
A B
  • 338
  • 3
  • 10
-1

Set the click listener(OnClickListener) on the button in the getView method of your custom Adapter class.

Steps - Read up on custom adapters for your listview and implement it using the View Holder pattern. [The new RecyclerView enforces the view holder pattern.] - Set the click listener on the button[contained in your listview row] in the getView method of your own adapter class[eg : BaseAdapter].

Rohan
  • 593
  • 7
  • 22