我是android新手。当我在列表视图中向编辑文本插入值时,它会在滚动时获得重复项。我到处搜索,但我找不到确切的解决方案。我的列表视图非常大,因为它从数据库中获取值。 这是我的android代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orders);
list = (ListView)findViewById(R.id.list1);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
SelectItems();
ViewItems();
cases = (EditText)findViewById(R.id.cases);
pcs = (EditText)findViewById(R.id.pcs);
clear1=(Button)findViewById(R.id.clear2);
cout1=(Button)findViewById(R.id.cout2);
clear1.setOnClickListener(this);
cout1.setOnClickListener(this);
}
private void SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
Log.v("item detailss", c.toString());
while (c.moveToNext()){
HashMap<String, String> datanums = new HashMap<String, String>();
String im_code = c.getString(c.getColumnIndex("im_code"));
String desc = c.getString(c.getColumnIndex("im_desc"));
datanums.put("code", im_code);
datanums.put("name", desc);
String price = c.getString(c.getColumnIndex("im_srp"));
datanums.put("ims", price);
datas.add(datanums);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
private void ViewItems() {
// TODO Auto-generated method stub
list.setAdapter(null);
String[] fromwhere = { "code","name","ims" };
int[] viewswhere = { R.id.code,R.id.name,R.id.srp};
ADAhere = new SimpleAdapter(Orders.this, datas,
R.layout.order_simple_row, fromwhere, viewswhere);
list.setAdapter(ADAhere);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.clear2:
Intent i= new Intent(getApplicationContext(),Orders.class);
startActivity(i);
break;
case R.id.cout2:
Checkout(v);
break;
default:
break;
}
}
private void Checkout(View view) {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialou = new AlertDialog.Builder(Orders.this);
alertDialou.setMessage("DO YOU WANT TO CHECKOUT ?");
alertDialou.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
getcases();
invoiceAlert();
}
private void invoiceAlert() {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialo = new AlertDialog.Builder(Orders.this);
alertDialo.setMessage("ORDER SUCCESS..");
alertDialo.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Orderheader();
printAlert();
}
private void printAlert() {
// TODO Auto-generated method stub
AlertDialog.Builder alertDial = new AlertDialog.Builder(Orders.this);
alertDial.setMessage("DO YOU WANT TO PRINT NOW ?");
alertDial.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
Intent in = new Intent(Orders.this,Menu_iem.class);
startActivity(in);
}
});
alertDial.setNegativeButton("LATER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = alertDial.create();
// show alert
alertDialog.show();
}
});
AlertDialog alertDialog = alertDialo.create();
// show alert
alertDialog.show();
}
});
alertDialou.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent in= new Intent(Orders.this,Orders.class);
startActivity(in);
}
});
AlertDialog alertDialog = alertDialou.create();
// show alert
alertDialog.show();
}
protected void getcases() {
// TODO Auto-generated method stub
DatabaseHelper databasecontroller = new DatabaseHelper(Orders.this);
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
float srps = (float) 0.0;
int qtys = 0;
databas.put("imuom", "CASES");
if(list != null){
for(int i = 0; i< list.getChildCount();i++){
View vie = list.getChildAt(i);
EditText ed1= (EditText) vie.findViewById(R.id.cases);
String cases1 = ed1.getText().toString();
databas.put("A",ed1.getText().toString());
EditText ed2= (EditText) vie.findViewById(R.id.piece);
String cases2 = ed2.getText().toString();
databas.put("B",ed1.getText().toString());
data.add(databas);
databasecontroller.inserttable(databas);
}
}
请帮助我。
答案 0 :(得分:1)
尝试在适配器类的getview方法中设置标记,并发布适配器类的代码,这样我们可以提供更好的帮助。
if (convertView == null) {
convertView = inflater.inflate(R.layout.simple_store_item, parent, false);
mViewHolder = new CustomViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (CustomViewHolder) convertView.getTag();
}
答案 1 :(得分:0)
将这两个方法添加到listview适配器类
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return getCount();
}
答案 2 :(得分:0)
Android视图中的Listview重用项目,因此如果用户滚动视图,某些视图会重复使用旧视图,如果您不仔细管理,它应显示重复值。 如果您的行视图中有很多项,则应检查条件以正确显示视图。例如:
if(true){
edt.setText("abc");
}else{
edt.setText("cde");
}
如果您在每种情况下显示数据,则必须设置值或重置该视图以防无法使用