Action Imageview in Listview

Asked

Viewed 728 times

0

Good night,

I’m developing an application for android, and I have on my main screen a Listview and an Activity with two Imageview and a Textview, this Activity is called in the main class that controls Listview, until then everything right.

What I would like to know is if it is possible to make one of the Imageview when clicked (since it belongs to Listview), take the Listview id in your onClick action.

Does anyone know if that’s possible?

Follows the codes:

Main screen where has the Listview

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
>

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >




    <ListView
        android:id="@+id/listViewPlacas"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|top"
        android:divider="#FFFFFF"
        android:dividerHeight="4dp"
        android:layout_weight="1"
        android:listSelector="@drawable/list_selector"
        android:background="@drawable/fundo_list"
        />


</FrameLayout>

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="checklist.system.cooper.br.syschecklist.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

Screen where have the items of Listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<View
    android:id="@+id/Vbarra"
    android:layout_height="2dp"
    android:layout_width="335dp"
    android:background="#3929769f"
    android:visibility="gone" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/imagemview"
        android:layout_width="64dp"
        android:layout_height="47dp"
        android:src="@drawable/checklist"
        android:layout_gravity="top" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/txtPlacaPrincipal"
        android:layout_weight="0.40"
        android:textColor="#ff000000"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="0"
        android:id="@+id/txtQuantidade"
        android:layout_weight="0.22"
        android:textColor="#ff000000"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imagemAdd"
        android:layout_width="64dp"
        android:layout_height="47dp"
        android:src="@drawable/ic_action_add_to_queue"
        android:layout_gravity="top"
        android:onClick="addCheckList"/>


</LinearLayout>

Code of call from Imageview

public void addCheckList(View v)
{

}

Thank you

  • Try following this tutorial: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/. With it I was able to make clickable areas inside an item on ListView

2 answers

2

Try this

listView.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });

0

Place the click event on the Adapter in your listview:

See if that helps you.

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

public ListAdapter(Context context, int resource, List<Item> items) {
    super(context, resource, items);
}

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

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.itemlistrow, null);

    }

        final Item item = getItem(position);

        TextView txtQuantidade = (TextView) v.findViewById(R.id.txtQuantidade);
        TextView txtPlacaPrincipal= (TextView) v.findViewById(R.id.txtPlacaPrincipal);
        ImageView imagemAdd= (ImageView )v.findViewById(R.id.imagemAdd);

         txtQuantidade.setText(String.valueOf(item.getQtde());
         txtPlacaPrincipal.setText(item.getPlacaPrincipal());

        imagemAdd.setOnClickListener(new OnClickListener() {
         public void onClick(View v)
         {
             //evento

         } 
           });




    return v;

}
  • Someone could help me in this situation?

  • Didn’t that post help you? What’s wrong?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.