1
I have a app where a json is read and returned in a list all items in this json.
I’m using the ADAPTER to do this, I put a EditText
on the screen, this editText is replicated to every json read item.
Example
1- product (EditText
) QTD:
2- product 2 (EditText
) QTD:
And so on, my difficulty is in reading these EditText
1 by 1, I’m using a FOR
today to change the Qtd of each and give one update on the bench, but when placing QTD = 5
for example in the first item and qtd = 10
on the other, it replicates the 5 for the other items at the time of update.
Follows my Activity
:
carrinho.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = HOST + "/listaCarrinho.php";
String idc = "162";
Ion.with(getBaseContext())
.load(url)
.setBodyParameter("idCarrinho", idc)
.asJsonArray()
.setCallback(new FutureCallback<JsonArray>() {
@Override
public void onCompleted(Exception e, JsonArray result) {
try {
String qtd = "";
for(int i = 0; i < result.size(); i++){
JsonObject obj = result.get(i).getAsJsonObject();
String IDCarrinho = Integer.toString(obj.get("id").getAsInt());
Log.d(IDCarrinho, "onCompleted: IDCarrinho");
EditText quantidade = (EditText) findViewById(R.id.qtd);
String idedit = Integer.toString(quantidade.getId());
Log.d(idedit, "onCompleted: IDEdit");
qtd = quantidade.getText().toString();
Log.d(qtd, "onCompleted: quantidade");
String url1 = HOST + "/updateQtd.php";
String idcar = "162";
Ion.with(getBaseContext())
.load(url1)
.setBodyParameter("quantidade", qtd)
.setBodyParameter("id", IDCarrinho)
.setBodyParameter("idcarrinho", idcar)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
try {
String RETORNO = result.get("UPDATE").getAsString();
if(RETORNO.equals("OK")){
Toast.makeText(CarrinhoActivity.this, "O PEDIDO FOI FINALIZADO", Toast.LENGTH_LONG).show();
} else if(RETORNO.equals("ERRO")){
Toast.makeText(CarrinhoActivity.this, "NÃO FOI FINALIZADO!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(CarrinhoActivity.this, "Ops ocorreu um erro,", Toast.LENGTH_LONG).show();
}
} catch (Exception e1) {
e1.printStackTrace();
}
carrinhoAdapter.notifyDataSetChanged();
}
});
}
} catch (Exception e1) {
e1.printStackTrace();
}
carrinhoAdapter.notifyDataSetChanged();
}
});
}
});
Follows my Adapter:
private Context ctx;
private List<Carrinho> lista;
public CarrinhoAdapter(Context ctx2, List<Carrinho> lista2){
ctx = ctx2;
lista = lista2;
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Carrinho getItem(int position) {
return lista.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = null;
if(view == null){
LayoutInflater inflater = ((Activity)ctx).getLayoutInflater();
v = inflater.inflate(R.layout.carrinho_lista,null);
} else {
v = view;
}
Carrinho c = getItem(position);
EditText Qtd = (EditText) v.findViewById(R.id.qtd);
TextView Prod = (TextView) v.findViewById(R.id.prod);
Qtd.setText(String.valueOf(c.getQtd()));
Prod.setText(c.getNome());
return v;
}[![inserir a descrição da imagem aqui][1]][1]
From what I understand from the code, you are starting your edittext and synchronously trying to recover its value. Depending on the case it is right to use some button that the user indicates when saving the form or something of the type, ai in the event of that button you recover the value passed to Edittext.
– Murillo Comino
You say, put 1 check for example on each item? ai do if checked... da o update?
– Gustavo Melo
Like this. Unfortunately I am without the pc, so I can help you in your specific situation. Here link It shows something like what you need. In the case of this example it uses a Listener when taking the edittext focus retrieves the values in it. You can try to implement this example for yourself.
– Murillo Comino