Listview setonitemclicklistener

时间:2015-02-19 17:55:39

标签: android listview dialog onclick

我已经找到了关于这个问题的类似帖子,但他们没有解决我的问题,所以就是这样。

我试图设置一个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()){

        }
    }

感谢您的时间

2 个答案:

答案 0 :(得分:1)

 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.
        }

    });

答案 1 :(得分:0)

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

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

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

基本上,您正在创建自己的OnItemClickListener类,并重写OnItemClick方法。只要单击列表中的项目,就会调用此回调。您可以在该功能中执行任何您想要的处理。