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>