I have a RecyclerView with EditText. I am manually adding items to RecyclerView by searching and each row has a EditText and some TextViews. I know how to get TextView data, but the problem is to get EditText input data(number values). I can get first 5 edit text data (first 5 rows) but from the 5th position it gave me the 5th position EditText value.
Item : 7.50-15 10PR FM CEAT Quantity : 1
Item : 5.60-15 04PR K 511 CEAT Quantity : 2
Item : 2.75-18 04PR SECURA F85 CEAT (F) Quantity : 3
Item : 3.00-17 06PR SECURA ZOOM CEAT (R) Quantity : 4
Item : 6.50-14 10PR FM CEAT Quantity : 5
Item : 5.00-12 04PR GRIP MASTER CEAT Quantity : 5
Item : 4.00-08 06PR AUTO RAJA RPG Quantity : 5
Item : 9.00-20 14PR CEAT CLT LUG Quantity : 5
Item : 8.25-20 14PR TR PLUS SUNTRAC Quantity : 5
Item : 7.00-15 12PR FM CEAT Quantity : 5
I am using this method from main activity to get edit text data. I am using for loop.
private String getNumPicNumber(int i) {
if (myRecyclerView.findViewHolderForLayoutPosition(i) instanceof SelectItemAdapter.ItemHolder) {
SelectItemAdapter.ItemHolder childHolder = (SelectItemAdapter.ItemHolder) myRecyclerView.findViewHolderForLayoutPosition(i);
numberPickerNumber = childHolder.getQtyNumber();
}
return numberPickerNumber;
}
This is my adapter.
public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {
private List<String> itemsName, itemsQty, itemsPCode, itemPlant;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
//private List<DummyEntry> mDataset;
private String[] mDataset;
public ArrayList myItems = new ArrayList();
public SelectItemAdapter(Context context, String[] mDataset) {
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
itemsQty = new ArrayList<String>();
itemsPCode = new ArrayList<String>();
itemPlant = new ArrayList<String>();
this.mDataset = mDataset;
}
#Override
public SelectItemAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.custom_row_selected_item, parent, false);
return new ItemHolder(itemView, this, new MyCustomEditTextListener());
}
#Override
public void onBindViewHolder(SelectItemAdapter.ItemHolder holder, final int position) {
holder.setItemName(itemsName.get(position));
holder.setItemQty(itemsQty.get(position));
holder.setItemPCode(itemsPCode.get(position));
holder.setItemPlant(itemPlant.get(position));
holder.myCustomEditTextListener.updatePosition(position);
holder.numPicker.setText(mDataset[position]);
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return itemsName.size();
}
public Object getItemName(int position) {
return itemsName.get(position);
}
public Object getItemPCode(int position) {
return itemsPCode.get(position);
}
public Object getItemPlant(int position) {
return itemPlant.get(position);
}
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, String iQty, String iPCode, String iPlant) {
itemsName.add(location, iName);
itemsQty.add(location, iQty);
itemsPCode.add(location, iPCode);
itemPlant.add(location, iPlant);
notifyItemInserted(location);
}
public void remove(int location) {
if (location >= itemsName.size())
return;
itemsName.remove(location);
notifyItemRemoved(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SelectItemAdapter parent;
TextView textItemName, txtPCode, txtAvailableQty, txtTempQty, txtPlant;
Button bRemove;
EditText numPicker;
public MyCustomEditTextListener myCustomEditTextListener;
public ItemHolder(View itemView, SelectItemAdapter parent, MyCustomEditTextListener myCustomEditTextListener) {
super(itemView);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.txtProductName);
txtAvailableQty = (TextView) itemView.findViewById(R.id.txtAvailbleQty);
txtPCode = (TextView) itemView.findViewById(R.id.txtPCode);
txtPlant = (TextView) itemView.findViewById(R.id.txtPlant);
bRemove = (Button) itemView.findViewById(R.id.bRemove);
numPicker = (EditText) itemView.findViewById(R.id.numberPicker);
this.myCustomEditTextListener = myCustomEditTextListener;
this.numPicker.addTextChangedListener(myCustomEditTextListener);
bRemove.setOnClickListener(this);
}
public void setItemName(CharSequence name) {
textItemName.setText(name);
}
public void setItemQty(CharSequence name) {
txtAvailableQty.setText(name);
}
public void setItemPCode(CharSequence name) {
txtPCode.setText(name);
}
public void setItemPlant(CharSequence name) {
txtPlant.setText(name);
}
public String getQtyNumber() {
return numPicker.getText().toString();
}
public CharSequence getItemName() {
return textItemName.getText();
}
public CharSequence getItemPCode() {
return txtPCode.getText();
}
#Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if (listener != null) {
listener.onItemClick(this, getPosition());
}
}
}
private class MyCustomEditTextListener implements TextWatcher{
private int position;
public void updatePosition(int position) {
this.position = position;
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
mDataset[position] = charSequence.toString();
}
#Override
public void afterTextChanged(Editable editable) {
}
}
}
Is there any way to get all edit text data? Only showing values are coming in the screen.
Related
I want remove item from shopping cart.
I am getting following error:
Process: com.example.myapp, PID: 21655
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.remove(ArrayList.java:503)
at com.example.myapp.CartActivity.onCartItemDelete(CartActivity.java:191)
at com.example.myapp.Adapter.CartAdapter$CartViewHolder.onClick(CartAdapter.java:96)
My Adapter Code
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
private List<CoffeeOrder> listData = new ArrayList<>();
Context context;
private OnCartListener mOnCartListener;
public CartAdapter(Context context, List<CoffeeOrder> listData, OnCartListener onCartListener){
this.listData = listData;
this.context = context;
this.mOnCartListener = onCartListener;
}
#NonNull
#Override
public CartViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.cart_item , parent, false);
return new CartViewHolder(v,mOnCartListener);
}
#Override
public void onBindViewHolder(#NonNull CartViewHolder holder, int position) {
CoffeeOrder order = listData.get(position);
holder.txt_cart_name.setText( order.getProductName());
holder.txt_price.setText(order.getPrice() + " \u20ac");
TextDrawable drawable = TextDrawable.builder()
.buildRound(""+ listData.get(position).getQuantity(), Color.BLACK);
holder.img_cart_count.setImageDrawable(drawable);
Double double_total;
double_total = 0.00;
double_total += (Double.parseDouble(order.getPrice()))*(Double.parseDouble(order.getQuantity()));
//two decimal number
String total = String.format("%.2f", double_total);
holder.txt_price.setText(total + " \u20ac");
}
#Override
public int getItemCount() {
return listData.size();
}
public static class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txt_cart_name,txt_price;
public ImageView img_cart_count;
public ImageView itemDeleteBtn;
OnCartListener onCartListener;
public CartViewHolder(View itemView, OnCartListener onCartListener){
super(itemView);
txt_cart_name = itemView.findViewById(R.id.cart_item_name);
txt_price = itemView.findViewById(R.id.cart_item_price);
img_cart_count = itemView.findViewById(R.id.cart_item_count);
itemDeleteBtn = itemView.findViewById(R.id.btn_item_delete);
this.onCartListener = onCartListener;
itemDeleteBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onCartListener.onCartItemDelete(getAdapterPosition());
}
}
public interface OnCartListener{
void onCartItemDelete(int position);
}
}
My activity Method Code
#Override
public void onCartItemDelete(int position) {
listCart.remove(position);
}
Seems like you are removing an item from index 1 but your array is just 1 item long
what you have
[0]
what you want to delete
[0][1]
^
So try to change
#Override
public void onClick(View v) {
onCartListener.onCartItemDelete(getAdapterPosition());
}
to
#Override
public void onClick(View v) {
onCartListener.onCartItemDelete(getAdapterPosition()-1);
}
I am using name and description in RecyclerView.
But name and description has edittext where user can change the name,description and submit the entire fields.
Now question is how to get the entire fields of all items in the recyclerview android?
public static List<CityEvent> getData() {
List<CityEvent> list = new ArrayList<>();
list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
list.add(new CityEvent("Droidcon", "Droidcon in Berlin", CityEvent.EVENT_TYPE));
return list;
}
public class CityEvent {
public static final int EVENT_TYPE = 1;
private String mName;
private String mDescription;
private int mType;
public CityEvent(String name, String description, int type) {
this.mName = name;
this.mDescription = description;
this.mType = type;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
this.mDescription = description;
}
public int getType() {
return mType;
}
public void setType(int type) {
this.mType = type;
}
}
So I placed this in edittext fields.
public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<CityEvent> mList;
public DifferentRowAdapter(List<CityEvent> list) {
this.mList = list;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case CITY_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city, parent, false);
return new CityViewHolder(view);
case EVENT_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false);
return new EventViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
CityEvent object = mList.get(position);
if (object != null) {
switch (object.getType()) {
case EVENT_TYPE:
((EventViewHolder) holder).mTitle.setText(object.getName()); //in edittext i am using name
((EventViewHolder) holder).mDescription.setText(object.getDescription()); //in edittext i am using description.
break;
}
}
}
#Override
public int getItemCount() {
if (mList == null)
return 0;
return mList.size();
}
#Override
public int getItemViewType(int position) {
if (mList != null) {
CityEvent object = mList.get(position);
if (object != null) {
return object.getType();
}
}
return 0;
}
public static class EventViewHolder extends RecyclerView.ViewHolder {
private EditText mTitle;
private EditText mDescription;
public EventViewHolder(View itemView) {
super(itemView);
mTitle = (EditText) itemView.findViewById(R.id.titleTextView);
mDescription = (EditText) itemView.findViewById(R.id.descriptionTextView);
}
}
}
Now question is if i change the edittext fields how to get all items once again to store in array list?
Because i need to send this in array list as POST request parameters.
So how to get all items to send it?
//For eg: POST params:
"cityEvent": [
{
"type": "event"
"title":"london",
"description":"some events"
}
]
MainActivity:
//just pasting the main logic
DifferentRowAdapter adapter = new DifferentRowAdapter(getData());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
final RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(linearLayoutManager);
//this is imp
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);
I think what you need to do is listen for text change in each of your EditText fields for all ViewHolder objects. You can set the listeners either in onBindViewHolder() or in the constructor of ViewHolder class, in your case EventViewHolder. Then you need to have to ArrayList, one for title and one for description and set them with the initial values that you put into these fields. On any text change, you can update the value in the ArrayList by using TextWatcher's onTextChanged(). When you need to pass all the values, you just need to pass the two ArrayList objects I mentioned above.
This is how your onBindViewHolder() should look like after the line where you have used setText() for your fields. titleData and descriptionData are two ArrayList<String> objects and assumed to be initialized.
titleData.add(object.getName());
descriptionData.add(object.getDescription());
((EventViewHolder)holder).mTitle.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
titleData.remove(position);
titleData.add(position, s.toString());
}
((EventViewHolder)holder).mDescription.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
descriptionData.remove(position);
descriptionData.add(position, s.toString());
}
I have a RecyclerView in my app and I am adding items to it by searching manually like in a shopping cart. Each row has a name as well as a edit text which i can enter value. I am using an ArrayList to store data which enter in the edit text using TextWatcher. For example lets say I add two or three items(rows) to the RecyclerView and try to enter value not in to the first edit text in the first row, from the second or third item(row), I am getting an IndexOutOfBoundsException error. I tried lot of ways. Nothing work.
This is my Adapter class.
public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {
private List<String> itemsName, itemsQty, itemsPCode, itemPlant, _retData;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public ArrayList myItems = new ArrayList();
private Context context;
public SelectItemAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
itemsQty = new ArrayList<String>();
itemsPCode = new ArrayList<String>();
itemPlant = new ArrayList<String>();
_retData = new ArrayList<String>();
}
#Override
public SelectItemAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.custom_row_selected_item, parent, false);
return new ItemHolder(itemView, this);
}
#Override
public void onBindViewHolder(final SelectItemAdapter.ItemHolder holder, final int position) {
holder.setItemName(itemsName.get(position));
holder.setItemQty(itemsQty.get(position));
holder.setItemPCode(itemsPCode.get(position));
holder.setItemPlant(itemPlant.get(position));
holder.numPicker.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
_retData.add(position, arg0.toString());
}
});
}
#Override
public int getItemViewType(int position) {
return position;
}
public String retrieveData(int i) {
return _retData.get(i);
}
#Override
public int getItemCount() {
return itemsName.size();
}
public Object getItemName(int position) {
return itemsName.get(position);
}
public Object getItemQty(int position) {
return itemsQty.get(position);
}
public Object getItemPCode(int position) {
return itemsPCode.get(position);
}
public Object getItemPlant(int position) {
return itemPlant.get(position);
}
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, String iQty, String iPCode, String iPlant) {
itemsName.add(location, iName);
itemsQty.add(location, iQty);
itemsPCode.add(location, iPCode);
itemPlant.add(location, iPlant);
notifyItemInserted(location);
}
public void remove(int location) {
if (location >= itemsName.size())
return;
itemsName.remove(location);
notifyItemRemoved(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SelectItemAdapter parent;
TextView textItemName, txtPCode, txtAvailableQty, txtTempQty, txtPlant;
Button bRemove;
EditText numPicker;
public ItemHolder(View itemView, SelectItemAdapter parent) {
super(itemView);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.txtProductName);
txtAvailableQty = (TextView) itemView.findViewById(R.id.txtAvailbleQty);
txtPCode = (TextView) itemView.findViewById(R.id.txtPCode);
txtPlant = (TextView) itemView.findViewById(R.id.txtPlant);
bRemove = (Button) itemView.findViewById(R.id.bRemove);
numPicker = (EditText) itemView.findViewById(R.id.numberPicker);
bRemove.setOnClickListener(this);
}
public void setItemName(CharSequence name) {
textItemName.setText(name);
}
public void setItemQty(CharSequence name) {
txtAvailableQty.setText(name);
}
public void setItemPCode(CharSequence name) {
txtPCode.setText(name);
}
public void setItemPlant(CharSequence name) {
txtPlant.setText(name);
}
public String getQtyNumber() {
return numPicker.getText().toString();
}
public CharSequence getItemName() {
return textItemName.getText();
}
#Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if (listener != null) {
listener.onItemClick(this, getPosition());
}
}
}
public class RetItem {
public String _itemNumPic;
}
}
My logCat.
05-31 19:39:57.394 30412-30412/com.ceatkelanisrilanka.dushanmadushanka.ceat E/InputEventSender: Exception dispatching finished signal.
05-31 19:39:57.394 30412-30412/com.ceatkelanisrilanka.dushanmadushanka.ceat E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
05-31 19:39:57.397 30412-30412/com.ceatkelanisrilanka.dushanmadushanka.ceat E/MessageQueue-JNI: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.add(ArrayList.java:147)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.SelectItemAdapter$1.afterTextChanged(SelectItemAdapter.java:73)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7695)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9483)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:972)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:516)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
at android.widget.TextView.doKeyDown(TextView.java:5803)
at android.widget.TextView.onKeyDown(TextView.java:5616)
at android.view.KeyEvent.dispatch(KeyEvent.java:2619)
at android.view.View.dispatchKeyEvent(View.java:8411)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2361)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1709)
at android.app.Activity.dispatchKeyEvent(Activity.java:2702)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:241)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
at android.view.inputmethod.InputMethodManager
your variable _retData size at the begining is 0. when you try to add item in the second position it's out of its bounds.
You have to make sure that you list is at the same size as your recycler size.
In order to do so, you have to ways:
1. in your add function add:
_retData.add(location, new String());
in your remove function add:
_retData.remove(location);
2. the second way, which i think is better, is to create a class called MyItem for example, and let it have the fields: Name, Qty, Pcode, Plant, retData.
then, in your recycler create a List of MyItem, and then you want have to keep an eye on its size. it's also more convenient.
i have a RecyclerView which have some values now i want to add 2 sections one for favourites another for defaults i can do it manually like this :
i have ChannelsAdapter for holding the values :
public class ChannelsAdapter extends RecyclerView.Adapter<ChannelsAdapter.ChannelsViewHolder> implements Filterable {
private LayoutInflater inflater;
private Context context;
List<ChannelsInformation> data = Collections.emptyList();
private final List<ChannelsInformation> filteredChannelsList;
private final MultiSelector mMultiSelector = new MultiSelector();
ArrayList <String> selectedChannelName , selectedChannelID;
private HashMap<String, Boolean> map;
private TabFragment5 tabFragment5;
public ChannelsAdapter(Context context, List<ChannelsInformation> data){
inflater = LayoutInflater.from(context);
this.context = context;
this.data = data;
filteredChannelsList = data;
}
public void remove(int position){
data.remove(position);
notifyItemRemoved(position);
}
#Override
public ChannelsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rowView = inflater.inflate(R.layout.custom_channel_row, parent, false);
ChannelsViewHolder holder = new ChannelsViewHolder(rowView);
return holder;
}
#Override
public void onBindViewHolder(final ChannelsViewHolder holder, final int position) {
final ChannelsInformation current = data.get(position);
holder.CHANNELNAME.setText(current.channelName);
selectedChannelName = new ArrayList<String>();
selectedChannelID = new ArrayList<String>();
holder.mSolvedCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!selectedChannelID.contains(current.id)) {
holder.mSolvedCheckBox.setChecked(true);
selectedChannelName.add(current.channelName);
selectedChannelID.add(current.id);
} else {
holder.mSolvedCheckBox.setChecked(false);
selectedChannelName.remove(current.channelName);
selectedChannelID.remove(current.id);
}
}
});
}
#Override
public int getItemCount() {
return data.size();
}
#Override
public Filter getFilter() {
return new UserFilter(this ,filteredChannelsList);
}
private static class UserFilter extends Filter {
private final ChannelsAdapter adapter;
private final List<ChannelsInformation> originalList;
private final List<ChannelsInformation> filteredList;
private UserFilter(ChannelsAdapter adapter, List<ChannelsInformation> originalList) {
super();
this.adapter = adapter;
this.originalList = new ArrayList<>(originalList);
this.filteredList = new ArrayList<>();
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
filteredList.clear();
final FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(originalList);
} else {
final String filterPattern = constraint.toString().toLowerCase().trim();
for (final ChannelsInformation channel : originalList) {
if ( (channel.channelName != null && channel.channelName.toLowerCase().contains(filterPattern))
)
{
filteredList.add(channel);
}
}
}
results.values = filteredList;
results.count = filteredList.size();
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.filteredChannelsList.clear();
if ((ArrayList<ChannelsInformation>) results.values != null ) {
adapter.filteredChannelsList.addAll((ArrayList<ChannelsInformation>) results.values);
}
adapter.notifyDataSetChanged();
} }
class ChannelsViewHolder extends SwappingHolder implements View.OnClickListener {
TextView CHANNELNAME;
CheckBox mSolvedCheckBox;
public ChannelsViewHolder(View itemView) {
super(itemView , mMultiSelector);
mMultiSelector.setSelectable(true);
mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.selectedChannelCheckBox);
itemView.setOnClickListener(this);
CHANNELNAME = (TextView) itemView.findViewById(R.id.ChannelNameTxtView);
}
#Override
public void onClick(View v) {
Toast.makeText(context, ""+CHANNELNAME.getText() ,Toast.LENGTH_SHORT).show();
tabFragment5 = new TabFragment5();
tabFragment5.addFavoriteSectionToRecyclerView(); // here am calling this function which is throwing me error
}
}}
and i have an another adapter for sections SimpleSectionedRecyclerViewAdapter :
public class SimpleSectionedRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context mContext;
private static final int SECTION_TYPE = 0;
private boolean mValid = true;
private int mSectionResourceId;
private int mTextResourceId;
private LayoutInflater mLayoutInflater;
private RecyclerView.Adapter mBaseAdapter;
private SparseArray<Section> mSections = new SparseArray<Section>();
public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
RecyclerView.Adapter baseAdapter) {
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mSectionResourceId = sectionResourceId;
mTextResourceId = textResourceId;
mBaseAdapter = baseAdapter;
mContext = context;
mBaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
mValid = mBaseAdapter.getItemCount()>0;
notifyDataSetChanged();
}
#Override
public void onItemRangeChanged(int positionStart, int itemCount) {
mValid = mBaseAdapter.getItemCount()>0;
notifyItemRangeChanged(positionStart, itemCount);
}
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
mValid = mBaseAdapter.getItemCount()>0;
notifyItemRangeInserted(positionStart, itemCount);
}
#Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
mValid = mBaseAdapter.getItemCount()>0;
notifyItemRangeRemoved(positionStart, itemCount);
}
});
}
public static class SectionViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public SectionViewHolder(View view,int mTextResourceid) {
super(view);
title = (TextView) view.findViewById(mTextResourceid);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int typeView) {
if (typeView == SECTION_TYPE) {
final View view = LayoutInflater.from(mContext).inflate(mSectionResourceId, parent, false);
return new SectionViewHolder(view,mTextResourceId);
}else{
return mBaseAdapter.onCreateViewHolder(parent, typeView -1);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder sectionViewHolder, int position) {
if (isSectionHeaderPosition(position)) {
((SectionViewHolder)sectionViewHolder).title.setText(mSections.get(position).title);
}else{
mBaseAdapter.onBindViewHolder(sectionViewHolder,sectionedPositionToPosition(position));
}
}
#Override
public int getItemViewType(int position) {
return isSectionHeaderPosition(position)
? SECTION_TYPE
: mBaseAdapter.getItemViewType(sectionedPositionToPosition(position)) +1 ;
}
public static class Section {
int firstPosition;
int sectionedPosition;
CharSequence title;
public Section(int firstPosition, CharSequence title) {
this.firstPosition = firstPosition;
this.title = title;
}
public CharSequence getTitle() {
return title;
}
}
public void setSections(Section[] sections) {
mSections.clear();
Arrays.sort(sections, new Comparator<Section>() {
#Override
public int compare(Section o, Section o1) {
return (o.firstPosition == o1.firstPosition)
? 0
: ((o.firstPosition < o1.firstPosition) ? -1 : 1);
}
});
int offset = 0; // offset positions for the headers we're adding
for (Section section : sections) {
section.sectionedPosition = section.firstPosition + offset;
mSections.append(section.sectionedPosition, section);
++offset;
}
notifyDataSetChanged();
}
public int positionToSectionedPosition(int position) {
int offset = 0;
for (int i = 0; i < mSections.size(); i++) {
if (mSections.valueAt(i).firstPosition > position) {
break;
}
++offset;
}
return position + offset;
}
public int sectionedPositionToPosition(int sectionedPosition) {
if (isSectionHeaderPosition(sectionedPosition)) {
return RecyclerView.NO_POSITION;
}
int offset = 0;
for (int i = 0; i < mSections.size(); i++) {
if (mSections.valueAt(i).sectionedPosition > sectionedPosition) {
break;
}
--offset;
}
return sectionedPosition + offset;
}
public boolean isSectionHeaderPosition(int position) {
return mSections.get(position) != null;
}
#Override
public long getItemId(int position) {
return isSectionHeaderPosition(position)
? Integer.MAX_VALUE - mSections.indexOfKey(position)
: mBaseAdapter.getItemId(sectionedPositionToPosition(position));
}
#Override
public int getItemCount() {
return (mValid ? mBaseAdapter.getItemCount() + mSections.size() : 0);
}
}
and here is my Fragment where am adding values and sections TabFragment5 like this :
channelsAdapter = new ChannelsAdapter(getActivity(), getData()); // getting values
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
List<SimpleSectionedRecyclerViewAdapter.Section> sections =
new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>();
//Sections , first section by default
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "All Channels"));
//Add your adapter to the sectionAdapter
SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()];
SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new
SimpleSectionedRecyclerViewAdapter(getActivity(),R.layout.section,R.id.section_text,channelsAdapter);
mSectionedAdapter.setSections(sections.toArray(dummy));
recyclerView.setAdapter(mSectionedAdapter);
// well the above is working fine but this is 50% of what i want to accomplished , i want to add the an default section when user haven't set any value to favourite and once the user makes any value favourite i want to add the second section Favourites i don't know how can i do it dynamically on runtime , i tried this off course didn't worked i got an error :
public void addFavoriteSectionToRecyclerView(){
Toast.makeText(context, "Function Called" ,Toast.LENGTH_SHORT).show();
channelsAdapter = new ChannelsAdapter(getActivity(), getData());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
List<SimpleSectionedRecyclerViewAdapter.Section> sections =
new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>();
//Sections
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "Favorites")); // adding second Section dynamically
sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "All Channels"));
//Add your adapter to the sectionAdapter
SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()];
SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new
SimpleSectionedRecyclerViewAdapter(getActivity(),R.layout.section,R.id.section_text,channelsAdapter);
mSectionedAdapter.setSections(sections.toArray(dummy));
recyclerView.setAdapter(mSectionedAdapter);
}
my error :
03-08 13:50:41.397 4237-4237/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: pb.myPackage, PID: 4237
java.lang.NullPointerException
at android.view.LayoutInflater.from(LayoutInflater.java:211)
at pb.myPackage.ChannelsAdapter.<init>(ChannelsAdapter.java:52)
at pb.myPackage.TabFragment5.addFavoriteSectionToRecyclerView(TabFragment5.java:420)
at pb.myPackage.ChannelsAdapter$ChannelsViewHolder.onClick(ChannelsAdapter.java:236)
at android.view.View.performClick(View.java:4469)
at android.view.View$PerformClick.run(View.java:18468)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
any idea guys why am getting this error ? if yes then please point out the problem or if my approach for adding this section on runtime is not good enough then please suggest me a better approach, any help or guidance will be much appreciated and helpful for me , thanks
P.S. for adding this sections i followed this SimpleSectionedRecyclerViewAdapter
UPDATE:
please see the image for better understanding , i have a normal RecyclerView where i putted a section named as default . now by default there's only first section which is named as default now if user selects any value as favourite i want to add another section named as Favourites and move the selected value to favourites section
tabFragment5 = new TabFragment5();
tabFragment5.addFavoriteSectionToRecyclerView(); // here am calling this function which is throwing me error
If TabFragment5 is indeed a Fragment, then the above code builds a brand new TabFragment5 and then calls addFavoriteSectionToRecyclerView().
Reading between the lines, I'm assuming you want to call this method on the current instance of TabFragment5, and that you don't want to create a new one.
For that, the simplest approach I can recommend is to use an EventBus.
Create an "Event" class as per the EventBus documention, i.e.:
public class AddFavoriteSectionEvent {
public AddFavoriteSectionEvent();
}
Register your Fragment to listen for events:
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
Add a method to your Fragment to respond to these events:
#Subscribe
public void onAddFavoriteEvent(AddFavoriteSectionEvent event){
addFavoriteSectionToRecyclerView();
}
And replace the two lines causing your NPE with:
EventBus.getDefault().post(new AddFavoriteSectionEvent());
This should resolve your NPE at least.
I have a class called AdapterFilaProducto.java , class show certain information in the list.
This is my class
public class AdapterFilaProducto extends BaseAdapter {
protected Activity activity;
protected ArrayList<ItemFilaProducto> items;
protected ItemFilaProducto item;
protected String tipo;
protected Mascaras msk = new Mascaras();
public ImageButton btnImDerecha ;
public ImageButton btnImIzquierda;
public TextView producto;
private Venta contex;
TextView precio;
private BaseDataAdapter db;
private TextView precioCantidad;
public AdapterFilaProducto(Activity activity, ArrayList<ItemFilaProducto> items,String tipo) {
this.activity = activity;
this.items = items;
this.tipo = tipo;
}
public AdapterFilaProducto(Activity activity,Venta contex, ArrayList<ItemFilaProducto> items,String tipo) {
this.activity = activity;
this.items = items;
this.tipo = tipo;
this.contex=contex;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return items.get(position).getId();
}
public String getItemProducto(int position) {
return items.get(position).getProducto();
}
public String getItemCantidad(int position) {
return items.get(position).getCantidad();
}
public String getItemPercio(int position) {
return items.get(position).getPrecio();
}
public EditText getItemEdit(int position) {
return items.get(position).getEditTxt();
}
public View getView(int position, View convertView, ViewGroup parent)
{
db = new BaseDataAdapter(activity);
View vi=convertView;
final ItemFilaProducto item = items.get(position);
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.list_producto, null);
}
btnImDerecha = (ImageButton) vi.findViewById(R.id.BtnDerechaVenta);
btnImIzquierda = (ImageButton) vi.findViewById(R.id.BtnIzquierdaVenta);
TextView producto = (TextView) vi.findViewById(R.id.txtProductoVenta);
item.precio = (TextView) vi.findViewById(R.id.txtCantidadVenta);
item.edtxt = (EditText) vi.findViewById(R.id.editTxtListVenta);
producto.setText(item.getProducto());
Log.i("Precio",item.montoPrecio);
if(item.cantidad.equalsIgnoreCase("0")){
item.precio .setText(item.Precio);
}else
item.precio .setText(item.montoPrecio);
item.edtxt.setText(""+item.cantidad);
btnImDerecha.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt(item.edtxt.getText().toString());
i=i+1;
item.edtxt.setText(""+i);
}
});
btnImIzquierda.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int i = Integer.parseInt(item.edtxt.getText().toString());
if(0<i){
i=i-1;
item.edtxt.setText(""+i);
}
}
});
item.edtxt.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
db.open();
Cursor cur = db.obtenerProductosTipo(tipo,item.getIdPro());
if(cur.moveToFirst()){
int precio = cur.getInt(3);
int cantidad = Integer.parseInt(item.edtxt.getText().toString());
int monto = precio*cantidad;
if(0 < monto){
item.setPrecio(msk.mascaraMontoTxt(String.valueOf(monto)));
item.precio .setText(item.getPrecio());
}
db.actualizarProductoMonto(item.getIdPro(),monto);
db.actualizarProductoCantidad(item.getIdPro(),cantidad);
}
cur.close();
int montoTotal = 0;
Cursor curAll = db.obtenerProductosTipo(tipo);
if(curAll.moveToFirst()){
do{
montoTotal = montoTotal + curAll.getInt(5);
Log.e("CANTIDAD", ""+curAll.getInt(5));
}while(curAll.moveToNext());
}
curAll.close();
try{
db.borrarTablaPar("MONTO");
}catch(Exception e){
}
Log.e("MONTO", ""+montoTotal);
DtoParametro dtoParametro = new DtoParametro();
dtoParametro.ID_PAR = "MONTO";
dtoParametro.VALOR = String.valueOf(montoTotal);
Log.i("MONTO",""+String.valueOf(montoTotal));
db.insertarParametro(dtoParametro);
db.close();
contex.mostrarMonto();
}
});
return vi;
}
}
And I need to do is to show in my main Activity is montoTotal. and display it in a total amount in the Textview for my Principal Activity .We tried several methods but none succeeded.
I would appreciate your help, thanks.
Its really hard to follow your code but you should either be able to make this an inner class of your PrincipalActivity and make montoTotal a member variable so that you can access it from where you need or create an instance of this class that retrieves the variable from a function