我使用此代码将EditText
(名称)的内容保存到数组中,我希望然后在另一个活动的列表视图中显示此数组。任何的想法?谢谢
SingleItemView:
ArrayList<String> addArray = new ArrayList<String>();
Intent intent = new Intent(SingleItemView.this, mygardenMain.class);
Bundle bundle =new Bundle();
public void onClick(View v) {
String getInput = txt.getText().toString();
if (addArray.contains(getInput)){
Toast.makeText(SingleItemView.this,"Plant Already Added",Toast.LENGTH_SHORT).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(SingleItemView.this,"No Plant Selected",Toast.LENGTH_SHORT).show();
}
else
{
addArray.add(getInput);
Toast.makeText(SingleItemView.this,"Plant Added",Toast.LENGTH_SHORT).show();
bundle.putStringArrayList("addArray",addArray);
intent.putExtras(bundle);
startActivity(intent);
mygarden主要代码:
public class mygardenMain extends Activity {
private ListView list;
private ArrayList<String> addArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygarden_list);
list = (ListView) findViewById(R.id.mygardenlist);
Bundle bundle = getIntent().getExtras();
ArrayList<String> addArray = bundle.getStringArrayList("addArray");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mygardenMain.this, android.R.layout.simple_list_item_1, addArray);
list.setAdapter(adapter);
}
}
答案 0 :(得分:0)
使用以下代码执行此操作。将String数组从一个Activity发送到下一个Activity。
String[] stringArray = new String[10];
Intent intent = new Intent(MainActivity.this,MyDrawing.class);
Bundle bundle =new Bundle();
bundle.putStringArray("stringArray",stringArray);
intent.putExtras(bundle);
startActivity(intent);
并在下一个活动中从下一个活动中搜索数组,如下所示:
Bundle bundle = getIntent().getExtras();
String[] stringArray = bundle.getStringArray("stringArray");
您可以将Array List直接传递给Intent
ArrayList<String> arraylist = new ArrayList<String>();
Intent intent = new Intent(MainActivity.this, MyDrawing.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("arraylist",arraylist);
intent.putExtras(bundle);
startActivity(intent);
并检索如下:
Bundle bundle = getIntent().getExtras();
ArrayList<String> arraylist = bundle.getStringArrayList("arraylist");
答案 1 :(得分:0)
使用Parcelable在活动之间发送和接收数据。
发送
intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
startActivity(intent);
收到,
getIntent().getParcelableArrayListExtra("key");
最后设置了listview适配器。
答案 2 :(得分:0)
public class BasicViews5Activity extends ListActivity {
String[] presidents = {
“Dwight D. Eisenhower”,
“John F. Kennedy”,
“Lyndon B. Johnson”,
“Richard Nixon”,
“Gerald Ford”,
“Jimmy Carter”,
“Ronald Reagan”,
“George H. W. Bush”,
“Bill Clinton”,
“George W. Bush”,
“Barack Obama”
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---no need to call this---
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, presidents));
}
public void onListItemClick(
ListView parent, View v, int position, long id)
{
Toast.makeText(this,
“You have selected “ + presidents[position],
Toast.LENGTH_SHORT).show();
}
}
希望这会有所帮助
String {}总统是数组
答案 3 :(得分:0)
我认为这就是你想要的:
自: https://developer.android.com/training/basics/firstapp/starting-activity.html
建立意图 Intent是一个在不同组件之间提供运行时绑定的对象(例如两个活动)。 Intent代表一个应用程序&#34;意图做某事。&#34;您可以将意图用于各种任务,但在本课程中,您的意图将开始另一项活动。
在MainActivity.java中,将下面显示的代码添加到sendMessage():
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
这是你将android中的String传递给下一个活动的方法。 这可以适用于通过for循环在数组中添加一个或多个数组或对象或字符串。
答案 4 :(得分:0)
试试这个。
首先将ArrayList转换为数组,然后传递给下一个活动
String [] arrrayData = addArray.toArray(new String [addArray.size()]);
得到这样的
String[] array;
Intent intent = new Intent(MainActivity.this,MyDrawing.class);
Bundle bundle =new Bundle();
bundle.putStringArray("array",array);
intent.putExtras(bundle);
startActivity(intent);
100%正常工作
答案 5 :(得分:0)
请具体说明你的问题。其次ListView已经过时了,我建议你使用Recycler view
private RecyclerAdapter ra;
private ArrayList<ModalClassName> yourarraylistname;
recyclerView = (RecyclerView) findViewById(R.id.recyclerdown);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
yourarraylistname =new ArrayList<ModalClassName>();
//make sure yourarraylistname has some value
ra = new DownloadAdapter(this,yourarraylistname);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(ra);
答案 6 :(得分:0)
请在github上查看我的ListView示例项目。这是链接
https://github.com/SanjayKushwah0601/ListView-Example
您可以通过意图传递ArrayList
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.putStringArrayListExtra("list", addArray);
startActivity(intent);
然后你可以在另一个屏幕上获取这个ArrayList
// Now fetch the contents from intent
addArray = getIntent().getStringArrayListExtra("list");
ArrayAdapter<String> adapter = new ArrayAdapter<>(ListActivity.this, android.R.layout.simple_list_item_1, addArray);
list.setAdapter(adapter);