How to Select Java Listview Item

Asked

Viewed 4,810 times

0

How to use the method to work with the selected Listview item?

Activity:

public class ListaProntos extends Activity implements AdapterView.OnItemClickListener {
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

DataBaseHandler db = new DataBaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
List<Contact> contacts = db.getAllContacts("Pronto");
for (Contact cn : contacts) {

//add contacts data in arrayList
imageArry.add(cn);

}
adapter = new ContactImageAdapter(this, R.layout.screen_list,imageArry); 
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);

dataList.setOnItemClickListener(this); // Clique no item
}


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Código para trabalhar com o item que foi clicado
    // position é a posição do item no adapter

    Toast.makeText(getApplicationContext(),"Position of Selected Item is :-"+position,Toast.LENGTH_LONG).show(); 
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.55" >
</ListView>
</LinearLayout>

scree_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/cinza_claro"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textBancada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Orden de Serviço: "
            android:textColor="@color/azul_escuro"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="os"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cliente:  "
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/txtCliente"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="Cliente"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <TextView
        android:id="@+id/txtDescricao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Descrição"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="295dp"
        android:layout_height="200dp"
        android:gravity="center_horizontal"
        android:src="@drawable/camera" 

         />

    <TextView
        android:id="@+id/textDatahor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data hora"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_settings"
            android:onClick="menuAcao" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Situação:"
                android:textAppearance="?android:attr/textAppearanceSmall" 
                android:textColor="@color/azul_escuro"/>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Localização: "
                android:textColor="@color/azul_escuro"
                android:textAppearance="?android:attr/textAppearanceSmall" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="status"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/textLocal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="local"
                android:textAppearance="?android:attr/textAppearanceSmall" />

        </LinearLayout>

    </LinearLayout>


    <TextView
        android:id="@+id/textID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:visibility="gone"/>

</LinearLayout>
  • what you want to do exactly in your item click method of a ListView? You happen to want to retrieve your object Contact Adapter? So far you have some error?

  • you are asking a lot of questions in the Portuguese OS and leaving it behind, without giving back if you succeeded or not. Whether or not you let go.

1 answer

2


You must register a OnItemClickListener or a OnItemLongClickListener in his ListView, when an item is selected (ItemClick) or pressed (ItemLongClick) you will be notified.

To listen for these events you must do something like:

public MyActivity extends Activity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    public void onCreate(Bundle savedInstanceState) {
        ListView dataList = (ListView) findViewById(R.id.list);

        dataList.setOnItemClickListener(this); // Clique no item
        dataList.setOnItemLongClickListener(this); // Pressão sobre o item

        // Mais codigo...
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Código para trabalhar com o item que foi clicado
        // position é a posição do item no adapter
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // Código para trabalhar com o item que foi pressionado
        // position é a posição do item no adapter
    }
}

In addition I recommend taking a look at the documentation of AdapterView.OnItemClickListener and of AdapterView.OnItemLongClickListener.

I have a suggestion for the code to work, remove any View that generates action or focus (Button, Imagebutton, Edittext, etc...) of your item layout.

In order for the selection to work with these elements, you need to adapt the layout of your ListView. For this I suggest reading the following article: Several Clickable Areas in Listview and Multitouch Listview.

They required a major layout change and the creation of a View customized for best effect.

  • Wakim! take a look at these mistakes: dataList.setOnItemClickListener(this); //The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (ListaProntos) dataList.setOnItemLongClickListener(this);//The method setOnItemLongClickListener(AdapterView.OnItemLongClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (ListaProntos)

  • @Emersonbarcellos, don’t miss putting implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener in your class ListProntos?

  • Really missing! but it’s amazing how nothing happens when you click on the list. should be?

  • Could update the code including the layout of the inflated item on Adapter?

  • Okay, updated!

Browser other questions tagged

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