[英]Send listView data from one activity to another
I'm trying to figure out how to get the data from a listView, store it in an Array, and be able to access that array of data in another activity. 我试图弄清楚如何从listView中获取数据,将其存储在Array中,并能够在另一个活动中访问该数据数组。 Would I do an Intent for this and pass it as an extra?
我会为此做一个意图并将其作为额外的一项传递吗? I've searched around but haven't gotten a clear answer.
我已经搜索了一下,但是没有一个明确的答案。 I want to be able to access that array of data so that I can randomly display it in another activity.
我希望能够访问该数据数组,以便可以在其他活动中随机显示它。
listView.java listView.java
public class ListView extends AppCompatActivity {
private static final String TAG = "ListView";
private EditText editText;
private android.widget.ListView listView;
DatabaseHelper mDatabaseHelper;
Button btnAdd;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
mDatabaseHelper = new DatabaseHelper(this);
btnAdd = (Button) findViewById(R.id.btnAdd);
editText = (EditText) findViewById(R.id.editText);
listView = (android.widget.ListView) findViewById(R.id.lv);
ArrayList<String> list = getIntent().getStringArrayListExtra("myList");
android.widget.ListView lv = (android.widget.ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
//Takes user back to the main activity after clicking on back arrow
ImageView ivBack = (ImageView) findViewById(R.id.ivBackArrow);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: pressed back arrow");
Intent intent = new Intent(ListView.this, MainActivity.class);
startActivity(intent);
}
});
//Adds new hashtag to list and prompts if nothing is entered
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newEntry = editText.getText().toString();
if (editText.length() != 0) {
addData(newEntry);
editText.setText("");
} else {
toastMessage("you must put something in the text field");
}
}
});
populateListView();
}
/**
* Adds new data into the Database
* @param newEntry
*/
public void addData(String newEntry) {
boolean insertData = mDatabaseHelper.addData(newEntry);
if (insertData) {
toastMessage("Successfully inserted");
recreate();
} else {
toastMessage("Whoops, something went wrong");
}
}
/**
* Default toastMessage
* @param message
*/
private void toastMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
/**
* Populate listView with data and create listener to navigate to editDeleteList
*/
private void populateListView() {
Log.d(TAG, "populateListView: displaying data in the listview");
//get data and append to list
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()) {
//get the value from the database in column 1
//set it to the arraylist
listData.add(data.getString(1));
}
//create arraylist and set it to the adapter
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
//set onclick listen to edit activity
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String name = adapterView.getItemAtPosition(position).toString();
Log.d(TAG, "onItemClick: you clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
int itemID = -1;
while (data.moveToNext()) {
itemID = data.getInt(0);
}
if (itemID > -1) {
Log.d(TAG, "onItemID: the ID is: " + itemID);
Intent editScreenIntent = new Intent(ListView.this, EditDeleteList.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
} else {
toastMessage("No ID found");
}
}
});
}
/**
* Updates the listView after navigating back from EditDeleteList activity
*/
@Override
protected void onResume() {
super.onResume();
populateListView();
}
}
Get data from listView: 从listView获取数据:
public static String[] getStringArray(ListAdapter adapter){ String[] a = new String[adapter.getCount()]; for (int i = 0; i < a.length; i++) a[i] = adapter.getItem(i).toString(); return a;}
String[] array = getStringArray(myListView.getAdapter());
Send array from activity to another one: 将数组从活动发送到另一个:
Intent intent = new Intent(context, Class.class); intent.putExtra("mylist", array);
Get it back within another activity: 在另一个活动中找回它:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
You can pass an ArrayList<E>
the same way, if the E
type is Serializable. 如果
E
类型是Serializable,则可以以相同的方式传递ArrayList<E>
。
I am assuming 1) You are modifying the ListView, 2) On press of button you want to go to another activity and have the data updated from listview in Activity2. 我假设1)您正在修改ListView,2)按下按钮您想转到另一个活动,并从Activity2中的listview更新数据。 From the ListView's adapter, you can create an interface (UpdateListListener) which will be implemented by the Activity1.
从ListView的适配器,您可以创建一个接口(UpdateListListener),该接口将由Activity1实现。 Whenever you press button(after editing) you just a)Call notifyDatasetChanged() and on the implemented method listener(onUpdatedList()) you just send the list to the other activity with intent.
每当您按下按钮(在编辑后)时,您只需a)调用notifyDatasetChanged(),然后在实现的方法listener(onUpdatedList())上,就可以将列表有意地发送到其他活动。 Since List is an object you need to send the intent as below:
由于列表是一个对象,因此您需要按以下方式发送意图:
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("myList", ListOfItems);
startActivity(intent);
Where ListOfItems implements Parcelable. ListOfItems实现Parcelable的地方。 You need to study how Parcelable works.
您需要研究Parcelable的工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.