如何传递一个活动的数据,然后在另一个活动的recyclerview中检索?

时间:2018-08-11 03:28:05

标签: java android android-intent android-recyclerview

我想将编辑后的文本值从一个活动传递到另一个活动,然后显示在recyclerView中。This is the errors I have

我是否必须将值放在等式标记下?但是我不知道如何在其中添加编辑后的文本值。

我想传递此类的数据

public class addEvent extends AppCompatActivity {

public void addEvent(View view){

    EditText timeEditText = findViewById(R.id.timeEditText);
    EditText descriptionEditText = findViewById(R.id.descriptionEditText);

    Intent intent = new Intent(getApplicationContext(), events.class);
    String timeInfo = timeEditText.getText().toString();
    String descriptionInfo = descriptionEditText.getText().toString();
    intent.putExtra("time", timeInfo );
    intent.putExtra("description", descriptionInfo);

}

public void events(View view){

    Intent intent1 = new Intent(getApplicationContext(), events.class);
    startActivity(intent1);
}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_event);
}
}

这是“查看活动”

public class events extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_events);

    recyclerView = findViewById(R.id.recyclerViewID);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    Bundle bundle = getIntent().getExtras();
    String time = bundle.getString("time");
    String description = bundle.getString("description");
    listItems = new ArrayList<>();

            listItems = new ArrayList<>();
            ListItem item = new ListItem(time, "d");

            listItems.add(item);





    adapter = new MyAdapter(this, listItems);
    recyclerView.setAdapter(adapter);


}
}

这是适配器类

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private Context context;
private List<ListItem> listitem;

public MyAdapter(Context context, List listitems){

    this.context = context;
    this.listitem = listitems;

}


@NonNull
@Override
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int i) {

    ListItem item = listitem.get(i);
    viewHolder.time.setText(item.getTime());
    viewHolder.description.setText(item.getDescription());

}

@Override
public int getItemCount() {
    return listitem.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView time;
    public TextView description;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        time = itemView.findViewById(R.id.time);
        description = itemView.findViewById(R.id.description);
    }
}
}

这是模型类

public class ListItem {

private String time;
private String description;

public ListItem(String time, String description) {

    this.time = time;
    this.description = description;

}


public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}


}

3 个答案:

答案 0 :(得分:0)

您必须在定义额外功能的同一范围内调用startActivity:

public void addEvent(View view){

    EditText timeEditText = findViewById(R.id.timeEditText);
    EditText descriptionEditText = findViewById(R.id.descriptionEditText);

    Intent intent = new Intent(getApplicationContext(), events.class);
    String timeInfo = timeEditText.getText().toString();
    String descriptionInfo = descriptionEditText.getText().toString();
    intent.putExtra("time", timeInfo );
    intent.putExtra("description", descriptionInfo);
    startActivity(intent);//add this


}

答案 1 :(得分:0)

在您的addEvent()方法中,

for i := range toInit{
        i := i
        go func(){
          toInit[i] = i; 
          syncLock <- true}()
 }

然后,在您的事件活动和onCreate()方法中,

public void addEvent(View view){
    //your code
        Intent intent = new Intent(addEvent.this, events.class);
        Bundle bundle = new Bundle();
        bundle.putString("time", timeInfo);
        bundle.putString("description",descriptionInfo);
        intent.putExtras(bundle);
        startActivity(intent);
}

这将完成您的工作。

答案 2 :(得分:0)

您要使用“ getStringExtra”,而不是“ getExtras / bundle”