我有一個'批准'按鈕在每一行的列表視圖。當我點擊該「批准」按鈕時,其背景顏色應該改變,文本應該從「批准」更改爲「批准」。在這裏,我能夠爲此編寫代碼,如setBackgroundColor()
,setText()
, 但是問題在於,當我滾動時,它也影響了listview中的另一行。該特定行的按鈕顏色和文字也被更改。我過去兩天曾試過這個問題,但沒有找到任何答案。請幫助我。提前感謝。Android:列表視圖按鈕單擊事件在一行中影響其他行
這裏是一個適配器類
public class OrderApprovalAdapter extends ArrayAdapter
{
SessionManager session;
String Wholesaler_name;
String mobile_no,Order_id;
List list1 = new ArrayList();
Context context;
OrderApprovalDetails contacts;
public OrderApprovalAdapter(@NonNull Context context, @NonNull int Resource)
{
super(context, Resource);
this.context = context;
}
@Override
public void add(@Nullable Object object)
{
super.add(object);
list1.add(object);
}
@Override
public int getCount()
{
return list1.size();
}
@Override
public Object getItem(int position)
{
return list1.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final OrderApprovalAdapter.ContactHolder contactHolder;
session = new SessionManager(context);
HashMap<String, String> user = session.getUserDetails();
Wholesaler_name = user.get(SessionManager.KEY_NAME);
View row;
row = convertView;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.order_approval_format, parent, false);
contactHolder = new ContactHolder();
contactHolder.date = (TextView) row.findViewById(R.id.invoice_date);
contactHolder.orderid = (TextView) row.findViewById(R.id.invoice_no);
contactHolder.shopname = (TextView) row.findViewById(R.id.shop_name);
contactHolder.ownername = (TextView) row.findViewById(R.id.owner_name);
contactHolder.mobile=(TextView) row.findViewById(R.id.mobile_noo);
contactHolder.location=(TextView)row.findViewById(R.id.location);
contactHolder.itemscount=(TextView)row.findViewById(R.id.items);
contactHolder.amount=(TextView)row.findViewById(R.id.total);
contactHolder.products_details=(TextView)row.findViewById(R.id.products);
contactHolder.Approve=(Button) row.findViewById(R.id.approve_btn);
row.setTag(contactHolder);
} else
{
contactHolder = (ContactHolder) row.getTag();
}
final OrderApprovalDetails contacts = (OrderApprovalDetails) this.getItem(position);
contactHolder.date.setText(contacts.getDate());
contactHolder.orderid.setText(contacts.getOrderid());
contactHolder.shopname.setText(contacts.getShopname());
contactHolder.ownername.setText(contacts.getOwnername());
contactHolder.mobile.setText(contacts.getMobile());
contactHolder.location.setText(contacts.getLocation());
contactHolder.itemscount.setText(contacts.getItemscount());
contactHolder.amount.setText(contacts.getAmount());
contactHolder.products_details.setText(contacts.getProducts_details());
contactHolder.Approve.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(context, "Approved Succesfully", Toast.LENGTH_SHORT).show();
contactHolder.Approve.setEnabled(false);
contactHolder.Approve.setText("Approved");
contactHolder.Approve.setBackgroundColor(Color.BLUE);
notifyDataSetChanged();
}
});
final View finalRow = row;
return row;
}
static class ContactHolder
{
TextView date,orderid,shopname,ownername,mobile,location,itemscount,amount,products_details;
Button Approve,Decline;
}
嘗試使用列表視圖的位置.. –