Reputation: 113
Every time i add the new values from edit text the initial text disappears and recently added text will appears.Why this is happening? This is my adapter class.
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());
}
}
}}
This is Activity from where i send the Edit text field values on button click.
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();
}
});
This is the Builder.Activity where the items needs to be displayed.
@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);
The xml for Builder class is
<?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>
Upvotes: 1
Views: 90
Reputation: 1114
It is not what you think that items are not being replaced.But every time you click new instances are created so every-time you are seeing the new value or data.So you need to save the data you added somewhere.You have DataStorageOptionsInAndroid. SO you can go through any of the options to save the data.
Upvotes: 0
Reputation: 1342
You are creating and setting the adapter on onCreate. It will create a new instance always.
myRecyclerView.setAdapter(myRecyclerViewAdapter);
after this following manner should take place.
myRecyclerViewAdapter.add(0,nameList);
myRecyclerViewAdapter.add(1,nameList);
myRecyclerViewAdapter.add(2,nameList);
myRecyclerViewAdapter.add(3,nameList);
Try to change your logic. Don't pass the name list via string. Get all the names and send it to that activity and Get those values as String array or Get all names from that activity itself. Set that array to recycler view.
Upvotes: 1