1

我有以下代码,当我单击每一行时,它没有注册 onclick 事件并且没有显示日志。

我可能正在做一些愚蠢的事情,所以任何帮助都将不胜感激。

public void loadTable(){
     File dbfile = new File(Global.currentDBfull); 
     SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

     mItemName.clear();
     mItemID.clear();
     adapter.notifyDataSetChanged();

     String SQLStatement = "select * from assets where areaobjectid = '" + StationObjectID + "' and CriticalAsset = 'Y'";
     Log.e("SQLSTATEMENT", SQLStatement);

     String intCount = "0";

     Cursor c = db.rawQuery(SQLStatement, null);  
     if(c.getCount() != 0) {

     c.moveToFirst();
     while(!c.isAfterLast()) {

         mItemName.add(c.getString(c.getColumnIndex("Description")));
         mArrayClick.add(c.getString(c.getColumnIndex("StationObjectID")));
         mItemStatus.add(c.getString(c.getColumnIndex("ConditionID")));
         Log.e("MITEMSTATUS",c.getString(c.getColumnIndex("ConditionID")));


        c.moveToNext();

     }

     rowCount = mItemName.size();

     listView = (ListView) findViewById(R.id.lvAssets);
     adapter.notifyDataSetChanged();   
     int[] colors = {0, 0xFFFF0000, 0}; 
     listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
     listView.setDividerHeight(1);

     listView.setAdapter(adapter);


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

             Log.e("TOMTEST", "Clicked");   

           }
       });


     } else {

        Log.e("Assessment", "No Results");  


     }

     db.close();
}

class CustomAdapter extends BaseAdapter
{

    @Override
    public int getCount() {

        return mItemName.size();
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageButton btnAction;

        LayoutInflater inf=getLayoutInflater();
        View v=inf.inflate(R.layout.iconrow, arg2,false);
        ImageView ivStatus=(ImageView)v.findViewById(R.id.icon);
        TextView tv=(TextView)v.findViewById(R.id.text);
        btnAction = (ImageButton)v.findViewById(R.id.btnAction);

        final String itemID =  "Item ID";
        final String itemText =  "Item Text";
        final String itemStage = "Item Strage";

        tv.setText(mItemName.get(arg0).toString()); 

        String currentStatus = mItemStatus.get(arg0).toString();
        if(Functions.isNullOrEmpty(mItemStatus.get(arg0).toString())){
             ivStatus.setImageResource(R.drawable.ico_not_verified); 
        } else if(mItemStatus.get(arg0).toString() == "99") {
            ivStatus.setImageResource(R.drawable.ico_not_found); 
        } else {
            ivStatus.setImageResource(R.drawable.ico_found); 
        }


        return v;
    }


}
4

2 回答 2

0

如果你正在extends学习ListActivity你应该override使用的OnListItemClick方法。这是你需要的。另外,如果我能给你一个建议,你的模式getView应该是这样的:

 if(arg1==null){
   LayoutInflater inflater=getLayoutInflater();
   arg1=inflater.inflate(R.layout.row, parent, false);
 }

为了避免浪费内存,因为这ListView是您回收的方式,View以降低内存使用率

于 2012-12-12T10:20:41.077 回答
0

你的代码很适合我。

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

             @Override
             public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                 Log.v("TOMTEST", "Clicked");   
                 Toast.makeText(getApplicationContext(), "Clicked : "+position, Toast.LENGTH_LONG).show();

               }
           });

它也会显示正确位置的 Toast。

不知何故,它无法在日志中打印消息。

希望它可以帮助你。

谢谢。

于 2012-12-12T10:56:13.490 回答