user1079425
user1079425

Reputation:

Clickable imageView on ListView

I have a listview which should contain some data (text) and an ImageView in each item. So this is what I do, but it looks that the ImageView is not clickable :

here's a part the layout code :

<RelativeLayout 
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:orientation="vertical" 
        android:layout_alignParentLeft="true"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="5dp" >

        <ImageView
            android:id="@+id/searchIcone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:scaleType="center"
            android:background="#c7c7c7"
            android:layout_marginTop="60dp"
            android:layout_marginRight="20dp"
            android:clickable="true"
            android:focusable="false"
            android:src="@drawable/search" /> 
</RelativeLayout>

<RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_alignParentLeft="true"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="5dp" >
        <TextView
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="25dp"
            android:onClick="searchVisiter"
            android:textColor="#000" />

        <TextView
            android:id="@+id/CityCountry"
            android:layout_below="@id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="5dp"
            android:textColor="#000" />

and my Java code :

final ListView lv1 = (ListView) findViewById(R.id.ListViewEvents);
        lv1.setAdapter(new EventListViewAdapter(EventListActivity.this, records));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Object o = lv1.getItemAtPosition(position);
                JsonObject response = (JsonObject)o;
                ID = response.getString("ID");

                // If the image is clicked (doesn't work)
                final ImageView img1 = (ImageView) findViewById(R.id.searchIcone);
                img1.setClickable(true);

                img1.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
                        startActivity(intent);

                    }
                });

                // If the list item is clicked (works)
                Intent intent = new Intent(EventListActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        }); 

Please, do you have any idea about this ?

Thank you.

Upvotes: 0

Views: 3122

Answers (3)

Rajan Kali
Rajan Kali

Reputation: 12953

I have experienced the same problem.It is Because when you click on image on listItem ..the onclick() Method of ListView is triggered by Default. The Mistake is you are registering setOnItemClickListener() for your listView lv1. Instead just disable setOnItemClickListener() on listView and In your Adapter class EventListViewAdapter.java getView() Method just insert following code:

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

        //your layout inflater code here

    ImageView iv = (ImageView) convertView.findViewById(R.id.searchIcone);

            iv.setOnClickListener(new OnClickListener() 
             {
              @Override
              public void onClick(View v) {
              Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
                startActivity(intent);
               }
             });


        ....

    }

Upvotes: 0

maszter
maszter

Reputation: 3720

What you do is setting listeners after clicking item on list - it doesn't make sense. In method onItemClick you actually have View v, which was clicked, so you can handle that.

I think you tried to write something like:

lv1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        JsonObject response = (JsonObject) lv1.getItemAtPosition(position);
        ID = response.getString("ID");
        if (v.equals(findViewById(R.id.searchIcone)) {
            Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(EventListActivity.this, SearchActivity.class);
            startActivity(intent);
        }
    }
}); 

Upvotes: 1

Manishika
Manishika

Reputation: 5564

Add onClickListner to getView method of EventListViewAdapter

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

        ....

    ImageView img1 = (ImageView) convertView.findViewById(R.id.searchIcone);

            img1 .setOnClickListener(new OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
            Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
                startActivity(intent);
                    }
                });


        ....

    }

This should work

Upvotes: 0

Related Questions