2016-09-19 55 views
1

每次我从编辑文本添加新的值,初始文本消失,最近添加的文本会出现。为什么会发生这种情况?这是我的适配器类。RecyclerView添加项目,但替换最初的项目

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

的生成器类的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> 
+0

你在哪里调用下面的块“这是项目需要显示的Builder.Activity”。在一个方法或onCreate?指定。 –

+0

好吧我会编辑它 –

回答

0

这不是你认为物品没有被替换的原因。但是每次你点击新的实例时,你都会看到新的值或数据。所以你需要保存你添加到某个地方的数据。您有DataStorageOptionsInAndroid。所以你可以通过任何选项来保存数据。

1

您创建和设置上的onCreate适配器。它会一直创建一个新的实例。

myRecyclerView.setAdapter(myRecyclerViewAdapter); 

之后应该发生下列情况。

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

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

+0

加0,1这不解决问题。你可以分享你的逻辑如何实现。我是一个初学者。 –

+0

如何为应用程序获取“nameList”。让我知道你在哪里。我会进一步帮助你。 –

+0

ArrayList nameList = getIntent()。getStringArrayListExtra(“key”);这个我在Builder类中调用过 –