ListView getChildAt(i) gives null - android

I have a listview with with custom Layout. ListView items come from a separate xml document. The listview renders properly, now I want to access the textview and the editview which is in that listview, and I want to change the value of that dynamically. I tried it using list.getChildAt(i) but it gives me a null view. So how can I access the textviews?
ListView list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this,5,R.layout.layout_id);
list.setAdapter(adapter);
for (int j = 0; j < list.getCount(); j++) {
View v = list.getChildAt(j);
EditText text = (EditText) v.findViewById(R.id.textview1);
text.setText("Hello");
}
Ok I tried a lot and found that list.getChildAt(i) gives me value on onClickListener(). I want to get that value after the list render in onCreate() method. So how can i get the list value immediately of list render?

As you said you used a custom Layout, did you write your own Adapter? If so, you properly have to override the getChildAt-method. Hence you have to manage the model on your own.

Related

How to get sum of all Edittexts of Recyclerview items? Getting only items which are visible on screen, not all

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.

Get value from textview in ListView Android

I'm following this guide to create a listview with textviews and eddittexts in it.
http://www.webplusandroid.com/creating-listview-with-edittext-and-textwatcher-in-android/
When I try to get values from listview items, I get only textviews values. It seems impossible getting text from the edittext based on listview position.
I use lv.getItemAtPosition(0).toString()) to retrieve values
Following: Android: Access child views from a ListView
int wantedChild = 1;
View wantedView = listview.getChildAt(wantedChild);
mEditText = (EditText) wantedView.findViewById(R.id.edittext);
Log.d("result",(mEditText.getText().toString()));

How to set number of textview in horizontal scroll dynamically when data is coming from JSON parsing?

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.

How to divide gridview in sections in android?

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.

How to get the new value to the top position of the listview

How to get the new value to the top poistion of the listview when I entered the digit in a edittext.
Am getting the new value, which is given in the edittext,adding to the last position of the listview.
But I want the new value must be added to the top position of the listview.
Please can anyone help me in doing so?
If you are using ArrayList<String> arrayList = new ArrayList<String>(); then you can add the data to ArrayList at the 0 index and call notifyDataSetChanged(); to re-populate your ListView.
arrayList.add(0, "First");
just Reverse Your For Loop from size of ArrayList to 0.
Like This following Code ...
int j = ArrayList.size();
for (int i =j-1; i >= 0; i--)
You might want to add a button to the View containing the EditText and then when the user clicks that button get the value of the EditText and add it to the ListAdapter.
button.setOnClickListener(new OnClickListener(){
//Get the data from the edittext
String s = editText.getText().toString();
//Add it to your adapter
adapter.add(s);
//Refresh the adapter
adapter.notifyDataSetChanged()
});
Here are some links you might want to check out:
ArrayAdapter
EditText

Categories

Resources