Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView: How to add a row with a buttonclick

I have a listview. In it, each row had a text saying 0:00. But now, I added a button on my actionbar but then I got stuck. I don't know how to make the button create a new row, displaying 0:00

This is my code for the data in a row.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RowData rowdata_data[] = new RowData[]
            {                   
             new RowData("0:00")            
            };

    RowdataAdapter adapter = new RowdataAdapter(this, 
            R.layout.listview_item_row, rowdata_data);
    listView1.setAdapter(adapter);

    listView1 = (ListView)findViewById(R.id.listView1);
}

this is my RowData class:

public class RowData {
String title;

public RowData(String title) {                     
    this.title = title;
}
}

So how should I implement a button click to add another row? Under addtionbutton: should be the method.

public boolean onOptionsItemSelected(MenuItem item) 
{

    // Handle presses on the action bar items
    switch (item.getItemId()) 
    {
        case R.id.additionbutton:                      

            return true;           
        default:
            return super.onOptionsItemSelected(item);
    }
}
like image 210
user2554781 Avatar asked Mar 21 '23 09:03

user2554781


1 Answers

I don't know how to make the button create a new row

With your current solution it's not possible (in efficient way do it) because you're using static array of objects - it means array has fixed size (it means that you're assuming that size won't be changed at runtime).

But your goal is "slightly" different. You want after each click on Button increase number of objects by one. It means you don't know exactly how many rows you can have (or can be changed at runtime).

Due to things mentioned above you should (have to) use dynamic array with changeable size. For this reason you need to use as source of data List that is representation of dynamic array.

Basic algorithm:

  • Create new List with zero (or you can have by default one row at start).

  • Then create public method in your adapter that will add new item to collection and send request that Adapter should be refreshed (after you added new row into collection).

  • Assign OnClickListener to your Button and in onClick() method you'll use created method for adding new row into ListView.

Solution:

How to initialise ListAdapter:

// Activity-level variable scope
private List<RowData> items = new ArrayList<RowData>();
private RowdataAdapter adapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView1 = (ListView)findViewById(R.id.listView1);

    // adding first item to List, it's optional step
    items.add(new RowData("0:00"));

    adapter = new RowdataAdapter(this, R.layout.listview_item_row, items);
    listView1.setAdapter(adapter);
}

Method for adding new row into ListAdapter:

public void addRow(RowData newRow) {
   // items represents List<RowData> in your Adapter class
   this.items.add(newRow);

   // sends request to update ListAdapter
   notifyDataSetChanged();
}

How to update Adapter after click on Button:

button.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
      // add new row to Adapter
      adapter.addRow(new RowData("0:00"));
   }
});

Hope i helped you to solve your problem.

like image 93
Simon Dorociak Avatar answered Apr 27 '23 13:04

Simon Dorociak