Java – onitemclicklistener() does not use the list adapter
•
Java
I extract the data into the list adapter by calling a function called getalldishes() Now I want to add onitemclicklistener () to the list when I click a specific item, which will open another activity and pass the ID of the selected item. I'm new to Android All suggestions are welcome
Main activities
public class MainActivity extends ListActivity {
private DishOperation dishDBoperation;
@Override
public void onCreate(Bundle savedInstanceState) {
Button btListe;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dishDBoperation = new DishOperation(this);
dishDBoperation.open();
List values = dishDBoperation.getAllDishes();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,values);
setlistadapter(adapter);
//This is what i tried
OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
Intent intent = new Intent(MainActivity.this,Result.class);
startActivity(intent);
finish();
}
}
Solution
Try this way, you can use it
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,long id) {
// Do your Staff Here
Intent intent = new Intent(MainActivity.this,Result.class);
startActivity(intent);
}
});
or
You can use
getListView().setOnItemClickListener(listener);
After setlistadapter;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
