0

問題是每次當我從按保存按鈕發送edittext字段我認爲項目被重疊,或者它可能是我每次添加編輯文本字段舊值一個可能會被新值取代。他們沒有出現在列表中。編輯文本項目被重疊在recyclerview

這是從我發送編輯文本字段

public class MyEditor extends AppCompatActivity { 

    EditText textIn,txtHeading; 
    Button buttonAdd,btnsave; 

    ArrayList<String> nameList = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.editor); 
     txtHeading = (EditText)findViewById(R.id.heading); 
      buttonAdd = (Button)findViewById(R.id.add); 

     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.putStringArrayListExtra("key", nameList); 
       nameList.add(newName); 
       int listsize = nameList.size(); 
       for (int i=0;i<listsize;i++){ 
        Log.i("Lists are", nameList.get(i)) ; 
       } 

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


      } 
     }); 
    } 
} 

這是我的佈局,包含RecyclerView類。

<?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" 
    android:orientation="vertical" 
    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> 

這是我的Builder類,它包含Builder xml。

public class BuilderPage 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.builder_layout); 
    ButterKnife.bind(this); 
    setSupportActionBar(toolbar1); 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    dbHelper = new DbHelper(this); 
    dbHelper.getWritableDatabase(); 
    ArrayList<String> nameList = getIntent().getStringArrayListExtra("key"); 
    myRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_builderxml); 

    linearLayoutManager = 
      new LinearLayoutManager(this, 

    LinearLayoutManager.VERTICAL,false); 


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

這是我的適配器類

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 (null != itemsName ? itemsName.size() : 0); 

} 

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.herecomes); 
    } 

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

呼叫'myRecyclerView.setLayoutManager(linearLayoutManager);'在設置適配器之前。 – Piyush

+0

對不起,說這不能解決問題 –

+0

你能否添加一個截圖來說明你的問題? – everyman

回答

0

您正在使用相同的textitemname everytime.change

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

} 

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

} 
+0

抱歉說這不能解決問題 –