Android ListView: how to add a row with a click button

I have a list. In it, each line had a text with the message 0:00. But now I added a button on my desktop, but then I got stuck. I don't know how to make the button create a new line 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 use a button to add another line? In addtionbutton: there must be a 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);
    }
}
+4
source share
2 answers

I do not know how to make the button create a new line

( ), - , ( , , ).

"" . , Button . , , ( ).

- () . List, .

:

  • ( ).

  • , ( ).

  • OnClickListener onClick() ListView.

:

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 optional step
    items.add(new RowData("0:00"));

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

ListAdapter:

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

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

:

button.setOnClickListener(new View.OnClickListener() {

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

, .

+6

, , , ,

, , - ,

adapter.notifyDatasetChanged();

+1

All Articles