根据值使用自定义适配器编辑列表视图中的项目
Posted
技术标签:
【中文标题】根据值使用自定义适配器编辑列表视图中的项目【英文标题】:edit item in listview with custom adapter depending on value 【发布时间】:2015-10-31 04:48:17 【问题描述】:当我单击列表视图中的按钮时,我会调用一个函数,在该函数中我会根据单击的按钮清楚地发送一个值。当我得到项目字段更改值的积极响应功能时,首先是validado = 0
,如果单击按钮更改为validado = 1
。如果将此验证为 1,则该按钮应具有背景图像,以警告该项目已通过验证。所有这一切起初对我来说都很好,但是如果我单击任何按钮,无论我的列表中有多少项目,总是会更改它被单击的项目以及我列表中的第一个项目。当我的数据库在本地和服务器中时,第一项的验证值仍然是 0,这很有趣
我的适配器中的getView
@Override
public View getView(int position, View convertView, final ViewGroup parent)
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = mInflater.inflate(R.layout.lista_validacion_multiple, null);
holder = new ViewHolder();
holder.txtNombre = (TextView)convertView.findViewById(R.id.txtNombre);
holder.txtAsiento = (TextView)convertView.findViewById(R.id.txtAsiento);
holder.txtTicket = (TextView)convertView.findViewById(R.id.txtTicket);
holder.txtNumero = (TextView)convertView.findViewById(R.id.txtNumero);
holder.btn = (Button)convertView.findViewById(R.id.button1);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
holder.txtNombre.setText("Nombre :"+rowItem.getNombre());
holder.txtTicket.setText("Ticket :"+rowItem.getTicket());
if (!rowItem.getAsiento().equals("") && !rowItem.getAsiento().equals("null") && rowItem.getAsiento() != null)
holder.txtAsiento.setText("Asiento :"+rowItem.getAsiento());
if (!rowItem.getNumero().equals("") && !rowItem.getNumero().equals("null") && rowItem.getNumero() != null)
holder.txtNumero.setText("Número :"+rowItem.getNumero());
System.out.println("item "+rowItem.getId_inscripcion()+" validado = "+rowItem.getValidado());
if(rowItem.getValidado()==1)
System.out.println("ENTRO");
holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
holder.btn.setText("");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.btn.setLayoutParams(params);
else
holder.btn.setTag(position);
holder.btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
int position=(Integer)v.getTag();
RowItem item_click = getItem(position);
Connection cn = new Connection();
SessionManager manager = new SessionManager();
BaseDeDatos nueva = new BaseDeDatos();
JSONObject json = new JSONObject();
if(cn.isNetworkAvailable(parent.getContext()))
String nombreCliente = manager.getValue(parent.getContext(), "nombreCliente");
String user = manager.getValue(parent.getContext(), "nombreUser");
String folioEvento = manager.getValue(parent.getContext(), "folioEvento");
String codigoEvento = manager.getValue(parent.getContext(), "codigoEvento");
String seleccionValidadora = manager.getValue(parent.getContext(), "opcionVerificadora");
String nombreUser = manager.getValue(parent.getContext(), "nombreUser");
String hashUser = manager.getValue(parent.getContext(), "hashUsuario");
String URL_TICKET = Config.URL_BASE + nombreCliente
+ "/" + Config.URL_VALIDACION_TICKET
+ nombreUser + "/" + hashUser + "/"
+ folioEvento + "/" + item_click.getHash()
+ "/0/";
System.out.println(URL_TICKET);
if (manager.getValue(parent.getContext(),
"checkin") != null)
int id_checkin = nueva.idCheckin(parent.getContext(), manager.getValue(parent.getContext(),"checkin"));
String url = URL_TICKET + id_checkin;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("content-type",
"application/json");
try
// Construimos el objeto cliente en formato
// JSON
JSONObject dato = new JSONObject();
StringEntity entity = new StringEntity(
dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient
.execute(post);
String respStr = EntityUtils.toString(resp
.getEntity());
JSONObject respJSON = new JSONObject(
respStr);
json = respJSON;
catch (Exception ex)
Log.e("ServicioRest", "Error!", ex);
else
String url = URL_TICKET;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("content-type",
"application/json");
try
// Construimos el objeto cliente en formato
// JSON
JSONObject dato = new JSONObject();
StringEntity entity = new StringEntity(
dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient
.execute(post);
String respStr = EntityUtils.toString(resp
.getEntity());
JSONObject respJSON = new JSONObject(
respStr);
json = respJSON;
catch (Exception ex)
Log.e("ServicioRest", "Error!", ex);
System.out.println(json);
else
);
return convertView;
我显示日志显示该值仍为 0
08-07 13:39:21.890: I/System.out(14317): item 936 validado = 0
08-07 13:39:21.890: I/System.out(14317): item 937 validado = 0
08-07 13:39:21.890: I/System.out(14317): item 938 validado = 0
当你第一次进入listview的视图时退出,然后我将验证一个铭文,无论如何,这就是我得到的
08-07 13:44:08.300: I/System.out(14317): item 936 validado = 0
08-07 13:44:08.300: I/System.out(14317): item 937 validado = 1
08-07 13:44:08.305: I/System.out(14317): item 938 validado = 0
没关系,除了第一项加上验证图像的按钮的费用,我只想对rowItem.getValidado()==1
中的元素收取费用
编辑
我回答了我的问题。
【问题讨论】:
I just want to be charged to the element that is in rowItem.getValidado()==1
是什么意思
表示只有当rowItem.getValidado() == 1 时改变按钮元素的背景图片。 @meda
【参考方案1】:
因此,您单击 ListView
上的 1 个项目,它会更改其外观(这是有意的),因为您的值更改为 1。但无论如何,ListView
中的第一个可见项目也会更改(无意) ?
我不是专家,但我猜您的ViewHolder
有问题。尝试删除 ViewHolder
逻辑,看看您是否仍然遇到问题(用于测试)。 ListView
很难回收视图并用新数据重新填充它们。你的getItem()
甚至可能有问题。但我怀疑这是ViewHolder
逻辑。
还需要在每次调用getView()
时声明Inflater
吗? prob 应该将其移至构造函数并在getView()
中实例化它。
【讨论】:
没错,没有任何意图改变。如果我单击第一个或单击最后一个,您应该将其更改为单击而不是任何其他。 我发现错误代码本身很好并且可以工作,错误是代码正在执行的地方。【参考方案2】:错误是代码到哪里而不是如何。
@Override
public View getView(int position, View convertView, final ViewGroup parent)
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = mInflater.inflate(R.layout.lista_validacion_multiple, null);
holder = new ViewHolder();
holder.txtNombre = (TextView)convertView.findViewById(R.id.txtNombre);
holder.txtAsiento = (TextView)convertView.findViewById(R.id.txtAsiento);
holder.txtTicket = (TextView)convertView.findViewById(R.id.txtTicket);
holder.txtNumero = (TextView)convertView.findViewById(R.id.txtNumero);
holder.btn = (Button)convertView.findViewById(R.id.button1);
holder.txtNombre.setText("Nombre :"+rowItem.getNombre());
holder.txtTicket.setText("Ticket :"+rowItem.getTicket());
if (!rowItem.getAsiento().equals("") && !rowItem.getAsiento().equals("null") && rowItem.getAsiento() != null)
holder.txtAsiento.setText("Asiento :"+rowItem.getAsiento());
if (!rowItem.getNumero().equals("") && !rowItem.getNumero().equals("null") && rowItem.getNumero() != null)
holder.txtNumero.setText("Número :"+rowItem.getNumero());
System.out.println("item "+rowItem.getId_inscripcion()+" validado = "+rowItem.getValidado());
if(rowItem.getValidado()==1)
holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
holder.btn.setText("");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.btn.setLayoutParams(params);
else
holder.btn.setTag(position);
holder.btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
int position=(Integer)v.getTag();
RowItem item_click = getItem(position);
Connection cn = new Connection();
SessionManager manager = new SessionManager();
BaseDeDatos nueva = new BaseDeDatos();
JSONObject json = new JSONObject();
if(cn.isNetworkAvailable(parent.getContext()))
String nombreCliente = manager.getValue(parent.getContext(), "nombreCliente");
String user = manager.getValue(parent.getContext(), "nombreUser");
String folioEvento = manager.getValue(parent.getContext(), "folioEvento");
String codigoEvento = manager.getValue(parent.getContext(), "codigoEvento");
String seleccionValidadora = manager.getValue(parent.getContext(), "opcionVerificadora");
String nombreUser = manager.getValue(parent.getContext(), "nombreUser");
String hashUser = manager.getValue(parent.getContext(), "hashUsuario");
String URL_TICKET = Config.URL_BASE + nombreCliente
+ "/" + Config.URL_VALIDACION_TICKET
+ nombreUser + "/" + hashUser + "/"
+ folioEvento + "/" + item_click.getHash()
+ "/0/";
System.out.println(URL_TICKET);
if (manager.getValue(parent.getContext(),
"checkin") != null)
int id_checkin = nueva.idCheckin(parent.getContext(), manager.getValue(parent.getContext(),"checkin"));
String url = URL_TICKET + id_checkin;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("content-type",
"application/json");
try
// Construimos el objeto cliente en formato
// JSON
JSONObject dato = new JSONObject();
StringEntity entity = new StringEntity(
dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient
.execute(post);
String respStr = EntityUtils.toString(resp
.getEntity());
JSONObject respJSON = new JSONObject(
respStr);
json = respJSON;
catch (Exception ex)
Log.e("ServicioRest", "Error!", ex);
else
String url = URL_TICKET;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("content-type",
"application/json");
try
// Construimos el objeto cliente en formato
// JSON
JSONObject dato = new JSONObject();
StringEntity entity = new StringEntity(
dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient
.execute(post);
String respStr = EntityUtils.toString(resp
.getEntity());
JSONObject respJSON = new JSONObject(
respStr);
json = respJSON;
catch (Exception ex)
Log.e("ServicioRest", "Error!", ex);
System.out.println(json);
else
);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
return convertView;
【讨论】:
以上是关于根据值使用自定义适配器编辑列表视图中的项目的主要内容,如果未能解决你的问题,请参考以下文章
Android - 通过复选框使用自定义适配器从列表视图中获取项目