Android Listview滚动插入重复记录

时间:2015-04-13 08:45:19

标签: android sqlite listview android-listview android-adapter

我创建了一个包含EditText框的自定义列表视图。当我更改EditText中的文本时,我将该字段更新为数据库。它工作正常。

我面临的问题是当我滚动列表时,记录被添加到数据库中(重复记录)...我觉得这个问题可以通过在ListView滚动期间删除EditText焦点来解决,我不知道如何去做吧。或者,如果有人能提出更好的想法意味着它会有所帮助。

这是我的ListAdapter代码

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());
            }
        }
    });
}

}

在我的活动中,我正在加载像这样的ListView

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();
    }
}

我面临的问题是,当我从数据库加载记录时,由于EditText焦点移动,由于记录被插入数据库(即使在列表视图中也是滚动)。

我想知道如何避免重复记录插入数据库

2 个答案:

答案 0 :(得分:0)

现在,每当用户上下滚动列表时,都会调用getView()。 我实际上并不知道确切的要求,但你应该点击Editbox设置TextWatcher监听器。它会做什么,它不会调用你的TextWatcher evertime用户上下滚动。 在editbox的onClickListener上添加TextWatcher时,您可以在完成编辑后删除编辑框的焦点。

答案 1 :(得分:0)

你的问题不在于EditText关注焦点。这是关于每次调用TextWatchergetView被调用的信息。

解决此问题的一种简单方法是在TextWatcher更改文本之前删除getView,然后将其添加回来,这意味着您需要保留对TextWatcher <的引用/ p> 顺便说一句,你的代码中的另一个问题是你使用了错误的持有者。

编辑您的代码,如下所示。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ZoneHolder holder = null;
    if(row == null){
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ZoneHolder();
        holder.Label = (EditText)row.findViewById(R.id.txtLable);
        holder.iCount = (TextView)row.findViewById(R.id.FirstText);
        holder.dbid = (TextView) row.findViewById(R.id.dbid);

        row.setTag(holder);
    }else{
        holder = (ZoneHolder) row.getTag();
    }

    holder.zoneList = items.get(position);

    if(holder.watcher != null){
         holder.Label.removeTextChangedListener(holder.watcher);
    }

    setupItem(holder);

    holder.Label.addTextChangedListener(/* Your watcher goes here */);

    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;
    TextWatcher watcher;
}