I am using a custom Listview with Custom views as list elements, each list item is a separate Custom view. The problem which I am facing is , click event on button in 1st item of ListView is not getting fired when we click on it. After clicking on it if we click somewhere else in the screen the click event get fires. I am not able to find the solutions for it and I am struggling with it. Any help will be highly appreciated.
Updated with code: Here is the getview method
public override View GetView(int position, View convertView, ViewGroup parent)
{
if (position == 0)
{
convertView = this.context.LayoutInflater.Inflate(Resource.Layout.home_hero_container, null);
this.heroSection = convertView.FindViewById<FrameLayout>(Resource.Id.heroContainer);
this.setHeroCard();
}
else
{
convertView = (View)GetItem(position - 1);
}
return convertView;
}
GetItem returns CustomView. 1st item will be the hero layout, after that all the Customviews will be added to Convertview. Click event is not working on the 1st item after hero.
Update with my answer:
Instead of assigning Customview directly on to Convertview I inflated a FrameLayout and add Customview to FrameLayout. Now I don't have click issue.
try this it will work
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
holder.chkselected.setOnCheckChangeListener(new CheckchangeListener() );
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
class CheckchangeListener implements OnCheckedChangeListener {
public CheckchangeListener() {
// TODO Auto-generated constructor stub
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
// do your work here
} else {
// do your work here
}
}
}
}
You can try to set onClick event in your Custom adapter and if you have time then check this tutorial for reference - http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html
Related
I have a custom listview with each row contaning a checkbox and text. now what i want that if any one checkbox of listview row is checked so others checkbox in other row if checked .it will be delected automatically.(i.e only one checkbox should be selected one at a time).how should i do that.
So far what i have done is as follows:
public class CustomAdapter extends BaseAdapter{
Context context;
List<String> items;
boolean array[];
public CustomAdapter(Context context, List<String> items) {
super();
this.context = context;
this.items = items;
array =new boolean[items.size()];
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v=convertView;
final int pos=position;
if(v==null)
{
v=LayoutInflater.from(context).inflate(R.layout.list,null);
}
TextView txt1=(TextView) v.findViewById(R.id.textView1);
final CheckBox chkbox=(CheckBox) v.findViewById(R.id.checkBox1);
txt1.setText(items.get(position));
int selectedindexitem=0;
chkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkbox.isChecked())
{
array[pos]=true;
}else{
array[pos]=false;
}
}
});
chkbox.setChecked(array[pos]);
return v;
}
}
In this code i can select multiple checkbox at a time but i need only one checkbox should be checked one at a time.
You need to keep track of selected item and code accordingly.
public class CustomAdapter extends BaseAdapter{
Integer selected_position = -1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Your Code
chkbox.setChecked(position==selected_position);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
selected_position = position;
}
else{
selected_position = -1;
}
notifyDataSetChanged();
}
});
return convertView;
}
}
Try to change all item boolean value false exclude selected item after notify adapter and also implement ViewHolder design pattern for ListView performance :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list,null);
holder.txt1 = (TextView) convertView.findViewById(R.id.textView1);
holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txt1.setText(items.get(position));
holder.chkbox.setChecked(array[position]);
holder.chkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i=0;i<array.length;i++){
if(i==position){
array[i]=true;
}else{
array[i]=false;
}
}
notifyDataSetChanged();
}
});
return convertView;
}
class ViewHolder{
TextView txt1;
CheckBox chkbox;
}
Maintain a variable:
int checked = -1; //say
Whenever you try to check a checkbox you check this variable, if it is -1 then check the checkbox and save the position of the list item in checked variable, when you try to check other checkbox again, if the variable ain't equal to -1 you first uncheck the checkbox at the position stored in checked variable, and then check the checkbox at current position and save the current position in checked variable
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v=convertView;
final int pos=position;
if(v==null)
{
v=LayoutInflater.from(context).inflate(R.layout.list,null);
}
TextView txt1=(TextView) v.findViewById(R.id.textView1);
final CheckBox chkbox=(CheckBox) v.findViewById(R.id.checkBox1);
txt1.setText(items.get(position));
int selectedindexitem=0;
if(position==selected_position)
{
chkbox.setChecked(true);
}
else
{
chkbox.setChecked(false);
}
chkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkbox.isChecked())
{
array[pos]=true;
selected_position = pos;
}else{
array[pos]=false;
}
}
});
chkbox.setChecked(array[pos]);
MainActivity.lv.setAdapter(new CustomAdapter(context,MainActivity.listitems));
return v;
}
Easy to maintain your selection. Get your selection from the model.
class PropertyModel{
String VAL= "SOME VALUE"
boolean selected = false;
public String getVAL() {return VAL;}
public void setVAL(String VAL) {this.VAL = VAL;}
public boolean isSelected() {return selected;}
public void setSelected(boolean selected) {this.selected = selected;}
}
Prepare your view like this:
public class CheckBoxAdptr extends BaseAdapter{
/*
..........
*/
public View getView(int position, View view, ViewGroup parent) {
PropertyModel items = list.get(position);
View row;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_checkbox, null);
}
else{row = view;}
TextView T1 = (TextView) row.findViewById(R.id.list_item_1);
CheckBox checkBox = (CheckBox) row.findViewById(R.id.list_item_check_box);
T1.setText(""+items.getVAL());
if(items.isSelected()){
checkBox.setChecked(true);
}
else {checkBox.setChecked(false);}
return row;
}
}
Now in your activity or fragment set the adapter and do something like this:
final ArrayList<PropertyModel> list = getList();
CheckBoxAdptr adpt = new CheckBoxAdptr(getActivity(), list);
listview.setAdapter(adpt);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for(int i = 0; i<list.size(); i++){
if(i!=position){
list.get(i).setSelected(false);
}
else{
list.get(i).setSelected(true);
}
}
adpt.notifyDataSetChanged();
}
});
Notice that all your work is done by adpt.notifyDataSetChanged() . Hope this helps.
Working with getView (like most of the answers posted) was not working properly for me. It was not responsive enough. Sometimes it would uncheck the previous checkbox, sometimes not.
This worked perfectly for me:
First we need to store the position of all previously checked checkboxes:
Set<Integer> selectedPreviousPositions = new HashSet<>();
Then in the onItemClick listener we uncheck all checkboxes before checking the one we selected.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
boolean isChecked = ((CheckBox) view.findViewById(R.id.cbSelected)).isChecked();
if (!isChecked) {
final ListView listDevices = (ListView) findViewById(R.id.lvDevices);
for (Integer pos : selectedPreviousPositions ) {
View pView = listDevices.getChildAt(pos);
if (pView != null) { // unnecessary but we never know !
((CheckBox)pView.findViewById(R.id.checkboxID)).setChecked(false);
}
}
selectedUniquePositions.add(new Integer(position));
// Now we can check the selected checkbox
((CheckBox) view.findViewById(R.id.cbSelected)).setChecked(true);
} else {
((CheckBox) view.findViewById(R.id.cbSelected)).setChecked(false);
selectedPreviousPositions.remove(new Integer(position));
}
}
Inside the adapter, set OnClickListener() for checkBox. Whenever checkBox is clicked, store clicked checkBox position and call notifyDataSetChanged(). This will getView() method again for all views in your listview. Whenever, getView() called, you check the checkBox of previously stored postition and uncheck remaining checkboxes.
public class CustomAdapter extends ArrayAdapter<YourModelObject>
{
private int selectedCheckBoxPosition = -1;
public int getSelectedCheckBoxPosition()
{
return selectedCheckBoxPosition;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
final ViewHolder viewHolder;
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) convertView;
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
if (position == selectedCheckBoxPosition)
{
viewHolder.checkBox.setChecked(true);
Log.d(TAG, "checkbox CHECKED at pos: " + position);
}
else
{
viewHolder.checkBox.setChecked(false);
Log.d(TAG, "checkbox UNCHECKED at pos: " + position);
}
//viewHolder.checkBox.setText("sample text");
viewHolder.checkBox.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
selectedCheckBoxPosition = position;
Log.d(TAG, "onClick() - set selectedCheckBoxPosition = " + position);
notifyDataSetChanged();
}
});
return convertView;
}
private class ViewHolder
{
CheckBox checkBox;
}
}
My gridview custom item is a image button.I want to change image button image when the image button is clicked.You can see below my custom adapter class getView method.But image button image does not change.
ImageButton btn1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.myItem, null);
btn1 = (ImageButton) view.findViewById(R.id.btn1);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn1.setImageResource(R.id.image1);
}
});
return view;
}
Try this way,hope this will help you to solve your problem.
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,Integer>> imagesList;
public CustomGridAdapter(Context context,ArrayList<HashMap<String,Integer>> imagesLis) {
this.context =context;
this.imagesList =imagesLis;
}
#Override
public int getCount() {
return imagesList.size();
}
#Override
public Object getItem(int position) {
return imagesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.myItem, null);
holder.btn1 = (TextView) convertView.findViewById(R.id.btn1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btn1.setImageResource(imagesList.get(position).get("normalImage"));
holder.btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ImageButton)v).setImageResource(imagesList.get(position).get("selectedImage"));
}
});
return convertView;
}
static class ViewHolder {
ImageButton btn1;
}
}
In listview, i am looking to create an action(eg. COPY) that will duplicate a selected view (with a new ID) in a list of objects. The view must be created next to the selected view. Any idea?
Here is my adapter
public class Test extends BaseAdapter {
Context context;
ArrayList<ListItemVO> itemList;
List<String> editList;
static class ViewHolder {
public TextView text;
public EditText editText;
public ViewSwitcher vs;
}
public Test(Context context, ArrayList<ListItemVO> arr) {
this.context = context;
this.itemList = arr;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.listview_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView) rowView.findViewById(R.id.textView111);
holder.editText = (EditText) rowView.findViewById(R.id.editText111);
holder.vs = (ViewSwitcher) rowView.findViewById(R.id.viewSwitcher111);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.text.setText(itemList.get(position).getName());
holder.editText.setText(itemList.get(position).getName());
holder.editText.setTag(R.id.TAG_POSITION_ID, position);
holder.editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int position = Integer.parseInt(v.getTag(R.id.TAG_POSITION_ID).toString());
final EditText editText = (EditText) v;
String val = editText.getText().toString(); // you have the value here
if (val.compareTo("") != 0) {
itemList.get(position).setName(val);
}
}
}
});
holder.vs.setDisplayedChild(0);
if(itemList.get(position).isEdit()) {
holder.vs.setDisplayedChild(1);
}
return rowView;
}
}
implement adapters onItemClick() for the adapter and update your list of items
For delete, remove that item from list.
For add, add an item at that position.
For edit, edit list.
in the end call adapter.notifyDataSetChanged()
I know that this question has been asked over and over again but still I've not been a able to find a suggestion that really helps me. The checkbox is unchecked whenever the list is scrolled down. Yes I'm using a boolean array to store the values but this still doesn't fix the problem. Here is my code. Please suggest a solution for this. Thank you.
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
final boolean[] itemChecked=new boolean[30];
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null)
{
convertView = inflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.description_text);
holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
itemChecked[position] = isChecked;
if(itemChecked[position])
{
holder.cb.setChecked(true);
}
else
{
holder.cb.setChecked(false);
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.cb.setChecked(itemChecked[position]);
holder.txtViewDescription.setFocusable(false);
holder.txtViewTitle.setFocusable(false);
return convertView;
}
}
getView() is called whenever a previously invisible list item needs to be drawn. Since you recreate itemChecked[] each time this method is called you will have the new checkbox unchecked and a different Array for each resulting View. (final in Java does not make that field unique like in C)
Simplest way to solve that is to make itemChecked a classmember and set / restore checkbox state based on that one.
public class MyListAdapter extends ArrayAdapter<Object> {
private final boolean[] mCheckedState;
private final Context mContext;
public MyListAdapter(Context context, int resource, int textViewResourceId, List<Object> objects) {
super(context, resource, textViewResourceId, objects);
mCheckedState = new boolean[objects.size()];
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// simplified to just a Checkbox
// ViewHolder and OnCheckedChangeListener stuff left out
CheckBox result = (CheckBox)convertView;
if (result == null) {
result = new CheckBox(mContext);
}
result.setChecked(mCheckedState[position]);
return result;
}
}
Here is an example. Read the comments in the getView(...)
of the adapter provided below.
class TaskObject {
private int pid;
private String processName;
private boolean toKill;
///getters/setters
public boolean isToKill() {
return toKill;
}
public void setToKill(boolean toKill) {
this.toKill = toKill;
}
................................
}
class TaskListAdapter extends BaseAdapter {
private static final String TAG = "adapter";
ArrayList<TaskObject> list;
Context context;
public TaskListAdapter(Context context) {
Log.d(TAG, "created new task list adapter");
this.context = context;
if (list == null) {
list = new ArrayList<TaskObject>();
}
}
public void addTask(TaskObject taskObject) {
list.add(taskObject);
}
public void clearTasks() {
list.clear();
Log.d(TAG, "list size:" + list.size());
this.notifyDataSetChanged();
}
public int getCount() {
return list.size();
}
public TaskObject getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout rl = new RelativeLayout(context);
TextView textPid = new TextView(context);
textPid.setText(Integer.toString(getItem(position).getPid()));
TextView textName = new TextView(context);
textName.setText(getItem(position).getProcessName());
/////Here is your and it will set back your checked item after scroll
CheckBox chckKill = new CheckBox(context);
if(getItem(position).isToKill())
{
chckKill.setChecked(true);
}
////////////////////////////////////////////////////////////////////
chckKill.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
getItem(position).setToKill(true);
}
}
});
chckKill.setTag(getItem(position).getPid());
/////////NOT LAYOUTED USE LAYOUT
rl.addView(chckKill);
rl.addView(textName);
rl.addView(textPid);
return rl;
}
hope it helps abit.
In mycase, I solved this issue as follows :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
ImageView thumbnail = null;
CheckBox checkBox = null;
Content rowData = GridViewActivity.contents.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.getContentTitle();
title.setText(rowData.getTitle());
thumbnail = holder.getThumbnail();
thumbnail.setImageResource(rowData.getIcon());
checkBox = holder.getCheckBox();
checkBox.setTag(position);
checkBox.setChecked(rowData.isCheckBox());
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
GridViewActivity.notifyCheckChanges(getPosition,
buttonView.isChecked());
}
});
return convertView;
}
You should place setChecked() after setOnCheckedChangeListener().
How to change the background image for list items, i am able to change only 1 item background at a time.
If there are 6 items on the list and if click on 3 items those 3 items background images should be changed, how it is possible
Below is my code
public class Places extends Activity {
private ListView listView;
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
} else {
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
}
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Try this,it should work logically.(I didn't try it,btw! :P)
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
...
private class EfficientAdapter extends BaseAdapter {
public static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
for(int i=0;i<5;i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Every time you click on listview item whole listview is getting refreshed.
So if you want to show previously selected items also then need to keep record of all the item selected. and when listview refreshed you need to check that this positions are previously selected or not according to that set your color.
Hope this help you
try this
android:background="#drawable/img_list_background_repeater"
if(clickWord.size()!=0)
{
for(int i = 0;i<clickWord.size();i++){
if(clickWord.get(i).equalsIgnoreCase(adListText[position])&&clickIndex.get(i)==position){
wordChk.setBackgroundResource(R.drawable.star_1);
}
}
}
Here clickWord is an arraylist of items selected. so when items get selected it will be added in this arraylist and when arraylist is refreshed i check all this using this loop.