Change background of item in Adapter - android

I want to change the background of a selected list item in my adapter.
The list adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Card card = getItem(position);
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.card_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.select.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.md_grey_600));
}
});
return convertView;
}
private class ViewHolder {
Button select;
RelativeLayout cardLayout;
public ViewHolder(View view) {
select = (Button) view.findViewById(R.id.carditem_btn_upvote);
cardLayout = (RelativeLayout) view.findViewById(R.id.cardlist_item);
}
}
The problem is that when I select for ex. the first item of the list, the 7th item is also selected. I have noticed that there is a kind of a patron. When I select the second item, the 8th item is also selected and so on.

Let' say you have 10 item in list and initially you will store all your item color as white .
HashMap<Integer,Integer> mhashColorselected=new HashMap();
HashMap<Integer,Integer> mHashBtnVisibility=new HashMap();
for(i=0;i<10;i++){
//Put Default Color of your All list item will be here
mhashColorselected.put(i,R.color.white)
mHashBtnVisibility.put(i,View.Invisible);
}
Now in your adapter getView();
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Card card = getItem(position);
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.card_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.select.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.md_grey_600));
//Here we are storing Selected item By User
mhashColorselected.put(position,R.color.md_grey_600);
mHashBtnVisibility.put(i,View.Visible);
notifitydataSetChange();
}
});
//Here it's will take fault value or else it will set color as selected item
viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, mhashColorselected.getValue(position)));
if(View.VISIBLE==mHashBtnVisibility.get(position)){
viewHolder.select.setVisibility(View.VISIBLE)
}else{
viewHolder.select.setVisibility(View.INVISIBLE)
}
return convertView;
}

Related

Multichoicemode listener not working when onClicklistener added to listitem's child view

I have added multichoice mode listener to my code:
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setMultiChoiceModeListener(this);
and my listview adpater getview code is:
public View getView(int position, View convertView, #NonNull ViewGroup parent)
{
final CustomObject customObject = arrayList.get(position);
final ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_pop_up2, parent, false);
holder.txtCompanyName = (TextView) convertView.findViewById(R.id.txtCompanyName);
holder.txtProductName = (TextView) convertView.findViewById(R.id.txtProductName);
holder.imgBanner = (ImageView) convertView.findViewById(R.id.imgBanner);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtCompanyName.setText(customObject.getCompanyName());
holder.txtProductName.setText(customObject.getProductName());
Glide.with(context).load(customObject.getImageUrl()).into(holder.imgBanner);
holder.imgBanner.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(context, SecondActivity.class);
context.startActivity(intent);
}
});
return convertView;
}
now my problem is onClick of ImageView affect the multimodeListener of listview. beacuse of onClickListener on long press of imagr for multimode listener not working.

Android: Un-select all CheckedTextView except present one

i'm selecting the ListView items by using the bellow code (This is based on CheckedTextView)
lvReport.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
rlAdapterReport = (RelativeLayout) view.findViewById(R.id.rl_lv_report_user);
tvAdapterReportOption = (CheckedTextView) view.findViewById(R.id.tv_lv_report_user_title);
if (tvAdapterReportOption.isChecked()) {
//value = "un-Checked";
tvAdapterReportOption.setCheckMarkDrawable(0);
tvAdapterReportOption.setTextColor(Color.parseColor("#000000"));
rlAdapterReport.setBackgroundColor(Color.parseColor("#FFFFFF"));
tvAdapterReportOption.setChecked(false);
} else {
//value = "Checked";
Drawable dr = getResources().getDrawable(R.drawable.report_tick_icon);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
Drawable finalDrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));
tvAdapterReportOption.setCheckMarkDrawable(finalDrawable);
tvAdapterReportOption.setTextColor(Color.parseColor("#04CFE7"));
rlAdapterReport.setBackgroundColor(Color.parseColor("#D5D5D5"));
tvAdapterReportOption.setChecked(true);
}
}
});
By using above code i'm able to select all items (For understanding see the attached image), but i want to select the single item at a time. So, i want to deselect the all items if already selected
Edit
My adapter (ArrayAdapter) getView code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final ReportUserItems rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = mInflater.inflate(R.layout.lv_report_user_adapter, null);
holder = new ViewHolder();
holder.rlReport = (RelativeLayout) convertView.findViewById(R.id.rl_lv_report_user);
holder.tvReportOption = (CheckedTextView) convertView.findViewById(R.id.tv_lv_report_user_title);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
In your adapter class store the selected row position and update and refresh whenever user clicks on other item.
Your Adpater class changes.
int selectedPosition = -1; //to store only selected item position
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final ReportUserItems rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
//your code
} else {
holder = (ViewHolder) convertView.getTag();
}
//your other code
if (selectedPosition != -1)
if (selectedPosition == position) {
//your drawable code
holder.tvReportOption.setCheckMarkDrawable(finalDrawable);
holder.tvReportOption.setChecked(true);
//your other stuff : changing color etc
} else {
holder.tvReportOption.setCheckMarkDrawable(0);
holder.tvReportOption.setChecked(false);
//your other stuff : changing color etc
}
return convertView;
}
//create this method in Adapter class
public void setSelected(int pos) {
selectedPosition = pos; //change selected item position
notifyDataSetChanged(); //refresh views
}
Activity code changes
lvReport.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
adapter.setSelected(position);
}
});

Not able to remove item at particular index from array adapter

i am not able to remove item from arraylist at particular index.
When i am trying to remove item then it is remove from bottom only.
private ArrayList<myproduct> values;
public View getView(final int position, final View convertView,
ViewGroup parent) {
final ViewHolder holder;
View v = convertView;
if (v == null) {
final LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_favourite, parent, false);
holder = new ViewHolder();
holder.btnaddcollection = (Button) v
.findViewById(R.id.btnaddcollection);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
homeproduct mhomeproduct = values.get(position);
holder.btnaddcollection.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
values.remove(position);
}
});
return v;
}
static class ViewHolder {
public Button btnaddcollection;
}
From above code when i am trying to remove then it is start from bottom to remove .
How can i start to remove item from top with particular position.?
Call notifyDataSetChanged() after you remove the item.
This 'will say' to adapter: "Eh!! data was changed!"

Delete a row from listview xamarin

I have a activity in which there are some buttons and a listview.each rows of the listview has a delete button which i'm intend to delete the row whenever the user click the delete button.
i'm using a custom adapter so here is my getview method:
public override View GetView(int position, View convertView, ViewGroup parent)
{
convertView = menuActivity.LayoutInflater.Inflate(Resource.Layout.OrderItemLayout, null);
ImageButton btn = convertView.FindViewById<ImageButton>(Resource.Id.btn_del);
btn.Tag = position;
btn.Click += btn_Click;
return convertView;
}
void btn_Click(object sender, EventArgs e)
{
_lstOrder.RemoveAt((int)((sender as ImageButton)).Tag);
NotifyDataSetChanged();
}
but the problem is that when i click the delete button the listview doesn't refresh so the row that was deleted is still there but it will gone if i go back and then come to the activity again.
how can I solve this problem so after i delete a row the listview refresh?
thank you
Creating a common arraylist
ArrayList<GeterSetterClass> comn_arylist = new ArrayList<GeterSetterClass>();
Customized addapter setting
custdet_adapter adapter = new custdet_adapter(this,comn_arylist);
listview.setAdapter(adapter);
listview.invalidateViews();
Customized Adapter class
public static class custdet_adapter extends BaseAdapter {
private ArrayList<GeterSetterClass> cust_arrayList;
private LayoutInflater l_Inflater;
public custdet_adapter(Context context,
ArrayList<GeterSetterClass> list_details) {
cust_arrayList = list_details;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return cust_arrayList.size();
}
public Object getItem(int position) {
return cust_arrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.layoutname_struct,null);
holder = new ViewHolder();
holder.txt_custname = (TextView) convertView
.findViewById(R.id.textView1);
holder.txt_mobno = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_custname.setText(cust_arrayList.get(position).getName());
holder.txt_mobno.setText(cust_arrayList.get(position).getunitcd());
holder.txt_custname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
comn_arylist.remove(position);
listview.invalidateViews();
}
});
return convertView;
}
class ViewHolder {
TextView txt_custname;
TextView txt_mobno;
}
}

Get the position after click on button in a list view

I've that in my Adapter :
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView = myInflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.text01 = (TextView) convertView.findViewById(R.id.Text01);
((Button)convertView.findViewById(R.id.ListButonPlus)).setOnClickListener(this);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
And that :
public void onClick(View v)
{
Toast.makeText(v.getContext(), "pouette",1000).show();
}
And I try t get the position of the item who contains my button.
How Can I pass the position variable present in the getView method to my onClick Method?
I will have several button In my View (Item view)
Thank you
Ok I found, I pass my position to the ViewHolder
and I get the ViewHolder from the tag of the view or parrent view by a recursive function.
Is there any different method?
public static class ViewHolder
{
private TextView text01;
public int position;
}
public ViewHolder getViewHolder(View v)
{
if(v.getTag() == null)
{
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
public void onClick(View v) {
ViewHolder vh = getViewHolder(v);
vh.position // Here I get position
}
I know this post is old, but this could help some others:
In the getView of my adapeter:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ContactHolder holder = null;
final ContactRow ContactRow = ContactRowList.get(position);
if(row == null)
{
...
holder.delete = (ImageButton)row.findViewById(R.id.btn_contact_delete); // my button inside the item
holder.delete.setTag(position);
...
}
This is my listener for this button
holder.delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
int position=(Integer)v.getTag();
((MainActivity)context).deleteContact(position,true); // call the delete function form the main activity
}
});
My holder
static class ContactHolder
{
...
ImageButton delete;
}

Categories

Resources