subair_a
subair_a

Reputation: 1088

Android listview handling click event

I have a custom list view implemented with Baseadapter. My dataset includes a string and a flag. I need to control the clickable property of each row in that list view based on this flag.. Any help will be greatly appreciated..

Upvotes: 0

Views: 4234

Answers (3)

Varun
Varun

Reputation: 33983

I understand that your dataset is collection of data objects that contains a string and a flag. In that case you can override the isEnabled(int position) in your base adapter like this

public boolean isEnabled(int position){
    return myDataSet.get(position).getFlag();// returning true here will make that item clickable
}

Note that i am referring to my data object of the corresponding position.

Upvotes: 1

Balaji.K
Balaji.K

Reputation: 4829

If you want the listview with custom properties try to implement your own customAdapter which extends the BaseAdapter

public class CustomListAdapter extends BaseAdapter {
private  ArrayList<Generics> allElementDetails;

private LayoutInflater mInflater;

public CustomListAdapter(Context context, ArrayList<Generics> results) {
    allElementDetails = results;
    mInflater = LayoutInflater.from(context);

}
public int getCount() {
    return allElementDetails.size();        
}
public Object getItem(int position) {
    return allElementDetails.get(position);
}
public long getItemId(int position) {
    return position;
}
public View getView(int position, View convertView, ViewGroup parent) 
{
    convertView = mInflater.inflate(R.layout.filedialog, null);
    ImageView imageview = (ImageView) convertView.findViewById(R.id.imageview);
    TextView textview = (TextView) convertView.findViewById(R.id.textview);
    if(flag==true)
             convertview.setClickable(true) 
     else
             convertview.setClickable(false); 
    return convertView;
}     

}

now use the listview.setOnItemClickListener to apply the actions i.e functionality that will perform when the clickable view clicked.

I think this may help u....

Upvotes: 0

Dinash
Dinash

Reputation: 3047

hi subi you could use this below code for setting the clicklistener....

 lv.setAdapter(new ArrayAdapter(ClassName.this)); 
 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
 { 
      @Override 
      public void onItemClick(AdapterView arg0, View arg1,int arg2, long arg3) 
      { 
      } 
 });

where the arrayAdapter is your customer class extending baseadapter... Hope this helps...

Upvotes: 4

Related Questions