Question

I added two imageviews on ListView's each item. I want to add these imageviews OnClick actions. I add android:OnClick="action" to imageview's on list_item.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:background="@drawable/point" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/imageView1"
        android:textColor="#000000" />

    <ImageView
        android:id="@+id/btn_youtube"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:clickable="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:background="@drawable/youtube"
        android:onClick="myClickHandler2" />

    <ImageView
        android:id="@+id/btn_music"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:clickable="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/btn_youtube"
        android:background="@drawable/musicstore"
        android:onClick="myClickHandler1" />

</RelativeLayout>

Then i add that myClickHandler1 and myClickHandler2 functions to my class. When i clicked btn_music, myClickHandler1 function was called and on btn_youtube is the same. But i can not get the position of ListView's item. I must get the position of the row number of list item which contains the clicked imageview(btn_music or btn_youtube). My class codes:

public class Tab2 extends Activity implements AdapterView.OnItemClickListener{

private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
Context context ;
Bitmap bitmap;

public static String title[] = { "song1", "son2", "song3"};

public static Integer[] imgid = {R.drawable.play, R.drawable.play, R.drawable.play};

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.tab2);
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String _status = (String) intent.getSerializableExtra("status");

    mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector<RowData>();
    for (int i = 0; i < title.length; i++) {
        try {
                rd = new RowData(i, title[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.add(rd);
    }
    CustomAdapter adapter = new CustomAdapter(this,
            R.layout.activity_list_view, R.id.list, data);
    android.widget.ListView list = (android.widget.ListView) findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
}

private class RowData {
    protected int mId;
    protected String mTitle;

    RowData(int id, String title) {
        mId = id;
        mTitle = title;
    }

    @Override
    public String toString() {
        return mId + " " + mTitle;
    }
}

private class CustomAdapter extends ArrayAdapter<RowData> {
    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<RowData> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        TextView title = null;
        ImageView i11 = null;
        RowData rowData = getItem(position);
        if (null == convertView) {
            convertView = mInflater
                    .inflate(R.layout.activity_list_view, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        title = holder.gettitle();
        title.setText(rowData.mTitle);

        return convertView;
    }

    private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private ImageView i11 = null;

        public ViewHolder(View row) {
            mRow = row;
        }

        public TextView gettitle() {
            if (null == title) {
                title = (TextView) mRow.findViewById(R.id.title);
            }
            return title;
        }

    }

}   

public void myClickHandler1(View v) 
{         
    RelativeLayout vwParentRow = (RelativeLayout)v.getParent(); 
    ListView list = (ListView)findViewById(R.id.list);
    String uri[]={"http://www.youtube.com/watch?v=rMltoD1jCGI", "http://www.youtube.com/watch?v=928obph1wo0", "http://www.youtube.com/watch?v=yFUAuIfJNJg"};      
    int position = vwParentRow.indexOfChild(v);
    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position-2])));      
}

public void myClickHandler2(View v) 
{
    RelativeLayout vwParentRow = (RelativeLayout)v.getParent();        
    String uri[]={"http://www.youtube.com/watch?v=1ogTxqidSTU", "http://www.youtube.com/watch?v=IMxDnPI_C98", "http://www.youtube.com/watch?v=koVHN6eO4Xg"};
    ListView list = (ListView)findViewById(R.id.list);
    int position = vwParentRow.indexOfChild(v);
    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position-2]))); 
}

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

}
}

Again i want to say my problem is with the functions at the bottom of Tab2 class. There is no stack, no error. But i can not control which ListItem's imageview was clicked. Thanks.

Was it helpful?

Solution

I have already asked this question. You need to implement a view holder: See this post: Android GridView clickable ImageView

You just need to re-structure your adapter. See the accepted answer in the above link.

OTHER TIPS

One simple solution for you is using TAG to manage the list items:

First-> Set TAG value to the views having Click action:

...

private class CustomAdapter extends ArrayAdapter<RowData> {
    ...

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

        convertView.findViewById(R.id.btn_youtube).setTag(position);
        convertView.findViewById(R.id.btn_music).setTag(position);
        return convertView;
    }
}   

Then, get the TAG value from View:

public void myClickHandler1(View v) 
{         
    int position = (int) v.getTag();
String uri[]={"http://www.youtube.com/watch?v=rMltoD1jCGI", "http://www.youtube.com/watch?v=928obph1wo0", "http://www.youtube.com/watch?v=yFUAuIfJNJg"};      

    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position])));      
}

public void myClickHandler2(View v) 
{
    int position = (int) v.getTag();     
    String uri[]={"http://www.youtube.com/watch?v=1ogTxqidSTU", "http://www.youtube.com/watch?v=IMxDnPI_C98", "http://www.youtube.com/watch?v=koVHN6eO4Xg"};
    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri[position]))); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top