0

I read many articles about this problem in stackoverflow but I didn't solve my problem. I have ListView that have rows, each row has 3 imagebuttons, when I click at my imagebutton code in "setOnItemClickListener" doesn't work.

I need which item(imagebutton) in rows was clicked. In my ListView will be many rows.

I hear about simpleCursorAdapter, fragmentlist, cursorloader, but my min required SDK is 10.

In this code I used 6 images but I going to use more images(100).

My java code :

package foxstrot.ghp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class images extends  Activity {

    int[] images = {R.raw.a2, R.raw.a3, R.raw.im2, R.raw.a4, R.raw.a5, R.raw.im6,};

    ListView lv;

    Intent i = new Intent(this, king.class);

    final String LOG_TAG = "L";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images);



        lv = (ListView) findViewById(R.id.lv);

        ArrayList<Map <String, Object>> data = new ArrayList<Map<String, Object>>(images.length);
        Map<String, Object> m;

        for(int i = 0; i < images.length; i++){


            m = new HashMap<String, Object>();
            m.put("image1", images[i]);
            m.put("image2", images[i+1]);
            m.put("image3", images[i+2]);
            i = i + 2;
            data.add(m);

        }

        String[] from = {"image1","image2","image3"};
        int[] to = {R.id.ib1, R.id.ib2, R.id.ib3};

        SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.a_, from, to);

        lv.setAdapter(sAdapter);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                i.putExtra("id", id);
                startActivity(i);
                Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
                        + id);
            }
        });






    }












}

MY XML CODE (CONTAINER WHICH USED IN ADAPTER)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="125dp"
    >



        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ib1"
            android:layout_weight="1"
            android:scaleType="fitXY"

            />

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ib2"
            android:layout_weight="1"
            android:scaleType="fitXY"
            />

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ib3"
            android:layout_weight="1"
            android:scaleType="fitXY"
            />



</LinearLayout>
Foxstrot
  • 15
  • 5
  • Show your `getView(...)` codes, do you have clicklistener for imagebutton? – Xcihnegn Apr 01 '15 at 15:41
  • I don't have clicklistener for imagebutton because I use 6 images and going to use more images, and how clicklistener defined which imagebutton was clicked for cause use listview. – Foxstrot Apr 01 '15 at 15:50
  • Class should start with upper case letter. Make your member variables private. Don't use a listview for 6 images. Use a ScrollView, inside of it a linear layout and inside of the linear layout add the images. Access your buttons in onCreate via findViewById. Cheers – JacksOnF1re Apr 01 '15 at 16:29
  • I will be use 100 images, 6 images I load at begin . – Foxstrot Apr 01 '15 at 16:52
  • Than create a new layout for the "row". The class should extend from a ViewGroup and inside of this class load the button with findViewById after each other. Implement the onClickListener then. This class should be used for the list view, you will have to create an own adapter. – JacksOnF1re Apr 02 '15 at 09:16

2 Answers2

0

In your images.xml layout file, set your ImageButtons's clickable and focusable attributes to false. The taps on the buttons would now pass-through and get handled by the ListView.

<ImageButton
    android:id="@+id/ib1"
    ...
    android:clickable="false"
    android:focusable="false" />
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • That didn't work because you asked *when I click at my imagebutton, code in "setOnItemClickListener" doesn't work* before. Now, you've changed your question! – Ravi K Thapliyal Apr 02 '15 at 02:42
0

setOnItemClickListener() is used to detect clicks on the list ITEMS (rows) not on specific buttons you may have on those items (rows).

For each button of the row on the xml you will need to add an onClick attribute with the function to handle the click: android:onClick="myFunction".

The on your activity you will have to create a function:

public void myFunction(View v) {...}

If you need to pass specific info about which row was clicked you can add a tag on the button's view on your adapter's getView() method. For more information on this check here: How do I get the row ID of the row from a ListView, which contains a clickable item?

Community
  • 1
  • 1
Evripidis Drakos
  • 872
  • 1
  • 7
  • 15
  • I need which item(imagebutton) in rows was clicked. In my ListView will be many rows. – Foxstrot Apr 01 '15 at 16:00
  • I dont understand your problem, you put an onClick function on each imagebutton (3 different functions if you want) and if you need to keep track the listview row that each button belongs to, check the link I posted on how to use settag() to achieve this. – Evripidis Drakos Apr 02 '15 at 08:12
  • Evripidis Drakos thank very much, settag() in myAdapter solved problem. – Foxstrot Apr 02 '15 at 15:52