I'm trying to create an Array of TextViews that each get populated from an Array of objects, but each TextView ends up displaying the last object that I enter.
I've initiated the array of TextViews with
TextView[] loanViewDisplay = new TextView[5];
and inside of my onCreate, I have
loanViewDisplay[0] = findViewById(R.id.loanView1);
loanViewDisplay[1] = findViewById(R.id.loanView2);
loanViewDisplay[2] = findViewById(R.id.loanView3);
loanViewDisplay[3] = findViewById(R.id.loanView4);
loanViewDisplay[4] = findViewById(R.id.loanView5);
I have an addNewLoan button that initiates an Intent and startActivityForResult. The new loan gets added to a array called arrayOfLoans and the program returns to this activity where I want to list each loan from arrayOfLoans in its own TextView.
for(int i=0; i<listOfLoans.length; i++){
if(arrayOfLoans[i] != null){
loanViewDisplay[i].setText(arrayOfLoans[i].loanInformation());
}
}
If I add one loan with a principle of $5000, the output is perfect with
Loan 1: Principle $5000
but if I then click my addNewLoan button and add a second loan with a value of $2000, my output turns into
Loan 2: Principle $2000
Loan 2: Principle $2000
I know my data is being added to arrayOfLoans properly. I've narrowed the problem down to the array of TextViews. Thanks for the help
The problem is in your loop you don't need loop for display name in text view . What you need is just number at which your last position array was populated .
For that, set i=0 initially then use below code line .
if(arrayOfLoans[i] != null){
loanViewDisplay[i].setText(arrayOfLoans[i].loanInformation());
i++;
}
Related
I am developing an android application, in which I have Edit texts in adapter rows of Recyclerview. Now I want to get sum of these Edit text values entered by user in activity. I have used below code for this purpose:
int count = listArray.size();
int sum = 0;
for (int i = 0; i < count; i++) {
LinearLayout ll = (LinearLayout) mRv_products.getLayoutManager().findViewByPosition(i);
if (ll != null) {
Log.v("TAG", "Item considered:" + i);
EditText t = (EditText) ll.findViewById(R.id.edt_quantity_ship_now);
String amount = t.getText().toString().trim();
if (!amount.isEmpty())
sum += Float.parseFloat(amount);
}
}
Now the problem is, I am getting views only for items which are visible on screen, not all. For example, if I have 10 total items in Recyclerview and only 3 items are visible on screen, I am getting sum of only 3 items which are visible on screen.
So far I have used other options for getting Edit text:
mRv_products.findViewHolderForAdapterPosition(i);
and
mRv_products.getChildAt(i);
Firstly in your adapter class create a string Array list.
private ArrayList<String> list;
Initialize this list in your adapters constructor and also create its getter setter and then in your onBindViewHolder() method add the editext textchangelistener and in this listeners onchangedtext() method add the following code
list.set(position,edittext.getText().toString);
Now you can get all the edittext value through the getter then you combine all the values using string builder.
Here is what i would do:
I will add textWatcher for every editText in RecyclerView item.
then will declare a variable in recyclerView Item model
when any text changed in editText, store the value in that item model.
when sum need to be calculated, iterate the list of model & calculate the result.
Main theme of recyclerView of any adapterView is that your view may or may not exist but your item list always exist. So, always update & use your item list as per as your requirement. It will help you a lot.
Hope it will help you. Thanks in advance.
I am fetching the JSON data via URL and displaying it in text view inside the horizontal scroll view.
JSONArray ja = response.getJSONArray("results");
ArrayList<Details> myModelList = new ArrayList<Details>();
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
mymodel = new Details();
mymodel.id = Integer.parseInt(jsonObject.optString("id").toString());
mymodel.url = jsonObject.getString("resLink");
mymodel.resType = jsonObject.getString("resType");
mymodel.name = jsonObject.getString("resName");
myModelList.add(mymodel);
setData(myModelList);
I am showing my data in text view but it shows the first data only
private void setData(List<Details> mList) {
for (int i =0; i <=mymodel.getResType().length();i++)
{
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(mymodel.getResType());
}
You are initializing the mymodel in the for loop again and again so I think it contains only one value that too the last one(not the first one).
If that is the problem, try declaring the following outside the for loop.
mymodel = new Details();
So there's quite a few problems in your code:
1) You are always replacing the text of the same TextView. Normally with scrolling views you back them up with an adapter, place your data into the list that is then added to the adapter, then rely on the scrolling list to dynamically create the TextView which you can populate. If you have a known number of views in your scroll view and you want to avoid adapters, make sure they all have unique IDs so that you could find them.
2) Second problem is you're calling your setData inside the loop. Finish the loop first, then call you method (although you won't really have to do this if you do the adapter thing)
3) Your setData method is all sorts of wrong. If you want to pass a list to a method then iterate through it, you do it like this:
for (Details d : list) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(d.getResType());
}
But like I said, that probably won't help you on its own. Look into documentation for the scroll lists or recycler view and adapters. There are many examples available around.
I'm having a problem of getting item that are not visible in ListView. What I want is once the user has finished putting data in all EditText in a listview, I get all those values using a loop. But the issue is once the user has finished, with the loop I only get values visible on the screen and the rest the view returns null. any help please here is my code below. `
Log.e("size of:"," size listStudent(listview)--"+number);
for (int i=0;i<number;i++) {
View view = listStudent.getChildAt(i);
if(view !=null){
Log.e("size of:","counter"+i);
studentIdTxt = (TextView) view.findViewById(R.id.student_id_ls);
obtainedTxt = (EditText) view.findViewById(R.id.obtained);
maxTxt = (TextView) view.findViewById(R.id.max);
student = studentIdTxt.getText().toString().trim();
obtained_value = obtainedTxt.getText().toString().trim();
max_value = maxTxt.getText().toString().trim();
//updating the new mark list array
HashMap<String,String>studentMark=new HashMap<String,String>();
studentMark.put(TAG_STUDENT_ID,student);
studentMark.put(TAG_MARKS_OBTAINED,obtained_value);
studentMark.put(TAG_MARKS_MAX, max_value);
studentMarksList.add(studentMark);
}
}
//start of calling the JSON transmitter
JSONTransmitter transmitter=new JSONTransmitter();
//set the url
String url=domain+"/app/caller.php";
//transform studentMarksList to json
String studentList=gson.toJson(studentMarksList);
transmitter.setUrl(url);
Everything works fine, it's only that I can't get those item visible when you scroll. "number" is equal to 29 but the counter only iterates up to 4. Any help please!
listStudent.getChildAt(i);
A child view can only be retrieved from listview if it is visible on the screen.
Use a setOnFocusChangeListener for each EditText in the adapter (I assume you're using a custom adapter). When the user is done putting data, retrieve the text and update the list accordingly.
This is a tutorial if you need one.
My question is basic but I couldn't find any answer for my question. I have a layout which contains 2 textviews and a gridview. I added this layout into a listview.I want to generate multiple layouts. For example;
ListView------------------1stElement
----------textview1-----------------
----------gridview1-----------------
----------textview1-----------------
ListView------------------2ndElement
----------textview2-----------------
----------gridview2-----------------
----------textview2-----------------
ListView------------------3rdElement
----------textview3-----------------
----------gridview3-----------------
----------textview3-----------------
I tried it like that;
for(int i = 0; i<category.getChildren().size(); i++){
listView.setAdapter(new CategoryListAdapter(this, category.getChildren().get(i)));
}
But of course, it showed my last view only. setAdapter() doesn't suit to my code. I need something like adding views over and over.
Thanks for your helps.
You don't have to do this in for loop, Adapter Takes whole list Once Only, If u do it in a loop it will set the last list visible, So make list in For loop first and then add i to adapter only once.
for(int i=0;i<size;i++)
{
ArrayList<Object> myChldrenList = new ArrayList<Object>();
Object mychild = mycategory.getChildren().get(i)
myChildrenList.add(myChild);
}
listView.setAdapter(new CategoryListAdapter(this, myChildrenList));
u can use Ur Class type Instead of object class or simply Type Cast Object Class.
i'm using a custom expandable listview in my app and i`m stopped in one issue. I'm trying to insert in each group all the results from the query and for that i'm using 2 queries..one for the parent and another for the child.
So...the result from parent query returns a String[] with the values that i put normally on parent group list.
When i try to query the itens for the child..i use the parent result as parameter for get the values. The problem i`m having is that only the last item was list on the child row is showed.
I`m trying to put each result from String[parent number] on a Arraylist that i respective put on his parent Array...but only the last query was showed in all the parent group.
What i think is the queries are overwrite the previous one and adding this one instead of adding all. Searching on some topic i found something like Multidimensional array..is that what i have to use?
this is the part of the code that i stopped
//String[] with the result from the parent query
String[] teste = consulta.NomeArtista(busca);
//custom array for parent
ArrayList<ItemExpandable> ArrayItem = new ArrayList<ItemExpandable>();
//custom array for child
ArrayList<DataAdapter.ItemLista> ArraySubItem = null;
//using the parent size for create all the parent group
for (int i = 0; i < teste.length; i++) {
//Create a array with all the field belong to parent
ArraySubItem = consulta.consultaArtista(teste[i]);
ItemExpandable itens = new ItemExpandable();
itens.setTitle(teste[i]);
itens.setArrayFilhos(ArraySubItem);
Log.v(String.valueOf(ArraySubItem),"2");
ArrayItem.add(itens);
}
//My custom ExpListview with the 2 arraylist
listagem.setAdapter(new CustomBaseAdapter(artista.this.getActivity(), ArrayItem, ArraySubItem));