Reputation: 11
I am new to android. when I was inserting value to the edit text in the list view it gets duplicates while scrolling. I searched everywhere but i could n't find exact solution. my list view is very large because it fetches value from database. here is my android code
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);
}
}
please help me with this.
Upvotes: 0
Views: 794
Reputation: 578
Listview in Android reuse items, so if user scrolls view, some view reuse old views and it should show duplicate value if you don't manage careful. If your row views have alot items, you should check conditions to show view properly. Example:
if(true){
edt.setText("abc");
}else{
edt.setText("cde");
}
If you show data in each condition, you must set value or reset that view in case unuse
Upvotes: 0
Reputation: 777
Try to set tag in your getview method of adapter class and also post your code for adapter class ,so we can help better.
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();
}
Upvotes: 1
Reputation: 1909
Add this two methods to your listview adapter class
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return getCount();
}
Upvotes: 0