Send data from edittext to listview

拜拜、爱过 提交于 2019-12-12 21:30:11

问题


I have created two Activities (MainActivity, ListActivity) and in MainActivity, I have an EditText and a save-button.

In ListActivity, I have a ListView.

I want to save the value/data/string text that is written in EditText and send it to the other Activity in the ListView.

This is how it looks now, but I think I know what I'm missing but don't know how to do it.

MainActivity.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button save = (Button) findViewById(R.id.btnSave);
        final EditText et = (EditText) findViewById(R.id.editText);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ListActivity.class);
                intent.putExtra("data", et.getText().toString());
                startActivity(intent);
            }
        });
    }
}

And the ListActivity.

public class ListActivity extends Activity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ListView lv = (ListView) findViewById(R.id.listView);

        Intent intent = getIntent();

        String data = intent.getStringExtra("data");
    }
}

Now the text is saved in data-string, but how paste it in ListView?


回答1:


First of all you have to create a List Container, most used is the ArrayList, then you will have to create a ListAdapater which will inflate your data list to your ListView you can find a good article explaining it here : http://windrealm.org/tutorials/android/android-listview.php




回答2:


Here is a sample code to add things in a ListView :

listV = (ListView) findViewById(R.id.list);

ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;

while (...) {
    map = new HashMap<String, String>();
    map.put(stuff);
    listItem.add(map);
}

s = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list,
                       new String[] {"title", "desc"}, new int[] {R.id.title, R.id.desc});
listV.setAdapter(s);



回答3:


You should create ArrayAdapter<String> object and set it to your ListView:

List<String> listItems = new ArrayList<String>();
listItems.add(data);
String[] items = {};
items = listItems.toArray(new String[listItems.size()]);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));



回答4:


You can add your String from the TextView and then add that to an ArrayAdapter<String> and then set that as the Adapter for the ListView

Pseudo code:

TextView textView = (TextView) getViewById(...);
String s = textView.getText();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(...);
adapter.add(s);
ListView lv = (ListView) getViewById(...)
lv.setAdapter(adapter)

And if I understand your question correctly you want to first populate the ListView and then send that to another activity. If this is the case, don't try to do that, just create an ArrayList of the String(s) and pass that and then create the ListView in the activity that needs it.

EDIT:

To solve your problem you can use a Bundle to send an ArrayList<String> to your other activity then just build an ArrayAdapter similar to my above code to populate the ListView Something like:

 Intent intent = new Intent(getBaseContext(), YourActivity.class);
 intent.putExtra("ArrayListOfStrings", yourArrayList);

Then in your other Activity (YourActivity) you can do:

 Bundle extras = getIntent().getExtras();
 ArrayList<String> myList = extras.getStringArrayList("ArrayListOfStrings");


来源:https://stackoverflow.com/questions/25081383/send-data-from-edittext-to-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!