jainal
jainal

Reputation: 3021

How to know in which view of a listView row has been clicked?

I have a list view.Each row of the this listview is the following layout.How can I know in which view, either ImageView(buddy_row_image) or TextView(chat_msg_count) user has been clicked.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:background="@drawable/cell"

   >

    <ImageView
       android:id="@+id/buddy_row_image"
       android:layout_gravity="center_vertical"
       android:layout_marginLeft="20dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginRight="2dp"
       />
    <TextView 
        android:text="1"
        android:textStyle="bold"
        android:gravity="center_vertical|center_horizontal"
        android:id="@+id/chat_msg_count"
        android:background="@drawable/tag"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

Upvotes: 3

Views: 1710

Answers (3)

Maurycy
Maurycy

Reputation: 3961

I agree with Carnal. quick tip however since you are using only imageview and textview you can use a SimpleAdapter making your life alot easier...

SimpleAdapter adapter = new SimpleAdapter(this, SomeHashMap, R.layout.list_row, new String[]{"img","text"}, new int[]{R.id.image,R.id.textviewid});

  ListView list = (ListView) findViewById(R.id.listView1);    
  list.setAdapter(adapter);

  list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
       // capture int position and use that to determine which row was selected
... and so on

Also I ended up making a hashmap attached with a key value such as

map.put("person","My Name");

Then check for names when clicked using something like....

if (map.get(position).get("person").equals("My Name"))
//do something

If this is confusing then just google "android listview simpleadapter" and you should find more in depth articles/tutorials.

Upvotes: 1

Carnal
Carnal

Reputation: 22064

If I hear you right, each row has a imageview and a textview, and you want to be able to click on the imageview or the textview, and then see which position that textview or imageview contains in the listview, this is how you do it:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.your_layout_for_each_row, null);
        }

        TextView text = (TextView) v.findViewById(R.id.your_text);
        ImageView img = (ImageView) v.findViewById(R.id.your_image);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.d("TAG", "position: " + position);
            }
        });
        return v;

And you'll do the same for ImageView with the onclicklistener.

Upvotes: 2

Dayerman
Dayerman

Reputation: 4003

You can capture the onClickEvent follow this

Set setOnClickListener inside your adapter.

Upvotes: 0

Related Questions