-2

When I am trying to set value to the list view using custom adapter it shows only the last entered value in the hash map. hash map is static.I dont know why I am not getting all the values in the hashmap for that keys I am used in hashmap. here my code

public class Nextclass extends Activity  {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextclass);
list = (ListView)findViewById(R.id.list1);

  SelectItems();
  ViewItems();
}

 private List<Map<String, String>> 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 code ,desc ,name "
            + " from item", null);


        while (c.moveToNext()){


       String cod = c.getString(c.getColumnIndex("code"));
       datanums.put("code", cod);
        String desc1  = c.getString(c.getColumnIndex("desc"));
        datanums.put("desc", desc1);
        String name = c.getString(c.getColumnIndex("name"));
         datanums.put("name", name);
              datas.add(datanums);
        }

    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
       } 
    return datas; 
}
private void ViewItems() {

// TODO Auto-generated method stub

arrTemp = new String[datas.size()];


MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
Log.v("list itemm",datas.toString());


 }

public  class MyListAdapter extends BaseAdapter{

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(datas != null && datas.size() != 0){
        return datas.size();    
    }
    return 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return datas.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {

        holder = new ViewHolder();
        LayoutInflater inflater = Orders.this.getLayoutInflater();
        convertView = inflater.inflate(R.layout.order_simple_row, null);
        holder.textView1 = (TextView) convertView.findViewById(R.id.name);
        holder.textView2 = (TextView) convertView.findViewById(R.id.code);
        holder.textview3 = (TextView) convertView.findViewById(R.id.desc);
         holder.editText1 = (EditText) convertView.findViewById(R.id.cases); 


        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();
    }

    holder.ref = position;

     holder.textView1.setText(datanums.get("name"));
   holder.textview3.setText(datanums.get("code"));
    holder.textView2.setText(datanums.get("desc"));
     holder.editText1.setText(arrTemp[position]);


        holder.editText1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                arrTemp[holder.ref] = arg0.toString();
            }
        });

        return convertView;
    }

 private class ViewHolder {

     public Object list;
    TextView textView1,textView2,textview3;
 EditText editText1,editText2;
     int ref;
    }


    return convertView;
}

private class ViewHolder {

 public Object list;
TextView textView1,textView2,textview3;
EditText editText1;
 int ref;
}

please help.I used alternate methods,but not worked.thanks in advance

Bivin
  • 375
  • 3
  • 24

1 Answers1

0
 holder.textView1.setText(datas.get(position).get("name"));
   holder.textview3.setText(datas.get(position).get("code"));
    holder.textView2.setText(datas.get(position).get("desc"));

you are adding hash map in the array list but in the adapter you are trying to get data from the hash map, instead of that you need to take value from arraylist which will return hashmap object and from that object you need to get value for your keys.

hope this will help.

Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
  • I tried this just now.but when I run the code it shows empty textviews in the list. – Bivin Mar 21 '16 at 06:24
  • Empty data that means you are getting number of rows as per your data size, now you need to check when you are entering your data in the list, Also please put c.moveToFirst() before your while loop. – Silvans Solanki Mar 21 '16 at 06:28
  • 1
    I over checked the code.Now I can print values to the text view.but still it retrieves the last entered value in the hash map for all columns. please,do you have another way to view values in list view? – Bivin Mar 21 '16 at 07:04
  • can you update your question with updated code, you can try with CursorAdapter instead of BaseAdapter. – Silvans Solanki Mar 21 '16 at 07:11