我已经使用Intent来保存我要传递的数据..这是在登录页面..
String name = etUsername.getText().toString();
Intent intent = new Intent(getApplicationContext(), Posting.class);
intent.putExtra("myname", name);
此用户名我想通过点击发布类中的添加按钮将其从登录页面传递到我的RecyclerView布局:
点击AddButton,用户名应出现在每个CardView中。
这是具有AddButton的发布类:
public class Posting extends ActionBarActivity {
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
EditText nameField;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posting);
myRecyclerView = (RecyclerView) findViewById(R.id.myrecyclerview);
btnAdd = (Button) findViewById(R.id.addbutton);
Toolbar my_toolbars = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_toolbars);
getSupportActionBar().setTitle(R.string.title);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
myRecyclerViewAdapter = new RecyclerViewAdapter(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
nameField = (EditText) findViewById(R.id.namefield);
btnAdd = (Button) findViewById(R.id.addbutton);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newName = nameField.getText().toString();
if (!newName.equals("")) {
myRecyclerViewAdapter.add(0, newName);
TextView textView = (TextView) findViewById(R.id.display);
Intent intent = getIntent();
String name = intent.getStringExtra("myname");
textView.setText(name);
}
}
});
}
这是cardView的布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_margin="10dp"
card_view:cardCornerRadius="20sp"
card_view:cardElevation="5sp">
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="8pt"
android:text="i" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_margin="25dp"/>
<TextView
android:id="@+id/card_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="8pt"
android:layout_marginTop="30dp"/>
我想通过带有id =&#34;用户名&#34;的TextView将其从登录页面传递到此页面。
怎么办呢?
答案 0 :(得分:0)
//in posting Activity
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newName = nameField.getText().toString();
if (!newName.equals("")) {
myRecyclerViewAdapter = new RecyclerViewAdapter(this,newName);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
}
});
In RecyclerViewAdapter
//Create Constructor like below:
String name;
Activity context;
public RecyclerViewAdapter(Activity activity,String newName){
this.context=activity;
this.name= newName;
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
holder.username.setText(name);
}