Get value from textview in ListView Android - 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()));

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 item Listview

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.

how to change the listview content only without changing list of items

I have a listview in which every listitem has one image and 4 textviews. I want to change only 2 textviews without changing the list and list of items.
Those textviews are changed every second.
Is it possible in android?
You can update the text items of listView like this:
private void updateText(int index){
View v = ListView.getChildAt(index);
TextView updatedText = (TextView) v.findViewById(R.id.txt_view);
updatedText .setText("changed Text");
}

ListView getChildAt(i) gives null

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.

android: Identify individual items in row of ListView?

I have a ListView in a ListActivity populated by a database table. Each row of the ListView is a RelativeLayout with three TextViews named rowid, date, and name in that order. I am able to select individual rows programmatically using the .setSelection(int position) method of the ListView.
Here is what I'm trying to do: When I push a button on the interface, get the rowid from the currently selected list row, and perform a db query using the rowid. I can't figure out how to get the rowid from the list itself. rowid may not be the same as the ID or position on the list as it is the rowid in the database.
I suspect this will require working with an adapter, but I've been trying/searching the web for a week and haven't been able to figure this out. Thanks for the help anyone can provide.
You know the list position of the currently selected item, you have a button outside the ListView that should trigger some action on that item, and you're not just making the ListView rows (or some child view within each row) clickable. Right?
You can get information from the list's adapter. getItem(int position) returns the object that is represented by the list item at position, so you can retrieve the information you need directly if it's stored in the object. getView(int position) returns the view for the row, allowing you to use findViewById(int id) to retrieve your TextView.
If you don't already have the adapter, you can get it from the ListView using getAdapter().
// ListView myListView = the ListView in question
// int selectedRow = the currently selected row in the ListView
// Each row in the ListView is backed by an object of type MyCustomDataClass
int dbRowId;
Adapter adapter = myListView.getAdapter();
MyCustomDataClass data = (MyCustomDataClass) adapter.getItem(selectedRow);
dbRowId = data.getDatabaseRowId();
// OR
dbRowId = data.rowId;
// OR whatever method the object has for getting the ID.
// OR
View listViewRow = adapter.getView(selectedRow);
TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid);
String dbRowAsString = dbRowView.getText().toString();
dbRowId = Integer.parseInt(dbRowAsString);
You might also consider whether it would be more natural for the user to just tap the ListView row, rather than selecting the row and then pressing another button. Reno's answer might work better.
I ended up using the last method with the following code.
int dbRowId;
Adapter adapter = myListView.getAdapter();
View listViewRow = adapter.getView(selectedRow);
TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid, null, null);
String dbRowAsString = dbRowView.getText().toString();
dbRowId = Integer.parseInt(dbRowAsString);
The only change I had to make was adding null, null to the parameters in .findViewByID
TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid, null, null);

Categories

Resources