Listview在滚动时丢失了editText的值

时间:2013-07-22 09:55:44

标签: android listview hashmap android-edittext

嗨,有谁可以请帮我这个?我正在使用自定义适配器来填充我的listview。我的listview有一个包含editText的hashmap。我的问题是当我在editText上输入内容并滚动listview时,editText的内容丢失了。我该怎么办?请帮助我,我现在正在堆叠它。下面显示了我的代码:

*************
myCustomAdapterIreport = new CustomArrayAdapterIreport(getApplicationContext(), mylist, R.layout.attribute_selected_ireport_file, 
                                    new String[]{FILE_NAME, DESC, UPLOADED_BY, DATE_UPLOADED, ACTION, ID, FILE_URI}, 
                                    new int[]{R.id.tv_iFile, R.id.txt_iDesc,R.id.tv_iUploadedBy,R.id.tv_iDateUploaded, R.id.tv_iAction, 
                                    R.id.tv_RowId, R.id.tv_iUri}, true);
        lv_AttachedFileData.setAdapter(myCustomAdapterIreport);

***********************


public class CustomArrayAdapterIreport extends SimpleAdapter
{
    private Context context;
    private final ArrayList<HashMap<String, String>> mData; 
    private ArrayList<HashMap<String, String>> unfilteredValues;
    private boolean chosenValues, ismarkAll = true;
    private int resource;
    private String[] from;
    private int[] to;
    private SimpleFilter mFilter;
    private ArrayList<String> arraylistAttach, arraylistAttachId;

    S_10th_IReportMain iReport;
    public CustomArrayAdapterIreport(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, 
            int[] to, boolean chosenValues) 
    {
        super(context, data, resource, from, to);
        this.context = context;
        mData = data;
        this.unfilteredValues = mData;
        this.resource = resource;
        this.from = from;
        this.to = to;
        this.arraylistAttach = new ArrayList<String>();
        this.arraylistAttachId = new ArrayList<String>();
        this.chosenValues = chosenValues;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        View rowView = null;

        if(rowView == null)
        {
            try{
                rowView = inflater.inflate(resource, null, true);
                textViewTitle = (TextView) rowView.findViewById(to[0]);
                txt_Desc = (EditText) rowView.findViewById(to[1]);
                tv_CreatedBy = (TextView) rowView.findViewById(to[2]);
                tv_DateCreated= (TextView) rowView.findViewById(to[3]);
                final TextView tv_Action = (TextView) rowView.findViewById(to[4]);
                final TextView tv_rowId = (TextView) rowView.findViewById(to[5]);
                tv_Uri = (TextView)rowView.findViewById(to[6]);

                Typeface tf= Typeface.createFromAsset(context.getAssets(), "Gothic_Regular.TTF");
                textViewTitle.setTypeface(tf);
                txt_Desc.setTypeface(tf);
                tv_CreatedBy.setTypeface(tf);
                tv_DateCreated.setTypeface(tf);
                tv_Action.setTypeface(tf);

                final String FileKey = from[0];
                String DescKey = from[1];
                String UploadedByKey = from[2];
                String DateUploadKey = from[3];
                String ActionKey = from[4];
                final String idKey = from[5];
                String FileUri = from[6];

                final String FileName = unfilteredValues.get(position).get(FileKey).toString();
                String Desc = unfilteredValues.get(position).get(DescKey).toString();
                String UploadedBy = unfilteredValues.get(position).get(UploadedByKey).toString();
                String DateUpload = unfilteredValues.get(position).get(DateUploadKey).toString();
                String Action = unfilteredValues.get(position).get(ActionKey).toString();
                String AttachId = unfilteredValues.get(position).get(idKey).toString();
                String FileNameUri = unfilteredValues.get(position).get(FileUri).toString();

                textViewTitle.setText(FileName);
                txt_Desc.setText(Desc);
                tv_CreatedBy.setText(UploadedBy);
                tv_DateCreated.setText(DateUpload);
                tv_Action.setText(Action);
                tv_rowId.setText(AttachId);
                tv_Uri.setText(FileNameUri);

                textViewTitle.setId(position);
                txt_Desc.setId(position);
                tv_CreatedBy.setId(position);
                tv_DateCreated.setId(position);
                tv_Uri.setId(position);
                final String id = tv_rowId.getText().toString();
                tv_Action.setOnClickListener(new OnClickListener() 
                {

                    @Override
                    public void onClick(View view) 
                    {

                        AlertDialog.Builder builder = new AlertDialog.Builder(S_10th_IReportMain.this);
                        builder
                        .setTitle("Warning!")
                        .setMessage("Are you sure you want delete this file?")

                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                        {                   
                            @Override
                            public void onClick(DialogInterface dialog, int id) 
                            {

                                deleteMyFilesiReport(position);
                                Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
                            }

                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() 
                        {                   
                            @Override
                            public void onClick(DialogInterface dialog, int id) 
                            {
                                dialog.cancel();
                            }
                        });

                        AlertDialog alertDialog = builder.create();
                        alertDialog.show(); 
                    }                                                       
                    });



            }catch (Exception e){
                e.printStackTrace();
            }
            catch (OutOfMemoryError E){
                E.printStackTrace();
            }
        }

        return rowView;
    }

        public ArrayList<String> getArrayListConsumer() {
            return this.arraylistAttach;
        }

        public ArrayList<String> getArrayListConsumerId() {
            return this.arraylistAttachId;
        }

        public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new SimpleFilter();
            }
                return mFilter;
        }

        public int getCount() {
           return unfilteredValues.size();
        }

        private class SimpleFilter extends Filter {
            @SuppressWarnings("unchecked")
            @Override
            protected FilterResults performFiltering(CharSequence prefix) {

                FilterResults results = new FilterResults();
                String prefixString = null == prefix ? null : prefix.toString().toLowerCase();
                ArrayList<HashMap<String, String>> unfilteredValues;

                if (null != prefixString && prefixString.length() > 0) {
                    synchronized (mData) {
                        unfilteredValues = (ArrayList<HashMap<String, String>>) mData.clone();
                    }

                    for (int i = unfilteredValues.size() - 1; i >= 0; --i) {
                        HashMap<String, String> h = unfilteredValues.get(i);

                        String str =  (String)h.get(from[0]).toString();
                            if (!str.toLowerCase().startsWith(prefixString)) {
                                unfilteredValues.remove(i);
                            }

                    }

                    //Log.i(Constants.TAG, String.valueOf(unfilteredValues.size()));
                    results.values = unfilteredValues;
                    results.count = unfilteredValues.size();

                } else {
                    synchronized (mData) {
                        results.values = mData;
                        results.count = mData.size();
                    }
                }
                return results;
            }

            @SuppressWarnings("unchecked")
            @Override
             protected void publishResults(CharSequence constraint, FilterResults results) {
                //noinspection unchecked
                unfilteredValues = (ArrayList<HashMap<String, String>>) results.values;
                notifyDataSetChanged();

            }
        }

    }

1 个答案:

答案 0 :(得分:0)

将hashMap用作classVariable。

HashMap<String, String> SavedData = new HashMap<String, String>();

然后,  将任何唯一标识符作为标记提供给

之类的编辑文本
editTxt.setTag(IdNo);

并检查

if(SavedData.containsKey(IdNo)) {
    editTxt.setText(SavedData.get(IdNo).toString());

}

editTxt.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void afterTextChanged(Editable s) {

                 //Save your Changed Text to HashMap using tag of edittext
                 SavedData.add(editTxt.getTag(),s.toString());

            }
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }
        });