RecyclerView添加项目但替换初始项目

时间:2016-09-19 11:44:31

标签: android android-recyclerview recycler-adapter

每次我从编辑文本中添加新值时,初始文本都会消失,最近添加的文本将会出现。为什么会发生这种情况?这是我的适配器类。

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());
        }
    }
}}

这是我点击按钮时发送编辑文本字段值的活动。

    btnsave =(Button) findViewById(R.id.btn_save);
    btnsave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            String newName = txtHeading.getText().toString();

            Intent intent = new Intent(getApplicationContext(),BuilderPage.class);
            intent.putExtra("key",newName);

            startActivity(intent);
            Toast.makeText(MyEditor
                    .this,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();
        }
    });

这是需要显示项目的Builder.Activity。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.builder_layout);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar1);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    dbHelper = new DbHelper(this);
    dbHelper.getWritableDatabase();

    String nameList = getIntent().getStringExtra("key");

    myRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_builderxml);
    linearLayoutManager = new
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);

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

Builder类的xml是

   <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView_builderxml"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:animateLayoutChanges="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>



 <android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_builderxml"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" >

        <EditText
            android:layout_width="350dp"
            android:id="@+id/edittxtsurvey"
            android:layout_height="wrap_content"
            android:hint="Enter Your SurveyName"/>


        </android.support.v7.widget.Toolbar>

  </android.support.design.widget.AppBarLayout>


  <RelativeLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="bottom" >


    <SlidingDrawer android:layout_width="wrap_content"
        android:id="@+id/SlidingDrawer"
        android:handle="@+id/slideHandleButton"
        android:content="@+id/contentLayout"
        android:padding="10dip"
        android:layout_height="120dip">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/slideHandleButton"

            android:src="@drawable/arrowdown">

        </ImageView>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:background="#1a237e"
            android:id="@+id/contentLayout"
            android:gravity="center|top"
            android:padding="10dip"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/plaintext"
                android:id="@+id/imgviewplaintext"
                android:padding="5dp"
                android:onClick="oOnClick_PlainText"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/checkbox"
                android:id="@+id/imgviewcheckbox"
                android:onClick="OnClick_CheckBox"
                android:padding="5dp"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/imgviewradiobutton"
                android:layout_toStartOf="@+id/imgviewradiobutton"
                android:layout_marginRight="21dp"
                android:layout_marginEnd="21dp" />



            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/radio"
                android:id="@+id/imgviewradiobutton"
                android:padding="5dp"
                android:onClick="OnClick_RadioButton"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/imgviewtextbox"
                android:layout_toStartOf="@+id/imgviewtextbox"
                android:layout_marginRight="37dp"
                android:layout_marginEnd="37dp" />


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/textbox"
                android:id="@+id/imgviewtextbox"
                android:onClick="onClick_textbox"
                android:padding="5dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="30dp"
                android:layout_marginEnd="30dp" />
        </RelativeLayout>

    </SlidingDrawer>

   </RelativeLayout>
   </android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:1)

您正在onCreate上创建和设置适配器。它将始终创建一个新实例。

  myRecyclerView.setAdapter(myRecyclerViewAdapter);

应采取以下方式。

   myRecyclerViewAdapter.add(0,nameList);
   myRecyclerViewAdapter.add(1,nameList);
   myRecyclerViewAdapter.add(2,nameList);
   myRecyclerViewAdapter.add(3,nameList);

尝试改变你的逻辑。不要通过字符串传递名称列表。获取所有名称并将其发送到该活动并将这些值作为String数组获取或从该活动本身获取所有名称。将该数组设置为recycleler视图。

答案 1 :(得分:0)

不是你认为没有被替换的东西。但是每次你点击新实例都会被创建,所以你每次都看到新的值或数据。所以你需要保存你添加的数据。你需要有DataStorageOptionsInAndroid。因此,您可以通过任何选项来保存数据。