Listview setonitemclicklistener

Listview setonitemclicklistener

关于这个问题,我已经查找过类似的 post,但他们没有解决我的问题,所以在这里。

我正在尝试设置一个 setOnItemClickListener,因为当我单击列表中的任何项目时,我将打开一个对话框,其中包含单击列表上的信息(从列表中检索)和更多信息。但是我正在努力实现 setOnItemClickListener 部分。

我在行中遇到错误:myList.setOnItemClickListener(new OnItemClickListener()){

package com.example.proyectoprueba;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;;

public class PlantillaChina extends ActionBarActivity{

     // DB Class to perform DB related operations
    DBController controller = new DBController(this);
    // Progress Dialog Object
    ProgressDialog prgDialog;
    HashMap<String, String> queryValues;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista);
        Intent myIntent = getIntent();

        String value = myIntent.getStringExtra("key");
        TextView titulo=(TextView)findViewById(R.id.titulo);

     // Get Platos records from SQLite DB

        ArrayList<HashMap<String, String>> platoList = controller.getAllEntremeses();
        if (value.equals("entremeses")){
            platoList = controller.getAllEntremeses();
            titulo.setText(value);
        }else if(value.equals("arroces y pasta")){
                platoList = controller.getAllArrocesyPasta();
                titulo.setText(value);
            }else if(value.equals("mar")){
                    platoList = controller.getAllMar();
                    titulo.setText(value);
                 }else if(value.equals("carnes")){       
                            platoList = controller.getAllCarnes();
                            titulo.setText(value);
                 }
        // If Platos exists in SQLite DB
        if (platoList.size() != 0) {
            // Set the Plato Array list in ListView
            ListAdapter adapter = new SimpleAdapter(PlantillaChina.this, platoList, R.layout.itemlista, new String[] {
                            "platoId", "platoNombre", "platoDescripcion", "platoPrecio" }, new int[] {R.id.codigo, R.id.nombre, R.id.descripcion, R.id.precio });
            ListView myList = (ListView) findViewById(R.id.listaplatos);
            myList.setAdapter(adapter);
            myList.setOnItemClickListener(new OnItemClickListener()){

        }
    }

感谢您的宝贵时间

您需要实际定义 OntItemClickListener 才能执行某些操作。

首先,你的代码没有多大意义。你想要这样的东西:

myList.setOnItemClickListener(new OnItemClickListener(){
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
    {
        // Your code here
    }
});

基本上您是在创建自己的 OnItemClickListener class,并重写 OnItemClick 方法。只要单击列表中的项目,就会调用此回调。您可以在该函数内进行任何处理。

 It think you want something like this.

 myList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int  
                               position, long id) 
        {
            // You have the position available here as well as the clicked view.
        }

    });