在下一个活动中将编辑文本内容传递给RecyclerView

时间:2016-09-08 06:13:28

标签: android android-layout android-recyclerview

我将EditText值从一个活动传递到另一个包含RecyclerView的活动。但是我遇到了一个对我来说无法解决的问题。

我的main.xml包含Edit text字段并添加Button。点击添加Button后,Edit Text值应传递到下一个XML中的RecyclerView

这是我的主要活动:

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity 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_main);
    myRecyclerView = (RecyclerView) findViewById(R.id.myrecyclerview);

    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();*/
            Intent i = new Intent(getApplicationContext(), ListFromMain.class);
            startActivity(i);
            /*myRecyclerViewAdapter.add(0,newName);
            Toast.makeText(MainActivity
                    .this,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();*/

        }
    });
}

}

我的Recycler适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {

private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;

public RecyclerViewAdapter(Context context){
    layoutInflater = LayoutInflater.from(context);
    itemsName = new ArrayList<String>();
}

@Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = layoutInflater.inflate(R.layout.text_view,parent, false);
    return new ItemHolder(itemView, this);
}

@Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
    holder.setItemName(itemsName.get(position));

}

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

public void setOnItemClickListener(OnItemClickListener listener){
    onItemClickListener = listener;
}

public OnItemClickListener getOnItemClickListener(){
    return onItemClickListener;
}

public interface OnItemClickListener{
    public void onItemClick(ItemHolder item, int position);
}

public void add(int location, String iName){
    itemsName.add(location, iName);
    notifyItemInserted(location);
}


public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private RecyclerViewAdapter parent;
    TextView textItemName;

    public ItemHolder(View itemView, RecyclerViewAdapter parent) {
        super(itemView);
        itemView.setOnClickListener(this);
        this.parent = parent;
        textItemName = (TextView) itemView.findViewById(R.id.textview);
    }

    public void setItemName(CharSequence name){
        textItemName.setText(name);
    }

    public CharSequence getItemName(){
        return textItemName.getText();
    }

    @Override
    public void onClick(View v) {
        final OnItemClickListener listener = parent.getOnItemClickListener();
        if(listener != null){
            listener.onItemClick(this, getPosition());
        }
    }
}}

ListActivity

public class ListFromMain extends ActionBarActivity implements RecyclerViewAdapter.OnItemClickListener{

private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
@Override
protected void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reycler_list);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.myrecyclerview);

    linearLayoutManager =
            new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

    myRecyclerViewAdapter = new RecyclerViewAdapter(this);

    myRecyclerView.setAdapter(myRecyclerViewAdapter);
    myRecyclerView.setLayoutManager(linearLayoutManager);


}
@Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
    Toast.makeText(this,
            "Remove " + position + " : " + item.getItemName(),
            Toast.LENGTH_SHORT).show();
}}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/namefield"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/addbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add"/>
</LinearLayout>

Recycler_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/myrecyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
 </LinearLayout>

textview xml

<?xml version="1.0" encoding="utf-8"?>
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview"/>

main的编辑文本值应显示在recycler_list中。

2 个答案:

答案 0 :(得分:1)

ListFromMain 中创建一个arraylist,如:

private ArrayList<String> itemsList;

并在 onCreate

itemsList = new ArrayList<>();
.
.
myRecyclerViewAdapter = new RecyclerViewAdapter(this, itemsName);

并在您的适配器内

public RecyclerViewAdapter(Context context, ArrayList<String> itemsList){
layoutInflater = LayoutInflater.from(context);
this.itemsName = itemsList;
}

现在,当您点击 MainActivity 中的 btnAdd 时,例如

btnAdd.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {

        String newName = nameField.getText().toString();
        Intent intent = new Intent(MainActivity.this, ListFromMain.class);
        intent.putExtra("item_name", newName);
        startActivity(intent);

    }
});

以及 ListFromMain 活动类的 onCreate

if(getIntent().hasExtra("item_name")) {
itemsList.add(getIntent().getStringExtra("item_name"));
myRecyclerViewAdapter.notifyDataSetChange();
}

希望它会帮助你。

答案 1 :(得分:1)

我已从此处删除了recyclerview代码,点击按钮,您将在下一个活动中传递名称列表。

public class MainActivity extends ActionBarActivity implements      
RecyclerViewAdapter.OnItemClickListener{

 EditText nameField;
 Button btnAdd;
 List<String> nameList;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   nameList = new Arraylist<>();

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();
        nameList.add(newName);
        Intent i = new Intent(getApplicationContext(),ListFromMain.class);
         intent.putStringArrayListExtra("key",nameList);
         startActivity(intent);
        Toast.makeText(MainActivity
                .this,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();

    }
});
 }

@Override
 public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
   Toast.makeText(this,
        "Remove " + position + " : " + item.getItemName(),
        Toast.LENGTH_SHORT).show();
}}

这里我们设置了recyclerview并从prvious Activity获取意图并在Adapter的构造函数中设置该列表。 ListActivity

     public class ListFromMain extends ActionBarActivity{
        private RecyclerView myRecyclerView;
        private LinearLayoutManager linearLayoutManager;
        private RecyclerViewAdapter myRecyclerViewAdapter;
     @Override
      protected void onCreate( Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.reycler_list);
        ArrayList<String> nameList = getIntent().getStringArrayListExtra("key");
          myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);

         linearLayoutManager =
        new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,     false);


          myRecyclerViewAdapter = new RecyclerViewAdapter(this,nameList);
          myRecyclerView.setAdapter(myRecyclerViewAdapter);
          myRecyclerView.setLayoutManager(linearLayoutManager);



   }
}

适配器没有太大的变化,只有一个是你在这里从构造函数初始化你的列表。 RecyclerAdaptger

    public class RecyclerViewAdapter extends     RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {

  private List<String> itemsName;
  private OnItemClickListener onItemClickListener;
  private LayoutInflater layoutInflater;

 public RecyclerViewAdapter(Context context,ArrayList<String> nameList){
     layoutInflater = LayoutInflater.from(context);
     itemsName = nameList;
  }

   @Override
    public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     View itemView = layoutInflater.inflate(R.layout.text_view,parent, false);
     return new ItemHolder(itemView, this);
  }

   @Override
    public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
       holder.setItemName(itemsName.get(position));

   }

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

    public void setOnItemClickListener(OnItemClickListener listener){
         onItemClickListener = listener;
    }

     public OnItemClickListener getOnItemClickListener(){
          return onItemClickListener;
      }

     public interface OnItemClickListener{
           public void onItemClick(ItemHolder item, int position);
    }

    public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

private RecyclerViewAdapter parent;
TextView textItemName;

public ItemHolder(View itemView, RecyclerViewAdapter parent) {
    super(itemView);
    itemView.setOnClickListener(this);
    this.parent = parent;
    textItemName = (TextView) itemView.findViewById(R.id.textview);
}

public void setItemName(CharSequence name){
    textItemName.setText(name);
}

public CharSequence getItemName(){
    return textItemName.getText();
}

@Override
public void onClick(View v) {
    final OnItemClickListener listener = parent.getOnItemClickListener();
    if(listener != null){
        listener.onItemClick(this, getPosition());
    }
}
  }}