我在listview的每一行都有一个'批准'按钮。当我点击“批准”按钮时,其背景颜色应该更改,文本应从“批准”更改为“已批准”。在这里,我可以为setText()
,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;
}
编写代码,
但问题是,当我滚动时,它也会影响列表视图中的另一行。该特定行的按钮颜色和文本也会发生变化。我在过去两天试过这个问题,但没找到任何答案。请帮帮我。谢谢。
以下是适配器类的代码
{{1}}
答案 0 :(得分:0)
尝试设置适配器
String txtnew = contact.get(position).getOrderid();
答案 1 :(得分:0)
您需要为每一行设置每次批准(或不批准)数据。所以你需要一个数组来保持点击的行,如下所示:
适配器中的:
private ArrayList<Integer> selectedPos = new ArrayList<>();
<{1>} onClick:中的
contactHolder.Approve
并最终低于此selectedPos.add(position);
行检查批准状态:
contactHolder.products_details.setText(contacts.getProducts_details());
答案 2 :(得分:0)
你可以这样做。
实施此方法:
@Override
public int getItemViewType(int position) {
return position;
}
希望有所帮助
答案 3 :(得分:0)
你可以使用这个想法:
在数据库'status'中再取一个字段,默认为0。
当用户单击“批准”按钮时,将状态更改为1,并根据该更新,列表视图中的项目类似于
if(status == 1)然后查看颜色,启用/禁用等。