recycled edittext inside recyclerview - java

With RecyclerView, the off-screen EditTexts will be recycled. For example, if we have a list of 200 items, and it shows 2 items at one time, we will only ever have 2 EditText. They will reuse the higher EditText for the lower elements.
For example, here is a list that contains EditText showing only 2 at a time, and as the user scrolls, it will recycle and reuse them.
EditText A
Edittext B
EditText C (recycled)
EditText D (recycled)
....
This means we cannot just loop over all the elements later and get the values, as they don't store their values.
So, what i am asking is that is there anyway to loop over all the items and get there values even the recycled ones and save them to firebase
EDIT:
I have a recyclerview with an edit text inside it the user enter numbers inside edit text and when he click save button a for loop iterate over all the recyclerview edit text and save data to Firebase
this is my fragment class
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.purchases_list_activity, container, false);
mItem = new ArrayList<>();
mAdapter = new PurchasesAdapter(getContext(), mItem, this);
re = view.findViewById(R.id.itemsPListView);
re.setLayoutManager(new LinearLayoutManager(getActivity()));
re.setAdapter(mAdapter);
fab = view.findViewById(R.id.fab_purchases);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < mItem.size(); i++) {
final ItemsAdapter mylist = mItem.get(i);
View childView = re.getChildAt(i);
final EditText Qty = childView.findViewById(R.id.itemNewPcs);
final String qty = Qty.getText().toString();
Toast.makeText(getContext(),qty+"",Toast.LENGTH_LONG).show();
..
}

I think that what you might need is the RecyclerView's OnChildAttachStateChangeListener functionality. Basically, you implement your own class that implements the OnChildAttachStateChangeListener interface. Specifically, the implement the onChildViewAttachedToWindow method to do save to Firebase or add to a list that can be added to Firebase later - basically whatever you need. You just attach your custom listener via the addOnChildAttachStateChangeListener() method.
Hope that helps.

Related

change data in recycler view with click of button

So I am trying to create a challenge game where you can have 2 options to pick from, I have 2 card views setup in a recycler view setup, how would I change the values in them to get a new challenge with the click of a button from the main activity if a user wanted a new challenge?
I am fetching the data from each battle at random using an SQLite database.
My onBindViewHolder
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
SQLiteDatabase sqLiteDatabase = faceMashDatabaseHelper.getReadableDatabase();
cursor = sqLiteDatabase.query("images", new String[]{"id","filename","name"},null,null,null,null,null);
MyViewHolder myViewHolder = (MyViewHolder) holder;
int position1 = (int)(Math.random()*(faceMashDatabaseHelper.getCount()-0+1)+0);
cursor.moveToPosition(position1);
String filename = cursor.getString(cursor.getColumnIndex("filename"));
String name = cursor.getString(cursor.getColumnIndex("name"));
CardView cardView = myViewHolder.getCardView();
ImageView imageView = (ImageView) cardView.findViewById(R.id.imageView);
TextView nameView = (TextView) cardView.findViewById(R.id.nameView);
nameView.setText(name);
File file = context.getFileStreamPath(filename);
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageView.setImageBitmap(bitmap);
}
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = nameView.getText().toString();
listener.onClick(position,name);
}
}); //This on click is for the card views themselves.
}
My onClick at the current moment, I'm using a toolbar option
case R.id.newbattle:
faceMashAdapter.notifyDataSetChanged();
First of all, you should never do heavy work in onBindViewHolder since it will be called every time your View is recreated after it was recycled (because it was unused).
You should query your Database outside of the Adapter instead and occupy a List with your queried data. Your List should have as many entries as your RecyclerView has entries. Then you can use the position parameter in onBindViewHolder to always get the correct data for the entry (list.get(position)).
This way you can just change the content of your List and notify the Adapter which items have changed via notifyItemChanged (or if all items should be refreshed, use notifyDataSetChanged). That way the content of the Adapter is refreshed.
So what you should do in your onClick is update the content of your List and notify the Adapter.

How to add checkbox on all the listView items after having a long item click? Android Java

I want to add a feature on my project where if the user clicks one of the items on the listView then a checkbox would appear allowing the user to delete 1 or many items at once. Similar to Samsung Notes when you want to delete notes and or folders, etc. However, this concept is completely foreign to me and currently, I don't know where to begin to start this or what topic/resource/sample code I should look for. Also, I have a custom Array Adapter class that I used to order to work with my ListView but it came to my knowledge that you only need 1 array adapter class to make this work which made me confused since I don't know where to begin to manipulate it even further. Any help would be amazing!
Here is my Array Adapter that I have at the moment
//want to create our own custom ArrayAdapter. Going to extends the base class ArrayAdapter and hold our
//Word object
public class WordAdapter extends ArrayAdapter<WordFolder> {
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
return listItemView;
}
}```
In R.layout.folder_view add one and make it invisible or gone. OnLongClick make them Visible.

Change selected item's layout in ListView (Custom Adapter)

Is it possible to change layout of selected item in ListView?
I made simple app with ListView using Custom Adapter. It's working great, but I would like to change the layout of selected item to different layout.
In practice, the clicked item would expand it's height and some new buttons would appear. Once user deselects the items, it would go back to classic layout.
This is my Custom Adapter code
public class ListAdapter extends ArrayAdapter<Polozka> {
// context mem variable
private Context mContext;
// Konstruktory
public ListAdapter(Context context,int textViewResourceId){
super(context,textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Polozka> items){
super(context,resource,items);
this.mContext=context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null){
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.polozkanakupniseznam,null);
}
final Polozka p = getItem(position);
if (p != null){
final TextView tt1 = (TextView) v.findViewById(R.id.seznam_nazevPolozky);
TextView tt2 = (TextView) v.findViewById(R.id.seznam_pocetPolozky);
CheckBox cb1 = (CheckBox) v.findViewById(R.id.checkBox_koupeno);
Button btnPridat = (Button) v.findViewById(R.id.buttonPridat);
Button btnOdebrat = (Button) v.findViewById(R.id.buttonOdebrat);
if (tt1 != null){
tt1.setText(p.getNazev());
}
if (tt2 != null){
tt2.setText(Integer.toString(p.getPocet()));
}
return v;
}}
This is how I set adapter to my listview
lv = (ListView) findViewById(R.id.lv);
adapter = new ListAdapter(this,R.layout.polozkaseznam,list);
lv.setAdapter(adapter);
I figured out how to change layout of every item by creating new custom adapter and asigning it to the ListView, but that's sadly not what I need.
Thank you very much!
The getView() method of the adapter is used to create the original view. In there you can use the view's setOnTouchListener() method to add a View.OnTouchListener implementation. The onTouch() handler of that class receives the selected view.
A useful member to add to the OnTouchListener implementation is a GestureDetector; this is created with an OnGestureListener implementation to which you can pass the MotionEvent received in the OnTouchListener's onTouch() callback. This listener is then used to distinguish various events (single tap, double tap, etc); GestureDetector.SimpleOnGestureListener is good implementation to use with a number of callbacks handling such events you can override.
At some point (e.g., in the SimpleGestureListener's onDown() callback, which fires when an item is first touched), you can modify the layout of the view handed to the OnTouchListener. To change it back, you could either use a handler for the "on up" phase of a gesture, or, if you wish the item to remain as is until something else is selected, hold a reference to the last selected view and change it when another one is selected.
In case this last point is not clear, the idea is that the SimpleGestureListener is a member object of the view's OnTouchListener instance, therefore it can access the view belonging to the enclosing OnTouchListener.

Android Studio gridView

Is it possible to have methods and actual coding inside each item for a GridView?
The app that I am attempting to create is a currency converter, and I am currently displaying 3 images in the gridView: Euros, Pesos, and Rupees.
Once the user clicks on one, I want the open to open up a new XML which displays a textView. The user enters the value of US dollars in the textView and clicks the compute button. The app then displays the converted amount in the bottom of the screen.
The problem is that I am unable to figure out how to open up a new XML every time a picture is clicked on in the gridView. Assuming that I am able to do this, I am also unsure of where to place the code that goes behind the conversions. Would I make a new .java or just place is all in MainActivity.java?
Thanks.
What you might be best doing is when the user clicks on a currency it takes them to another activity where you would then load another xml for whatever you want to show.
In order to detect which item had been clicked you can implement an onItemClickListener for example
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//this assumes you give the gridview a list of currency which it then displays. Here we get the currency selected to then pass to our new activity
String selectedCurrency = myArrayOfCurrencies.get(position);
//then start new activity and give it the currency. This means we won't have to create an activity for each currency. You just need to create 1 and then based on what currency you give it will change the functionality
Intent intent = new Intent(this, Converter.class);
Intent.putExtra("currency", selectedCurrency);
startActivity(intent);
}
First you should be able to detect the clicks on each item of the GridView by calling the setOnItemClickListener() method.
If you set the clicklistener and you still can't detect the clicks, then most probably you need to add those attribtutes to your imageView in the xml
android:focusable="false"
android:focusableInTouchMode="false"
Second, once you are able to detect the clicks you can start new activity or add fragment that contains that edit text that will promote the user to enter the value.
Third, I would suggest to put the code responsible for the currency conversion in a class separately and create static methods that takes a value and convert it to the other curreny such as:
public class CurrencyConverter {
public static double convertToRupees (String currencyType, double currencyValue){
....
return currencyInRupees;
}
}
and by the way I would suggest you to use RecyclerView with grid layout manager instead of GridView.
I would create more classes.
You asked how to open a different XML file for each gridView item.
Create a custom adapter that extends BaseAdapter.
Override getView and for each view attach the right Xml file, according to the position.
For example:
YourActivity.java:
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(getApplicationContext());
}
MyAdapter.java:
...
#Override
public int getCount() {
return XmlArr.length;
}
#Override
public Object getItem(int position) {
return XmlArr[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Xml myXml = (Xml) getItem(position);
Holder holder;
if (convertView == null) {
// Set your view's layout. Consider using LayoutInflater.
// Use a static holder to prevent re-initialization:
holder = new Holder();
// holder.textView = ...
// holder.Xml = ...
// Or whatever you decided to have in each gridView item.
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.Xml = myXml;
...
return convertView;
}
static class Holder() {
TextView tv;
Xml xml;
...
}
I assumed you would used an Xml array (xmlArr).
Now you have option to play with each gridView item as you wish. You can set each view/button/textView an onItemClickListener, or you can also set the whole gridView an onItemClickListener (from YourActivity.java).
Hope this helps.

Android adding n text fields

I just started learning android programming and can't seem to work out how to permit the user to create a selected number (n) of EditText fields by clicking a button n times such that each would be uniquely identified, namely 1,2,3...,n as well as accessible programatically, from a different method (invoked by a different button click). I hope the question is clear enough as I don't really have that much code to provide.
Do something like this
// A list to keep reference to your created edit texts
List<EditText> mEditTexts = new ArrayList<EditText>();
// Get root view of your activity
ViewGroup viewGroup = (ViewGroup) ((ViewGroup)
findViewById(android.R.id.content)).getChildAt(0);
// Get the button and set a click listener to it
Button mButton = (Button) findViewById(R.id.button_id);
mButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// Build edit text
EditText mEditText = new EditText(v.getContext());
// Pass two args (arg1/arg2); must be LayoutParams.MATCH_PARENT,
// LayoutParams.WRAP_CONTENT, or an integer pixel value.
mEditText.setLayoutParams(new LayoutParams(arg1, arg2));
// Add the edit text to your list
mEditTexts.add(mEditText);
// Add edit text to your root view
viewGroup.addView(mEditText);
}
}
To check your edit text fields you can then access them from the list
for(EditText editText : mEditTexts){
Log.d(TAG, editText.getEditableText().toString());
}
or explicitly
int specificPosition = (SOME_INT);
EditText specificEditText = mEditTexts.get(specificPosition);
Haven't been able to test it so it might need some modifying but it should be something along those lines. You can also use your layout directly if you don't want to use the viewGroup. Modify it to something like
LinearLayout mLayout = (LinearLayout) findViewById(R.id.layout_id);
....
mLayout.addView(mEditText);

Categories