How to select ListView item in Java

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>
    
asked by anonymous 07.06.2014 / 01:33

1 answer

2

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

To listen for these events you should 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
    }
}

I also recommend taking a look at the documentation for AdapterView.OnItemClickListener and < a href="http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html"> AdapterView.OnItemLongClickListener .

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

For selection to work with these elements, you need to tailor 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 change in layout and the creation of a% custom% for better effect.

    
07.06.2014 / 01:47