Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get click events for items within Listview in CustomAdapter and in the activity where Listview is declared

In my app I am using a ListView which has a customAdapter. In Listview the items are a TextView and a ImageButton. In OnClick event of the ImageButton, I delete an entry in the listview which I have defined in CustomAdapter. In OnClick event of TextView I have start a new activity using Intents. The problem is List.OnItemClick event is not being called in the activity and I cannot start a new Activity in CustomAdapter. How can I get a solution for onClick event of TextView.

Here is part of my code:

In Activity A (in which ListView is present in it's layout)

public class RecipientsActivity extends Activity {
....

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
        Person new_person = (Person) data.getSerializableExtra("Person");
        Log.e("Before adding", "size = " + recipientArray.size());
        recipientArray.add(new_person);
        Log.e("Add new person", "size = " + recipientArray.size());
        this.m_adapter = new CustomListAdapter(this,
                R.layout.recipients_list, recipientArray);
        list = (ListView) findViewById(R.id.rec_list);
        list.setAdapter(m_adapter);
//This event is not being called and I'm not sure if Textview's click event comes here          
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {

                Log.e("Get item clicked", "position may be " + pos);
                Intent rec_Intent = new Intent(RecipientsActivity.this,
                        RecipientAddressActivity.class);
                rec_Intent.putExtra("Recipient", recipientArray.get(pos));
                startActivity(rec_Intent);

            }

        });

    }
}

Here is my adapter code:

public class CustomListAdapter extends ArrayAdapter<Person> {

static class ViewHolder {
    public TextView txtRecName;
    public ImageButton btn_rec_del;
}

private ArrayList<Person> recipientArray;

public CustomListAdapter(Context context, int textViewResourceId,
        ArrayList<Person> objects) {
    super(context, textViewResourceId, objects);
    this.recipientArray = objects;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    final int index = position;
    View row = view;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        row = vi.inflate(R.layout.recipients_list, null);
        holder = new ViewHolder();
        holder.txtRecName = (TextView) row.findViewById(R.id.rec_name);
        holder.btn_rec_del = (ImageButton) row
                .findViewById(R.id.btn_rec_delete);
        holder.btn_rec_del.setFocusable(false);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
    Person p = recipientArray.get(position);
    if (p != null) {

        if (holder.txtRecName != null) {
            holder.txtRecName.setText(p.getName());
        }
    }

    holder.btn_rec_del.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {               
            //Log.e("Hey","position "+index);
            recipientArray.remove(index);
            notifyDataSetChanged();     

        }
    });

    /*holder.txtRecName.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Hola", "hola");
            Intent rec_Intent = new Intent(getContext(), RecipientAddressActivity.class);
            rec_Intent.putExtra("Current_Recipient", recipientArray.get(index));
            startActivity(rec_Intent);

        }
    });*/
    return row;

}

}

Here is part of the Layout file of the activity:

<ListView
        android:id="@+id/rec_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:paddingTop="20dp" />

ListView's Layout file:

<?xml version="1.0" encoding="utf-8"?>

    <TextView android:id="@+id/rec_name" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/fill_rece"
        android:focusable="false" android:textColor="#FFFFFF" android:ems="15"
        android:padding="10dp" />

    <ImageButton android:id="@+id/btn_rec_delete"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:contentDescription="@string/content" android:src="@drawable/ipad_postcare_landscape_from" />
</TableRow>

like image 445
user2688158 Avatar asked Dec 06 '25 10:12

user2688158


1 Answers

When you click on List item ImageButton takes focus. So add android:focusable="false" to ImageButton in xml.

Also if you need to use the commented code

 Context context;
 public CustomListAdapter(Context context, int textViewResourceId,
        ArrayList<Person> objects) {
    super(context, textViewResourceId, objects);
    this.recipientArray = objects;
    this.context = context;  // initialize
}

holder.txtRecName.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Hola", "hola");
            Intent rec_Intent = new Intent(context, RecipientAddressActivity.class);
            rec_Intent.putExtra("Current_Recipient", recipientArray.get(index));
            context.startActivity(rec_Intent); 

        }
    });
like image 187
Raghunandan Avatar answered Dec 08 '25 00:12

Raghunandan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!