1

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

3 Answers3

0

You have to make all the reusable parameters must be globally. make ListView object and Adapter Class object and List data globally and don't re-assign with new constructor.Add this code above onCreate method and remove from re-assign.

ListView lview;
listviewAdapter adapter;

in onCreate method

lview = (ListView) findViewById(R.id.listview);
list = new ArrayList<HashMap>();
adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);

now add three parameter in populateList() method like

populateList(String item,String qty,String rate)

now add data from Button onClick event using

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

and also call notifyDataSetChange to refresh adapter and ListView, add this in last line of the populateList method

adapter.notifyDataSetChanged();
Rahul Mandaliya
  • 709
  • 1
  • 10
  • 35
  • i get NullPointerException in the line"adapter.notifyDataSetChanged();" –  Sep 03 '16 at 16:14
  • i did just like your idea but if i click the button no entries appearing. –  Sep 04 '16 at 02:30
  • just log list item values and count you can get more accurate information that is your data is added on list or not, than if yes then problem in AdapterClass not refreshing – Rahul Mandaliya Sep 05 '16 at 07:27
0

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

Community
  • 1
  • 1
ceph3us
  • 6,591
  • 2
  • 34
  • 39
  • ArrayAdapter is preferred over BaseAdapter if you just are loading a simple List of objects. And if you store the adapter as a field, that cast at the end is unnecessary – OneCricketeer Sep 03 '16 at 15:20
-1

i hope this will help you:

Do this in onCreate()

           cross = (ImageButton) findViewById(R.id.cross);

       lview = (ListView) findViewById(R.id.listview);
       lview.setItemsCanFocus(false);


       clicks();
       }

Then add this methods:

     private void populateList() {
       HashMap temp = new HashMap();
       list = new ArrayList<HashMap>();

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

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


   }

   private void populateList1() {
       HashMap temp1 = new HashMap();

       i++;
       sno = Integer.toString(i);
       item = etItem.getText().toString();
       qty = etQty.getText().toString();
       rate = etRate.getText().toString();


       temp1.put(FIRST_COLUMN, sno);
       temp1.put(SECOND_COLUMN, item);
       temp1.put(THIRD_COLUMN, qty);
       temp1.put(FOURTH_COLUMN, rate);
       list.add(temp1);
       adapter.notifyDataSetChanged();
   }

   public void clicks() {

       Button bAdd = (Button) findViewById(R.id.bAdd);
       bAdd.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (click == true) {
                   populateList();
                   adapter = new listviewAdapter(List.this, list);
                   lview.setAdapter(adapter);
                   etItem.setText("");
                   etQty.setText("");
                   etRate.setText("");
                   click = false;
               } else if (click == false) {
                   populateList1();
                   etItem.setText("");
                   etQty.setText("");
                   etRate.setText("");
               }
           }
       });

   }

Hope i helped you:)

William Willi
  • 184
  • 1
  • 1
  • 14