2

Hi can anyone please help me with this one? I am using a custom adapter for populating my listview. My listview has hashmap that holds an editText. My problem is when I enter something on the editText and scroll the listview, the content of the editText lost. What should I do?Please help me I'm currently stack in it. Below shows my code:

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

            }
        }

    }
lolliloop
  • 389
  • 8
  • 23
  • 1
    Listview will reload the view when you scroll it. so it won't save any values of edittext. you need to store data in hashmap or something else to save entered data and then you can assign values to edittext when it gets reloaded. – Armaan Stranger Jul 22 '13 at 10:03
  • can you give an example please? – lolliloop Jul 22 '13 at 10:04
  • http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html – Lalit Poptani Jul 22 '13 at 10:27
  • thank you for your suggestion, it really gives me nice ideas but this http://stackoverflow.com/questions/9328301/android-edittext-loses-content-on-scroll-in-listview helps me to soleve my problem. – lolliloop Jul 23 '13 at 01:32

1 Answers1

0

Use hashMap as classVariable.

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

then, Give any unique identifier as tag to edittext like

editTxt.setTag(IdNo);

and check

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) {

            }
        });
Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24