Take the 2-minute tour ×

SO basically I need a little help or some suggestions with problem that I have. I'm populating my list view from database and I need to check when I'm creating my listview if the item's id on position is the same as the id from another table in my database. If it is, you can click that item, if not I want it to disable it, but all the information that I found about how to do that..I can't really understand how to do that.

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//For more information look at the bottom of file.

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private ArrayList<String> name;
    private ArrayList<String> info;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;
    private Bitmap b;

    public LazyAdapter(Activity a, Bitmap d, ArrayList<String> names, ArrayList<String> information) {
        activity = a;
        info = information;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());

    public int getCount() {
        return name.size();

    public Object getItem(int position) {
        return position;

    public long getItemId(int position) {
        return position;

    public static class ViewHolder{
        public TextView name,info;
        public ImageView image;

        View vi=convertView;
        ViewHolder holder;
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            Log.v("Position","Position : "+position);

            //Here I must do a black magic and get the images if user had 'em.


            //imageLoader.DisplayImage(data[position], activity, holder.image);

            // Black magic over.
        return vi;

Any ideas or suggestions how to do that?

share|improve this question
what are you using currently? Have you defined Custom adapter for the listview? –  Paresh Mayani Oct 15 '11 at 16:00
yeah, actually I'm using custom adapter. let me put the code –  Android-Droid Oct 15 '11 at 16:04
you could do this, in the getview method, put in an if condition and get the item id, simultaneously i believe you have another collection where you have the list of ids against which to check, or else make a select query with the id, if the count is 1 that means its present, else not, if present, set the setEnabled method of the view to true else false. (do this on the item click of the listview) –  Rat-a-tat-a-tat Ratatouille Oct 11 '13 at 8:46

3 Answers 3

In the adapter there is a method name isEnabled which you can overide. This is called for each row like getview. The onclicklistener will only fire if this function returns true. So try doing that in your customm adapter.

public boolean isEnabled(int position) {
        return false;
    return true;
share|improve this answer
Or, more simply: return !(YOUR_CONDITION);, where you can use the inverse operations (!= vs == to simplify further). –  reece Nov 16 '12 at 18:47
Nice answer. Am wondering why this wasn't accepted? –  Abx Oct 23 '13 at 5:51
Thank you a million times! –  Olayinka Mar 11 at 12:30
This is exactly what I was looking for! I'm surprised none of the tutorials I read mentioned it. –  Aelexe May 29 at 9:30

Not sure what you're doing there, but what you want to do is define your class as you would normally by extending Activity ( or ListActivity). Then making your adapter class a nested class within the Activity class by extending BaseAdapter.

Once you've done that, you'll write your getView() method within your adapter class. Assign the OnClickListener to the view there depending on the condition you've mentioned.

share|improve this answer

When you pass a list of objects to BaseAdpater, add a field in this list's object called isEnabled and set it to true/false as needed, then override isEnabled method of BaseAdapter like this

public boolean isEnabled(int position) {

    return info.get(position).isEnabled;

where info is your list of objects

share|improve this answer

Your Answer


