简体   繁体   中英

add data to listview from edittext another activity android

i want to ask..

i have 2 activity, just say form1 and form2, and i have a listview on form1. i want to add data from edittext in form2 with a button into listview in form 1..how can i do that?

if i use onResume() the user must change activity to form1 to get the listview refreshed..

i try to use this code but its seems didnt work..

if(form1.listviewname != null)
{
        ((ArrayAdapter)form1.listviewname.getAdapter()).notifyDataSetInvalidated();
}

thank you!!

------------------------------------------------------------edited----------

just now i tried to use this code on form2

list_list.add(myitem);
form1.listview.setAdapter(adapter1);                        
adapter1.notifyDataSetChanged();

myitem is string which get the value from textbox in form 2

its seems work but now the problem is everytime i add the data to listview in form1 its always overwrite the existing data in listview..

how can i fix this?

thank you!!

RESOLVED--------------------------------------------------- resolved by declaring this on form2

public static ArrayList<String> list_list = new ArrayList<String>(); 

public static ArrayAdapter<String> adapter1;

i added public static.

thank you!!!

- Suppose Activity A has the ListView and Activity B has the EditText with Button .

In Activity B :

Button mbutt = (Button)findViewById(R.id.button_mSubmit);
EditText mtext = (EditText)findViewById(R.id.editText_mData);

mbutt.setOnClickListener(new OnClickListener(){

   public void onClick(View v) {

                      Intent i = new Intent(B.this, A.class);

                      i.putExtra("text_key",mtext.getText().toString());

                      startActivity(i);
        }


});

In Activity A :

- Here in onResume(), do this

 Intent i = getIntent();

 String mdata = i.getExtras().getString("text_key");

- Now you have got the data in mdata variable, i hope you have used a static ArrayList in Activity A, to hold the data which in turn has to be fed into the ArrayAdater .

Eg:

public static ArrayList<String> aList = new ArrayList<String>();

- Now finally in the onResume() , you have to add this mdata variable's value into ArrayList and then fed it into the ArrayAdpater and then again set the ListView with this Adapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM