简体   繁体   中英

Custom adaptor onClick isn't registering click

I've got the following code which when I click each row, it's not registering the onclick event and the log isn't being displayed.

I'm probably doing something stupid so any help really will be appreciated.

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


}

if you are extends ing ListActivity you should override the OnListItemClick method. It is what you need. Also, if I can give you a suggestion, the pattern in your getView should look like:

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

in order to avoid waste of memory, since the ListView as your way to recycle the View in order to get the memory usage lower

Your code works well for me.

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();

               }
           });

It will show Toast with correct position also.

Somehow it is not able to print message in log.

Hope it helps you.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM