Android Listview 多个按钮点击
Posted
技术标签:
【中文标题】Android Listview 多个按钮点击【英文标题】:Android Listview multiple buttons click 【发布时间】:2017-02-12 16:43:59 【问题描述】:我的问题是关于列表视图的多个按钮单击。例如我的列表视图包含 3 行,每行包含 3 个按钮。当我点击第一行的第一个按钮时,它会改变最后一行第一个按钮的颜色
please check image you will know the problem
我尝试使用按钮标签、视图持有者来解决它,尝试将位置分配给按钮但仍然没有发生任何事情
public class ViewOrderAdapter extends BaseAdapter
@SuppressWarnings("unused")
private LayoutInflater mInflater;
Context mcontext;
ArrayList<ViewOrder> view_order_array = null;
SimpleDateFormat sdf;
String currentTime;
String ord_id;
public Button accept_btn, pickup_btn, delieverd_btn, order_view, rest_address, cust_address;
GPSTracker gps;
String latitude, longitude;
private static int ipos;
boolean[] buttonState;
AlertDialog alert;
View view;
OrdersFragment ord_frag;
ViewOrderAdapter view_order_adapter1;
public static final String PREFS_NAME = "MyApp_Settings";
SharedPreferences settings;
SharedPreferences.Editor editor;
SharedPreferences prefs;
public ViewOrderAdapter (Context context, ArrayList<ViewOrder> view_order_array)
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mcontext = context;
prefs = mcontext.getSharedPreferences(PREFS_NAME, mcontext.MODE_PRIVATE);
this.view_order_array = view_order_array;
buttonState = new boolean[view_order_array.size()];
gps = new GPSTracker(mcontext);
@Override
public int getCount()
return view_order_array.size();
@Override
public Object getItem(int position)
return view_order_array.get(position);
@Override
public long getItemId(int position)
return position;
@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent)
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.order_single_row, parent, false);
view = convertView;
sdf = new SimpleDateFormat("HH:mm:ss");
currentTime = sdf.format(new Date());
final ViewOrder v_ordr = view_order_array.get(position);
TextView cname = (TextView)convertView.findViewById(R.id.restaurant_name_view);
cname.setText(v_ordr.getRestaurant());
TextView caddr = (TextView)convertView.findViewById(R.id.restaurant_address_view);
caddr.setText(v_ordr.getRaddress());
accept_btn = (Button)convertView.findViewById(R.id.order_accept_btn);
pickup_btn = (Button)convertView.findViewById(R.id.order_pickup_btn);
delieverd_btn = (Button)convertView.findViewById(R.id.order_delievery_btn);
accept_btn.setTag(position);
pickup_btn.setTag(position);
delieverd_btn.setTag(position);
order_view = (Button)convertView.findViewById(R.id.order_info);
rest_address = (Button)convertView.findViewById(R.id.rest_addrs);
cust_address = (Button)convertView.findViewById(R.id.cstom_addrs);
int id = Integer.parseInt(view_order_array.get(position).getId());
accept_btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(final View v)
AppConstants.btn_id = view_order_array.get(position).getId();
String id = AppConstants.btn_id;
//accept_btn.setId(position);
buttonState[position] = false;
String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth);
ShowMessagesDialogsUtitly.showProgressDialog(mcontext);
RequestParams requestParams = new RequestParams();
requestParams.put("k", token);
requestParams.put("id", id); //order id
requestParams.put("status", Integer.toString(1));
requestParams.put("time", currentTime);
requestParams.put("page", "send");
HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback()
@Override
public void onCompleteHttpResponse(String whichUrl, String jsonResponse)
ShowMessagesDialogsUtitly.hideProgressDialog();
if (jsonResponse != null)
try
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
if (jsonResponseObj.getInt("status") == 1)
accept_btn.setBackgroundColor(Color.GRAY);
Intent n = new Intent(mcontext, Order.class);
mcontext.startActivity(n);
((Activity)mcontext).finish();
else
Toast.makeText(mcontext, "Order progress error", Toast.LENGTH_SHORT).show();
catch (Exception e)
AppUtilites.printStackTrace(e);
else
Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show();
);
);
pickup_btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
AppConstants.btn_id = view_order_array.get(position).getId();
String id = AppConstants.btn_id;
String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth);
pickup_btn.setId(position);
ShowMessagesDialogsUtitly.showProgressDialog(mcontext);
RequestParams requestParams = new RequestParams();
requestParams.put("k", token);
requestParams.put("id", id); //order id
requestParams.put("status", Integer.toString(2));
requestParams.put("time", currentTime);
requestParams.put("page", "send");
HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback()
@Override
public void onCompleteHttpResponse(String whichUrl, String jsonResponse)
ShowMessagesDialogsUtitly.hideProgressDialog();
if (jsonResponse != null)
try
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
Toast.makeText(mcontext, "Order accepted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mcontext, "Order order not accepted", Toast.LENGTH_SHORT).show();
catch (Exception e)
AppUtilites.printStackTrace(e);
else
Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show();
);
);
delieverd_btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
AppConstants.btn_id = view_order_array.get(position).getId();
String id = AppConstants.btn_id;
String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth);
delieverd_btn.setId(position);
ShowMessagesDialogsUtitly.showProgressDialog(mcontext);
RequestParams requestParams = new RequestParams();
requestParams.put("k", token);
requestParams.put("id", id); //order id
requestParams.put("status", Integer.toString(3));
requestParams.put("time", currentTime);
requestParams.put("page", "send");
HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback()
@Override
public void onCompleteHttpResponse(String whichUrl, String jsonResponse)
ShowMessagesDialogsUtitly.hideProgressDialog();
if (jsonResponse != null)
try
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
if (jsonResponseObj.getInt("status") == 1)
else
Toast.makeText(mcontext, "Order order not delievered", Toast.LENGTH_SHORT).show();
catch (Exception e)
AppUtilites.printStackTrace(e);
else
Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show();
);
);
order_view.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.order_view_lay, (ViewGroup)view.findViewById(R.id.order_view_lay_id));
AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setView(layout);
alert = builder.create();
TextView order_id = (TextView) layout.findViewById(R.id.order_no);
order_id.setText(view_order_array.get(position).getOid());
TextView rest_name = (TextView) layout.findViewById(R.id.rstarnt_name);
rest_name.setText(view_order_array.get(position).getRestaurant());
TextView quantity = (TextView) layout.findViewById(R.id.quantity);
quantity.setText(view_order_array.get(position).getOid());
TextView item = (TextView) layout.findViewById(R.id.item);
item.setText(view_order_array.get(position).getMenu());
TextView price = (TextView) layout.findViewById(R.id.price);
price.setText(view_order_array.get(position).getAmount());
TextView subtotal = (TextView) layout.findViewById(R.id.sub_total);
subtotal.setText("XXXX");
TextView total = (TextView) layout.findViewById(R.id.total);
total.setText("XXXX");
Button ok = (Button) layout.findViewById(R.id.ok_popup_order_view_btn);
ok.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
alert.dismiss();
);
alert.show();
);
rest_address.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.restaurant_addr_lay, (ViewGroup)view.findViewById(R.id.rest_addr_lay_id));
AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setView(layout);
alert = builder.create();
TextView r_name = (TextView) layout.findViewById(R.id.rst_name_popup);
r_name.setText(view_order_array.get(position).getRestaurant());
TextView r_add = (TextView) layout.findViewById(R.id.rst_addr_popup);
r_add.setText(view_order_array.get(position).getRaddress());
TextView r_phone = (TextView) layout.findViewById(R.id.rst_phone_popup);
r_phone.setText(view_order_array.get(position).getRestaurant());
Button map = (Button) layout.findViewById(R.id.map_to_restaurant_popup);
map.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
if (gps.canGetLocation())
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getRaddress();
String addr = AppConstants.Address_To;
gps.getLocationFromAddress(mcontext, addr);
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
else
gps.showSettingsAlert();
);
Button map_dirct = (Button) layout.findViewById(R.id.directions_to_restaurant_popup);
map_dirct.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
if (gps.canGetLocation())
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getRaddress();
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
else
gps.showSettingsAlert();
);
Button ok = (Button) layout.findViewById(R.id.ok_popup_raddr_btn);
ok.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
alert.dismiss();
);
alert.show();
);
cust_address.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.customer_addr_lay, (ViewGroup)view.findViewById(R.id.cust_addr_lay_id));
AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setView(layout);
alert = builder.create();
TextView c_name = (TextView) layout.findViewById(R.id.cst_name_popup);
c_name.setText(view_order_array.get(position).getCustomer());
TextView c_add = (TextView) layout.findViewById(R.id.cst_addr_popup);
c_add.setText(view_order_array.get(position).getCaddress());
TextView c_phone = (TextView) layout.findViewById(R.id.cst_phone_popup);
c_phone.setText(view_order_array.get(position).getCphone());
TextView order_inst = (TextView) layout.findViewById(R.id.cst_order_inst_popup);
order_inst.setText(view_order_array.get(position).getInstruction());
Button map = (Button) layout.findViewById(R.id.map_to_customer_popup);
map.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
if (gps.canGetLocation())
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getCaddress();
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
else
gps.showSettingsAlert();
);
Button map_direct = (Button) layout.findViewById(R.id.directions_to_customer_popup);
map_direct.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
if (gps.canGetLocation())
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getCaddress();
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
else
gps.showSettingsAlert();
);
Button ok = (Button) layout.findViewById(R.id.ok_popup_caddr_btn);
ok.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
alert.dismiss();
);
alert.show();
);
return convertView;
【问题讨论】:
你需要使用 ViewHolder 模式。在上面的代码中发生的是最后添加按钮的颜色正在更新它是自然的...... 那是什么?你很难过我想在同一行改变颜色。并想象你的例子,改变相同的行颜色?确定要问什么? 嗨,请尝试在列表视图中使用视图持有者模式实现我编辑的答案。 它在 gridview 项目单击中解决了同样的问题,如果您有任何疑问,请告诉我 【参考方案1】:试试这个简单的答案:
通过您设置的标签找到您的按钮视图。
((Button) convertView.findViewWithTag(position)).setBackgroundColor(Color.GRAY);
您遇到此问题是因为您的适配器持有的最后一个 accept_btn 对象是它创建的最后一个视图对象。
因此,通过在按钮上设置标签,您就在正确的轨道上。但是您必须通过标签检索该视图。 让我知道这是否有帮助。
谢谢。
【讨论】:
是的。但是,他接下来会遇到的问题是,所有设置了相同 TAG 的按钮都会改变颜色。从那里他可以弄清楚如何为每个按钮设置不同的标签名称。此外,这个适配器将来可能会遇到更多问题,但这个答案将帮助他弄清楚他最初的问题。【参考方案2】:我已经在 gridview HERE 中写了相同问题的答案。您可以使用相同的技巧来解决此问题。 // 简单的想法
您可以实现自己的自定义 OnClickListener,并且可以为每个列表视图项的每次点击关联 id/tag/position。
yourBtn.setOnclickListener(new BtnClick(position, btnid));
自定义 OnclickListener:
Class BtnClick View.OnClickListener
int position;
int btnId;
Public BtnClick(int position, int id)
this.position = position;'
this.btnId = btnId;
@Override
public onClick(View v)
// You can add logic here for each item clicked using position and id of each item you passed in constructor
【讨论】:
以上是关于Android Listview 多个按钮点击的主要内容,如果未能解决你的问题,请参考以下文章
如何根据 android studio 中的 listview 项目点击更改活动图像和文本? java 或 kotlin