Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android disable listview items

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;
        b=d;
        name=names;
        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;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.name=(TextView)vi.findViewById(R.id.name);
            holder.info=(TextView)vi.findViewById(R.id.info);
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
            Log.v("Position","Position : "+position);
        }
        else
            holder=(ViewHolder)vi.getTag();
            holder.name.setText(name.get(position));
            holder.info.setText(info.get(info.size()-1));

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

            holder.image.setImageBitmap(b);

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

            // Black magic over.
        return vi;
    }
}

Any ideas or suggestions how to do that?

like image 775
Android-Droid Avatar asked Oct 15 '11 15:10

Android-Droid


People also ask

How to disable item from ListView?

Then you should save which item is selected and then change view. enable=false. and when you are loading or refreshing listview check one condition that if view item is equal to clicked item then disable it.

How do I disable list view?

The general approach is to disable all the items instead of the whole view. Essentially: Inherit from ArrayAdapter (or any other adapter) Override isEnabled(int position) to return false.


3 Answers

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.

@Override
public boolean isEnabled(int position) {
    if(YOUR CONDTITION){
        return false;
    }
    return true;
}
like image 193
blessanm86 Avatar answered Nov 04 '22 16:11

blessanm86


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

@Override
public boolean isEnabled(int position) {

    return info.get(position).isEnabled;
}

where info is your list of objects

like image 21
Kammaar Avatar answered Nov 04 '22 16:11

Kammaar


For example if you want to disabled the positions 0 to 5

@Override
public boolean isEnabled(int position){
    if(position >= 0 && position < 6)
        return false;
    else
        return true;
}

@Override
public boolean areAllItemsEnabled(){
    return true;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

          //CODE OF ADAPTER
}
like image 2
Alex Zaraos Avatar answered Nov 04 '22 15:11

Alex Zaraos