A simple item list item does not select items

I created a list of arrays and displayed it as a list with a simple selection of the list item, but I cannot check or mark the items in the list, when I click on the items, nothing happens. Please check my code below and tell me what I am doing wrong.

package com.example.arrays; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemLongClickListener; public class MainActivity extends Activity { ListView showList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView show = (TextView)findViewById(R.id.txtShow); final Random generate = new Random(); showList = (ListView)findViewById(R.id.listView1); final String[] myAttraction = new String[4]; myAttraction[0]= "Walter Sisulu National Botanical Garden "; myAttraction[1]= "Coca-Cola Dome"; myAttraction[2]= "Promusica Theatre"; myAttraction[3]= "Unisa Science Campus"; Button arrays = (Button)findViewById(R.id.button1); arrays.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*int random = generate.nextInt(4); String display = myAttraction[random]; show.setText(display);*/ ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction); showList.setAdapter(adapter); } }); showList.setOnItemLongClickListener(new OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show(); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 
0
source share
3 answers

Add OnItemClickListener like this to check / uncheck CheckedTextView when user click on item

 showList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // change the checkbox state CheckedTextView checkedTextView = ((CheckedTextView)view); checkedTextView.setChecked(!checkedTextView.isChecked()); } }); 
+2
source

change your code as follows:

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction); showList.setAdapter(adapter); arrays.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*int random = generate.nextInt(4); String display = myAttraction[random]; show.setText(display);*/ } }); showList.setOnItemClickListener(new OnItemClickListener() { public boolean onItemClick(AdapterView<?> arg0, View arg1, int pos, long id) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show(); return true; } }); 
0
source

Set the choiceMode property of your list to multipleChoice . I implement several list options this way in my applications and certainly works.

0
source

Source: https://habr.com/ru/post/1347239/


All Articles