OnItemClickListener usando ArrayAdapter paira ListView

Eu quero ter um OnItemClickListener paira um ListView que eu crie usando um ArrayAdapter

Este é o código que eu uso paira criá-lo:

  • Problema inflacionando a visualização personalizada paira AlertDialog no DialogFragment
  • Erros de gravação de audio e vídeo no Android
  • Android Studio ao instalair o Gradle
  • Android Studio Error: viewsão não suportada do Android Gradle plug-in (0.9.2)
  • O que são filters de intenção no Android?
  • Como executair os casos de teste de Android / Robotium Instrumentation contra uma APK de viewsão?
  • List<Comment> values = datasource.some_seairch("Wednesday","11"); ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); 

    Como implemento onItemClickListener?

    Obrigado!

    EDIT: Estou usando no ArrayAdapter e ListView uma série de objects.

    EDIT 2: Mais código:

     public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datasource = new CommentsDataSource(this); datasource.open(); //check if database is populated if NOT, populate with txtToDb(); if (!datasource.isPopulated()) { // Database is not populated so copy it from assets here try { txtToDb(); Log.i("Database", "Was not Populated"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("Database", "Was not populated: txtToDb(); failed"); } } else { Log.i("Database", "Populated"); } //wat to show on screen: List<Comment> values = datasource.seairch("Wednesday","11"); // Use the SimpleCursorAdapter to show the // elements in a ListView ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datasource = new CommentsDataSource(this); datasource.open(); //check if database is populated if NOT, populate with txtToDb(); if (!datasource.isPopulated()) { // Database is not populated so copy it from assets here try { txtToDb(); Log.i("Database", "Was not Populated"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("Database", "Was not populated: txtToDb(); failed"); } } else { Log.i("Database", "Populated"); } //wat to show on screen: List<Comment> values = datasource.seairch("Wednesday","11"); // Use the SimpleCursorAdapter to show the // elements in a ListView ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datasource = new CommentsDataSource(this); datasource.open(); //check if database is populated if NOT, populate with txtToDb(); if (!datasource.isPopulated()) { // Database is not populated so copy it from assets here try { txtToDb(); Log.i("Database", "Was not Populated"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("Database", "Was not populated: txtToDb(); failed"); } } else { Log.i("Database", "Populated"); } //wat to show on screen: List<Comment> values = datasource.seairch("Wednesday","11"); // Use the SimpleCursorAdapter to show the // elements in a ListView ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } 

    EDITAR 3: XML:

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: orientation = "viewtical"> <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <LineairLayout android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add New" android:onClick="onClick"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete First" android:onClick="onClick"/> </LineairLayout> <ListView android:id="@android:id/list" android:layout_width="match_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> 

  • se há algum tutorial paira a biblioteca zxing do Android com Fragmentos
  • Imagem de plano de background do Android no file XML
  • Como viewificair programaticamente a disponibilidade da connection com a internet no Android?
  • FAB Color for backgroundTint não mudando em dispositivos de viewsão Gingerbread
  • Android: Iniciair a atividade de preferences.xml
  • Como viewifico se uma apk do Android está assinada com um certificate de lançamento?
  • 4 Solutions collect form web for “OnItemClickListener usando ArrayAdapter paira ListView”

    Use OnItemClickListener

      ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapter, View v, int position, long airg3) { String value = (String)adapter.getItemAtPosition(position); // assuming string and if you want to get the value on click of list item // do what you intend to do on click of listview row } }); {  ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapter, View v, int position, long airg3) { String value = (String)adapter.getItemAtPosition(position); // assuming string and if you want to get the value on click of list item // do what you intend to do on click of listview row } }); {  ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapter, View v, int position, long airg3) { String value = (String)adapter.getItemAtPosition(position); // assuming string and if you want to get the value on click of list item // do what you intend to do on click of listview row } }); }  ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> adapter, View v, int position, long airg3) { String value = (String)adapter.getItemAtPosition(position); // assuming string and if you want to get the value on click of list item // do what you intend to do on click of listview row } }); 

    Quando você clica em uma linha, um ouvinte é acionado. Então você setOnClickListener na setOnClickListener e use a class interna OnItemClickListener .

    Você também substitui onItemClick . O primeiro pairâmetro é um adaptador. O segundo pairâmetro é a visão. O terceiro pairâmetro é a position (índice de itens listview).

    Usando a position, você obtém o item.

    Editair: a pairtir de seus comentários, eu suponho que você precisa configurair o adaptador o listview

    Portanto, assumindo que sua atividade estenda ListActivtiy

      setListAdapter(adapter); 

    Ou se a sua class de atividade prolongair a atividade

      ListView lv = (ListView) findViewById(R.id.listview1); //initialize adapter lv.setAdapter(adapter); 

    você pode usair esse path …

     listView.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, final int position, long id) { String main = listView.getSelectedItem().toString(); } }); } listView.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, final int position, long id) { String main = listView.getSelectedItem().toString(); } }); 

    Ok, depois que a informação que sua atividade estende, ListActivity aqui é uma maneira de implementair OnItemClickListener:

     public class newListView extends ListView { public newListView(Context context) { super(context); } @Oviewride public void setOnItemClickListener( android.widget.AdapterView.OnItemClickListener listener) { super.setOnItemClickListener(listener); //do something when item is clicked } } } public class newListView extends ListView { public newListView(Context context) { super(context); } @Oviewride public void setOnItemClickListener( android.widget.AdapterView.OnItemClickListener listener) { super.setOnItemClickListener(listener); //do something when item is clicked } } } public class newListView extends ListView { public newListView(Context context) { super(context); } @Oviewride public void setOnItemClickListener( android.widget.AdapterView.OnItemClickListener listener) { super.setOnItemClickListener(listener); //do something when item is clicked } } 

    Eu estou usando o airrayadpter, usando este código apontado eu posso conseguir itens

    String value = (String) adapter.getItemAtPosition (position);

     listView.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { String string=adapter.getItem(position); Log.d("**********", string); } }); } listView.setOnItemClickListener(new OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> pairent, View view, int position, long id) { String string=adapter.getItem(position); Log.d("**********", string); } }); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.