我尝试使用对话框更新点击行的数据。我可以更新,但当我向下滚动时,某些行也会更新。然后,当我再次向上滚动时,数据会随机更新到其他行。
以下是我的适配器代码:
public class ViewHolder {
TextView productid, productname, pcs, cs;
ImageView dist;
}
public List<Product_List> parkingList;
public Context context;
ArrayList<Product_List> arrayList;
private MyAppAdapter(List<Product_List> apps, Context context) {
this.parkingList = apps;
this.context = context;
arrayList = new ArrayList<Product_List>();
arrayList.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View rowView = convertView;
final MyAppAdapter.ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.productlist, parent, false);
viewHolder = new MyAppAdapter.ViewHolder();
viewHolder.productid = (TextView) rowView.findViewById(R.id.productid);
viewHolder.productname = (TextView) rowView.findViewById(R.id.productname);
viewHolder.pcs = (TextView) rowView.findViewById(R.id.pcs);
viewHolder.cs = (TextView) rowView.findViewById(R.id.cs);
viewHolder.dist = (ImageView) rowView.findViewById(R.id.dist);
rowView.setTag(viewHolder);
} else {
viewHolder = (MyAppAdapter.ViewHolder) convertView.getTag();
}
viewHolder.productid.setText(parkingList.get(position).getInvtID() + "");
viewHolder.productname.setText(parkingList.get(position).getDescr() + "");
final String a = viewHolder.productid.getText().toString();
final String b = viewHolder.productname.getText().toString();
if ( position % 2 == 0) {
rowView.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else {
rowView.setBackgroundColor(Color.parseColor("#ECEAEA"));
}
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(context);
dialog.setTitle("Inventory Checking");
dialog.setContentView(R.layout.inventory);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
final TextView productids = (TextView) dialog.findViewById(R.id.productids);
productids.setText(a);
final TextView productdesc = (TextView) dialog.findViewById(R.id.productdesc);
productdesc.setText(b);
final EditText pcss = (EditText) dialog.findViewById(R.id.pcss);
final EditText css = (EditText) dialog.findViewById(R.id.css);
final RadioButton btnyes = (RadioButton) dialog.findViewById(R.id.btnyes);
final RadioButton btnno = (RadioButton) dialog.findViewById(R.id.btnno);
btnno.setChecked(true);
Button btnsave = (Button) dialog.findViewById(R.id.btnsave);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.pcs.setText(pcss.getText().toString());
viewHolder.cs.setText(css.getText().toString());
if (btnyes.isChecked()) {
viewHolder.dist.setImageResource(R.drawable.done);
} else {
viewHolder.dist.setImageResource(R.drawable.no);
}
notifyDataSetChanged();
dialog.dismiss();
}
});
dialog.show();
}
});
return rowView;
}
}
这是我的适配器代码。
String msg="Found";
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(InventoryChecking1.this, "Synchronising",
"Listview Loading! Please Wait...", true);
}
@Override
protected String doInBackground(String... strings) {
try {
Connection conn = connectionClass.CONN();
if (conn == null) {
success = false;
} else {
String query = "SELECT * FROM Inventory";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) {
while (rs.next()) {
try {
if (rs.getString("Status").equals("A") || rs.getString("Status").equals("AC")) {
itemsArrayList.add(new Product_List(rs.getString("InvtID"), rs.getString("Descr"), rs.getString("Brand"),
rs.getString("Category")));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "Found";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
success = false;
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
progress.dismiss();
// Toast.makeText(SellerActivity.this, msg + "", Toast.LENGTH_LONG).show();
if (success == false) {
} else {
try {
myAppAdapter = new MyAppAdapter(itemsArrayList, InventoryChecking1.this);
listView.setAdapter(myAppAdapter);
} catch (Exception ex) {
}
}
}
}
答案 0 :(得分:0)
更改列表中的数据而不是UI视图只需更改列表并通知 适配器:
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Product_List product_list = new Product_List(pcss.getText().toString(),css.getText().toString());
parkingList.set( position,product_list );
notifyDataSetChanged();
dialog.dismiss();
}
});