Listview onItemClick事件未触发

时间:2014-02-05 15:58:51

标签: android

我为listview设置了一个事件监听器(onclick),但它没有触发。 以下是我的代码。我把logcat条目作为测试。但是没有打印日志条目而新活动没有开始。

public class GetTenantList extends Activity   implements OnItemClickListener {
    private static String DB_NAME="meterapp.sqlite";
    DatabaseHelperClass db= new  DatabaseHelperClass( this,DB_NAME);    
    allTenants individualreadings;  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tenants);           
        allTenants individualreadings= new allTenants(this);
        ListView listview =(ListView)findViewById(R.id.tenants);            
        listview.setBackgroundColor(color.holo_orange_dark);
        listview.setAdapter(individualreadings);        
        listview.setOnItemClickListener(this) ;         

    }   

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



    @Override
      protected void onPause() {

        db.close();
        super.onPause();
      }
    @Override
      protected void onDestroy(){
          db.close();
            super.onDestroy();
          } 
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        db.close();
        super.onStop();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long index) {
        Intent intent = new Intent(this,EditTenants.class);
        Tenants tenant=(Tenants)individualreadings.getItem(position);
        String tenantid= Integer.toString(tenant.get_id());
        Log.e("Testing Intent Filter", tenantid);
        intent.putExtra("id", tenantid);
        startActivity(intent);

    }



}

没有回应的任何原因?

罗纳德

我有两个版本的代码建议

版本1

package com.example.metermanager;

import meter.manager.helper.DatabaseHelperClass;
import meters.model.Tenants;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class GetTenantList extends Activity   {
    private static String DB_NAME="meterapp.sqlite";
    DatabaseHelperClass db= new  DatabaseHelperClass( this,DB_NAME);    
    allTenants individualreadings;  
    ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tenants);           
         final allTenants individualreadings= new allTenants(this);
        listview =(ListView)findViewById(R.id.tenants);     
        listview.setAdapter(individualreadings);        
        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
            {
                 //whatever code you wish to invoke, in this case
                Intent intent = new Intent(getApplicationContext(),EditTenants.class);
                Tenants tenant=(Tenants)individualreadings.getItem(position);
                String tenantid= Integer.toString(tenant.get_id());
                Log.e("Testing Intent Filter", tenantid);
                intent.putExtra("id", tenantid);
                startActivity(intent);
            }
       });


            }   

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



    @Override
      protected void onPause() {

        db.close();
        super.onPause();
      }
    @Override
      protected void onDestroy(){
          db.close();
            super.onDestroy();
          } 
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        db.close();
        super.onStop();
    }










}

第2版:

我是。复制了错误的代码。没有保存!

两者都失败了。 版本2;

package com.example.metermanager;

import meter.manager.helper.DatabaseHelperClass;
import meters.model.Tenants;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class GetTenantList extends Activity  implements  OnItemClickListener {
    private static String DB_NAME="meterapp.sqlite";
    DatabaseHelperClass db= new  DatabaseHelperClass( this,DB_NAME);    
    allTenants individualreadings;  
    ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tenants);           
         final allTenants individualreadings= new allTenants(this);
        listview =(ListView)findViewById(R.id.tenants);     
        listview.setAdapter(individualreadings);        
        listview.setOnItemClickListener(this);       



            }   

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



    @Override
      protected void onPause() {

        db.close();
        super.onPause();
      }
    @Override
      protected void onDestroy(){
          db.close();
            super.onDestroy();
          } 
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        db.close();
        super.onStop();
    }


     @Override
     public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
     {
          //whatever code you wish to invoke, in this case
         Intent intent = new Intent(getApplicationContext(),EditTenants.class);
         Tenants tenant=(Tenants)individualreadings.getItem(position);
         String tenantid= Integer.toString(tenant.get_id());
         Log.e("Testing Intent Filter", tenantid);
         intent.putExtra("id", tenantid);
         startActivity(intent);
     }







}

以下是allTenants类的代码。

package com.example.metermanager;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import meter.manager.helper.DatabaseHelperClass;
import meters.model.Tenants;
import meters.model.VMeterReadings;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class allTenants extends BaseAdapter {

    @SuppressWarnings("unused")
    private Context context;
    DatabaseHelperClass db;
    private static String DB_NAME="meterapp.sqlite";    
    private List<Tenants> readings =new ArrayList<Tenants>();
    DecimalFormat df = new DecimalFormat("#,###,###,###");
    SimpleDateFormat fm =new SimpleDateFormat("dd-MM-yyyy",Locale.UK);
public allTenants(Context context1) {       
        this.context=context1;
        DatabaseHelperClass db= new  DatabaseHelperClass( context1,DB_NAME);
        readings=db.GetAllTenants();
        //close it
        db.close();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return readings.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return  getItem(index);
    }

    @Override
    public long getItemId(int index) {
        // TODO Auto-generated method stub
        return index;
    }


    @Override
    public View getView(int arg0, View view, ViewGroup parent) {
        try{        
        if (view ==null){
            LayoutInflater inflater=
                    LayoutInflater.from(parent.getContext());
                  view =inflater.inflate(R.layout.tenants_listview,parent,false);

        }
        Tenants reading =readings.get(arg0);        
        TextView tenantTextView =(TextView)
                view.findViewById(R.id.textView9);

        tenantTextView.setText(reading.getFirstName());

        TextView surnameTextView =(TextView)
                view.findViewById(R.id.textView10);     
        surnameTextView .setText(reading.getSurName());
        TextView othernamesTextView =(TextView)
                view.findViewById(R.id.textView11);     
        othernamesTextView .setText(reading.getOtherNames());

         TextView mobile1 =(TextView)
                    view.findViewById(R.id.textView12);     
         mobile1.setText(reading.getMobile1());

         TextView mobile2TextView =(TextView)
                    view.findViewById(R.id.textView13);     
         mobile2TextView.setText(reading.getMobile2()); 

             TextView dateaddedTextView =(TextView)
                        view.findViewById(R.id.textView14);     
             dateaddedTextView.setText(fm.format((reading.getDateAdded())));    

                 CheckBox inactiveCheckbox =(CheckBox)
                            view.findViewById(R.id.chkinactive);        
                 inactiveCheckbox.setChecked(reading.getInActive());
                     TextView inactivedateTextView =(TextView)
                                view.findViewById(R.id.textView16);     
                     inactivedateTextView.setText(fm.format(reading.getDateInActive()));

                   TextView tenantid =(TextView)
                            view.findViewById(R.id.textView17);
                   String test= Integer.toString(reading.get_id());
                   tenantid.setText(test);


        }catch(Exception e)
        {
            Log.e("Error loading data in All Tenants listbox",e.toString());
        }
        return view;
    }

}

4 个答案:

答案 0 :(得分:0)

您仅使用this作为上下文....将其更改为GetTenantList.this中的onItemClick。此外,您可以将intent参数更改为:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long index) {
    Intent intent = new Intent(GetTenantList.this,EditTenants.class);
    Tenants tenant=(Tenants)individualreadings.getItem(position);
    String tenantid= Integer.toString(tenant.get_id());
    Log.e("Testing Intent Filter", tenantid);
    Bundle params = new Bundle();
    params.putString("id", tenantid); 
    intent.putExtras(params); 
    startActivity(intent);

}

答案 1 :(得分:0)

像这样检查:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(GetTenantList.this,EditTenants.class);
    Tenants tenant=(Tenants)individualreadings.getItem(position);
    String tenantid= Integer.toString(tenant.get_id());
    Log.e("Testing Intent Filter", tenantid);
    intent.putExtra("id", tenantid);
    startActivity(intent);

}

答案 2 :(得分:0)

我认为您可能误读了setOnItemClickListener()文档/示例。尝试这样做:

listview.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
        {
             //whatever code you wish to invoke, in this case
            Intent intent = new Intent(getApplicationContext(),EditTenants.class);
            Tenants tenant=(Tenants)individualreadings.getItem(position);
            String tenantid= Integer.toString(tenant.get_id());
            Log.e("Testing Intent Filter", tenantid);
            intent.putExtra("id", tenantid);
            startActivity(intent);
        }
   }

不要忘记正确导入监听器!

答案 3 :(得分:0)

尝试使用ListView作为类的成员变量。