[英]get value from object arraylist and show it in another activity
public class User implements Parcelable
{
private int Id;
private String Name;
private String Desg;
public User() {
super();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
dest.writeInt(Id);
dest.writeString(Desg);
}
private User(Parcel in){
this.Name = in.readString();
this.Id = in.readInt();
this.Desg = in.readString();
}
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
public int getId() {
return Id;
}
public void setId(int id) {
this.Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getDesg() {
return Desg;
}
public void setDesg(String desg) {
this.Desg = desg;
}
}
public class SecondActivity extends Activity {
ArrayList<User> userlist = new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Button btn = (Button)findViewById(R.id.add);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
EditText name = (EditText)findViewById(R.id.name);
EditText desg= (EditText)findViewById(R.id.desg);
EditText id = (EditText)findViewById(R.id.id);
User user = new User();
user.setName(name.getText().toString());
user.setDesg(desg.getText().toString());
user.setId(Integer.parseInt(id.getText().toString())); //in case if type is integer
if(userlist.contains(user))
{
Toast.makeText(getBaseContext(), "Already Added", Toast.LENGTH_SHORT).show();
}
else{
userlist.add(user);
}
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("userlist",userlist);
startActivity(intent);
finish();
}
});
}
public class MainActivity extends Activity {
ListView show;
ArrayList<User> myList= new ArrayList<User>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (ListView)findViewById(R.id.list);
myList= getIntent().getParcelableExtra("userlist");
ArrayAdapter<User> adapter = new ArrayAdapter<User>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myList);
show.setAdapter(adapter);
System.out.println(show);
show.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = ((CheckedTextView)view);
checkedTextView.setChecked(!checkedTextView.isChecked());
Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show();
}
});
}
public void onClick(View v)
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
I am storing the user input of different datatype in an arraylist and on button click I want to show that user input (arraylist) in a list view of checkboxes. 我将不同数据类型的用户输入存储在arraylist中,然后单击按钮以在复选框列表视图中显示该用户输入(arraylist)。 But this code is not working even it has no error.
但是,即使没有错误,此代码也不起作用。 anyone please tell me may be there is some logical issue here?
有人请告诉我这里可能存在一些逻辑问题吗?
You can use intent extras in passing values from one activity to another. 您可以使用Intent Extras将值从一个活动传递到另一个活动。 Here's an example of sending values to the target activity:
这是向目标活动发送值的示例:
Intent intent = new Intent(OriginActivity.this, TargetActivity.class); intent.putExtra("variableName", variableValue); startActivity(intent)
This sends the data "variableName" with its corresponding value to the target activity. 这会将数据“ variableName”及其对应的值发送到目标活动。 In order to retrieve the sent data, you must first get the instance of the intent in the target activity and then call a type-specific function to return the value.
为了检索发送的数据,您必须首先在目标活动中获取意图的实例,然后调用特定于类型的函数以返回该值。 You must also supply a default value in case the function cannot find the specified data or the data does not exist.
如果函数找不到指定的数据或数据不存在,还必须提供默认值。 If for example you want to retrieve an integer , you can do it this way:
例如,如果您想检索一个integer ,则可以这样进行:
int retrievedValue= getIntent().getIntExtra("variableName", defaultValue);
PS: You might want to make it a habit separating your activities, models, etc. in different class files. PS:您可能想养成在不同的类文件中分隔活动,模型等的习惯。
Best Way 最好的办法
Put 放
intent.putParcelableArrayListExtra("userlist" , userlist);
Retrieve 取回
myList= (ArrayList<User>) getIntent().getParcelableArrayListExtra("userlist");
Alternate Way 替代方式
You have to put individually each item of arraylist in intent extra and then retrieve it accordingly. 您必须分别将arraylist的每个项目放入意向中,然后相应地进行检索。
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
for (int i = 0; i<userlist.size(); i++)
intent.putExtra("userlist_"+i , userlist.get(i));
intent.putExtra("userlist_size" , userlist.size());
startActivity(intent);
finish();
Now to retrieve it. 现在检索它。
ArrayList<User> myList= new ArrayList<User>();
int size = getIntent().getIntExtra("userlist_size" , 0);
for(int i=0; i<size ; i++){
myList.add((User)(getIntent().getParcelableExtra("userlist_"+i)))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.