I had created a Custom Listview that has EditText box. When I change the text in the EditText, I am updating the field to database. It is working fine.
The Problem I am facing is when I scroll the list, records are added to the database (duplicating records)... I feel this issue can be solved by removing EditText focus during the ListView Scrolling, I don't know how to do it. Or else if some can give better idea means it will be helpful.
This is my ListAdapter Code
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class ZoneListAdapter extends ArrayAdapter<Zone> {
protected static final String LOG_TAG = ZoneListAdapter.class.getSimpleName();
private PfDatabase db;
private List<Zone> items;
private int layoutResourceId;
private Context context;
int companyid;
public ZoneListAdapter(Context context, int layoutResourceId, List<Zone> items, int companyid) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.items = items;
this.companyid=companyid;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ZoneHolder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ZoneHolder();
holder.zoneList = items.get(position);
holder.Label = (EditText)row.findViewById(R.id.txtLable);
holder.iCount = (TextView)row.findViewById(R.id.FirstText);
holder.dbid = (TextView) row.findViewById(R.id.dbid);
setZoneLabelTextChangeListener(holder);
row.setTag(holder);
setupItem(holder);
return row;
}
private void setupItem(ZoneHolder holder) {
holder.Label.setText(holder.zoneList.getLabel());
holder.iCount.setText(holder.zoneList.getCounter()+"");
}
public static class ZoneHolder {
Zone zoneList;
EditText Label;
TextView iCount;
TextView dbid;
}
private void setZoneLabelTextChangeListener(final ZoneHolder holder) {
holder.Label.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//holder.zoneList.setLabel(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) {
holder.zoneList.setLabel(s.toString());
db = new ProfireDatabase(context);
String strDBID = holder.dbid.getText().toString();
Log.d("DBID",strDBID);
if(strDBID.equals("TextView")){ //Insert New Record
db.insertZone(companyid, s.toString());
Cursor getId = db.myQuery("SELECT _id from zone order by _id DESC limit 1");
int CompanyID = getId.getInt(0);
holder.dbid.setText(""+CompanyID);
} else { //Update Record
int iRowId = Integer.parseInt(strDBID);
Log.d("ROW ID",iRowId+"");
db.updatezone(iRowId, s.toString());
}
}
});
}
}
In My Activity I am loading the ListView like this
if (strAction != null && strAction.equals("new")){
//For New Records
istrID = Integer.parseInt(strID);
ZoneListView.setAdapter(zadapter);
} else {
//Loading the records from database for the company id
ZoneListView.setAdapter(zadapter);
istrID = Integer.parseInt(strID);
Cursor getZone = db.myQuery("Select * from zone where companyid="+istrID);
if (getZone != null) {
while(getZone.moveToNext()) {
zadapter.insert(new Zone(getZone.getString(1),getZone.getInt(0)), 0);
}
getZone.close();
}
}
The problem I am facing is, when I load the records from database, due to EditText focus moves, due to that the records are getting inserted in Database (even during the listview scroll also).
I would like to know how to avoid the duplicate records getting inserted in database
Aucun commentaire:
Enregistrer un commentaire