iamRaja iamRaja - 3 months ago 16
Android Question

how to add data into listviewAdapter?

I have made listViewAdapter which gets data from three EditText and display on it but the problem is, if i click the add button then the first entry is over written by the new entries .i want to add the data to be in the list.

Here is my android code:

public class List extends AppCompatActivity {

private EditText etItem;
private EditText etQty;
private EditText etRate;

public static String item;
public static String qty;
public static String rate;
private ArrayList<HashMap> list;
public listviewAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

etItem=(EditText)findViewById(R.id.etItem);
etQty=(EditText)findViewById(R.id.etQty);
etRate=(EditText)findViewById(R.id.etRate);

item=etItem.getText().toString();
qty=etQty.getText().toString();
rate=etRate.getText().toString();



Button bAdd=(Button)findViewById(R.id.bAdd);
bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

ListView lview = (ListView) findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);
adapter.notifyDataSetChanged();
etItem.setText("");
etQty.setText("");
etRate.setText("");
}
});
}
private void populateList() {
list = new ArrayList<HashMap>();
item=etItem.getText().toString();
qty=etQty.getText().toString();
rate=etRate.getText().toString();
HashMap temp = new HashMap();

temp.put(FIRST_COLUMN,"");
temp.put(SECOND_COLUMN, item);
temp.put(THIRD_COLUMN, qty);
temp.put(FOURTH_COLUMN, rate);
list.add(temp);
}

Answer

packing hashmap to list(adapter) is a nonsens :)

1) create simple pojo

https://en.wikipedia.org/wiki/Plain_Old_Java_Object

public class Item {
      private String _itemName;
      private int _qty;
      private int _rate;

     public Item(String itemName, int qty, int rate) {
         _itemName = itemName;
         _qty = qty;
         _rate = rate;
     }

     // create getter/setter if need 

    public String getName() { return _itemName;}
    // if you want create setter 
    // then keep in mind your 
    // item _itemName variable could not be marked as final 
    public void setName(String name) { _itemName = name;} 
}

2) create Adapter extending for example BaseAdapter class https://developer.android.com/reference/android/widget/BaseAdapter.html

public class MyAdapter extends BaseAdapter {

    private List<item> _myList;

   public MyAdapter(Context context, List<Item> myList) {
         // context is useful to use for create an inflater and getView method
         _context = context;  
         // check on list here 
         // instantiate new if need (if you plane to use method on list) 
         // to avoid null pointer exceptions 
        _myList = myList;
   }
       // implement rest methods methods for base adapter 
       //  getView() , id etc 
       // implement methods to add delete clear (on list) 


      public void addItem(Item item) {
         // do some chcks on list is not empty etc 
          _myList.add(item);
        // notify adapter observers to update their states
        notifyDatasetChanged();
      }
}

3) use adapter with list view:

  // create list to hold items 
  List<Items> listItems = new ArrayList<>()
  // create item 1
  Item item1 = new Item(String,int,int);
  // add item 1
  listItems.add(item1);
  Item item2 = new Item(String,int,int);
  listItems.add(item2)
  // create adapter 
  MyAdapter myAdapter = new MyAdapter(Context, listItems) 
  //set adapter 
  listView.setAdapter(myAdapter);

 // later you can re-user adapter 
 Adapter adapter = listView.getAdapter() 
 if(adapter !=- null && MyAdapter.class.isAssingableFromClass(adapter.getClass) {
     // do cast 
     MyAdapter myAdapter = MyAdapter(adapter);
     // add new item 
     myAdapter.addItem( new Item (...););
} 

full example:

https://github.com/c3ph3us/examples/blob/master/VaultListAdapter.java

other examples and infos :

ArrayAdapter is preferred over BaseAdapter if you just are loading a simple List of objects – cricket_007

do you mean use or extend ?

ArrayAdapter:

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

  1. i see no point & need to harness more resources here

  2. in my opinion using more complex classes / libs will not let you learn language - you should use low level approach to gain new skills