Reputation: 14565
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?
Upvotes: 18
Views: 18756
Reputation: 21
In you adapter, add this.
if ((position)!="your item"){
view.setEnabled(true);
view.setClickable(true);
} else{
view.setEnabled(false);
view.setClickable(false);
}
`
Upvotes: -1
Reputation: 6553
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
}
Upvotes: 2
Reputation: 1741
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
Upvotes: 2
Reputation: 31779
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;
}
Upvotes: 69
Reputation: 14600
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.
Upvotes: 0