Get the ID of dynamic Views in android - android

I retrieve data from the internet. I add the views dynamically to a layout. I get the ID and the name :
for (int i = 0; i < productsList.size(); i++) {
TextView tv[] = new TextView[productsList.size()];
tv[i] = new TextView(LoveActivity.this);
tv[i].setText(String.valueOf(productsList.get(i).get("name")));
tv[i].setTextColor(R.color.black);
tv[i].setId(Integer.parseInt(productsList.get(i).get("id")));
ID = tv[i].getId();
tv[i].setOnClickListener(LoveActivity.this);
linearLayoutWithData.addView(tv[i]);
}
Now I want to set the ID of an added view to the ID which I get from the internet. To check this I use:
ID = tv[i].getId();
But ID become always the size of the Array..
Thanks for replying :-)

In every Iteration of your loop the variable ID gets overwritten. So the ID you receive after the loop is finished is the ID of the last view you added.

Related

How to read Text on any view of any application using AccessibilityInfo?

I am trying to read every item inside a ListView of any application by using AccessibilityInfo class. The problem is that i am getting the total number of items inside the ListView by using the following piece of code:
if(event.getClassName().equals("android.widget.ListView")) {
String className= TextView.class.getName();
AccessibilityNodeInfo nodeInfo= event.getSource();
int i= nodeInfo.getChildCount(); }
But i am not getting the names of items inside the ListView , for example let there are 5 textviews inside one ListView then i want to get the text from that TextViews. I tried to further break the nodeInfo object by using:
for(int j=0; j<nodeInfo.getChildCount() ;j++){
AccessibilityNodeInfo subChild = nodeInfo.getChild(j);
CharSequence subText = subChild.getText();
Log.e("MyService: ","ListView child name: "+subText);
}
So what i am doing wrong in getting the values of TextView's inside ListView ,
or
it is not possible to do so?
I have found the solution by myself and the solution is:
event.getSource() method returns instance of AccessibilityNodeInfo and by converting that instance value to String format we can easily get the results. Just we need is to break the string and get the required result out, in my case the way i did is:
String get_cord= String.valueOf(event.getSource());
String[] first_split= get_cord.split("[;]");
for(int i=0; i<first_split.length; i++){
if(first_split[i].contains("text")){
String[] second_split= first_split[i].split(":");
user= second_split[1].trim();
System.out.println(user);
Log.e("MyService: ","username is: "+user);
break;
}
}`

how to clear the ArrayList after adding that ArrayList to HashMap in Android?

Am doing one small android app. In that i have Expandable listview and am trying to populate data's from SQLite. So that i used HashMap to store parent(String) and child(ArrayList).
For eg I have four integer no's in parent(1, 2, 3, 4) i get this and stored in ListArray. Now i need to get child(ListArray) for that particular parent. Parent 1 contain (aa, bb), parent 2 contain (cc).. etc, now see this code
try
{
header = new ArrayList<String>();
footer = new ArrayList<String>();
child= new HashMap<String, List<String>>();
String date = null;
int count=0, countchild=0;
Cursor c = db.rawQuery("SELECT Date FROM Table", null);
if (c.moveToFirst())
{
do
{
date=c.getString(c.getColumnIndex("Date"));
header.add(date);
count=count+1;
} while (c.moveToNext());
}
for (int i = 0; i < count; i++)
{
String temp=header.get(i);
Cursor cc=db.rawQuery("SELECT Id FROM Table WHERE Date ='"+ temp +"' ", null);
if(cc.moveToFirst())
{
do
{
countchild=countchild+1;
String id=cc.getString(cc.getColumnIndex("Id"));
//footer.clear(); // it clear all data's on child
footer.add(id);
Log.d("Footer date count", "" + countchild);
child.put(temp, footer);
}
while (cc.moveToNext());
//footer.clear(); // it clear all data's on child
}
footer.clear(); // it clear all data's on child
}
}
I want to clear footer here after adding it to child then only i will get proper Expandable listview. If i remove/comment this footer.clear() line then all child's are added to all parents. eg parent 1 contain (aa, bb, cc, etc) and parent 2 contain (aa, bb, cc, etc). If i leave this footer.clear() line then all child's are cleared and display only parent like this eg Parent 1(), Parent 2()... etc
How to clear this footer(ListArray) after adding it to HashMap ?? or tell me some suggestion to modify this code.
Thank you. Srihari
When you put the ArrayList to the HashMap, the HashMap actually saves a reference to your array, not a copy.
That's why you must use different arrays for each group:
for (int i = 0; i < count; i++)
{
List<String> children = new ArrayList<String>();
String temp = header.get(i);
Cursor cc = db.rawQuery("SELECT Id FROM Table WHERE Date ='"+ temp +"' ", null);
if(cc.moveToFirst())
{
countChild += cc.getCount();
Log.d("Footer date count", "" + countchild);
do
{
String id = cc.getString(cc.getColumnIndex("Id"));
children.add(id);
child.put(temp, children);
}
while (cc.moveToNext());
}
}
As you can see a new children ArrayList here is created for each group and it's never cleared as that would clear the values from HashMap also.
Furthermore I've let myself fix a few other things like using Cursor's getCount method, to get the count instead of looping. Also I'm not using the footer array as it's unnecessary in the code you've shown.
Lastly, please use understandable and meaningful names for your variables as it can help you as well as others who read your code.
Remember Java is OO so if you clear an instance of an object, in this case an ArrayList, every other object referencing to it will get that new value.
What I recommend is you create a new instance of your footer variable everytime you need to fill a new group of your expandable list view, that way every footer list instance you create will be "associated" with one group in your list.
Hope this helps. :)

Store multiple dynamic EditText value

I have a code adding multiple EditText. How can i store them into a array. This number 10 is just example, the number may bigger than that. How can i store it after click a button
for(int i=0; i<10; i++) {
String store[] = new String[10];
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
}
In your example the store variable isn't actually being used, did you intend do use it for storing the EditTexts?
Instead of using an array of String, just use an array of EditText and store a reference to them:
EditText store[] = new EditText[10];
for(int i=0; i<10; i++) {
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
store[i] = addAnsMCQ; //store a reference in the array to the EditText created
}
Then outside of the for loop, you can access the reference to each EditText, e.g.
store[0].setWidth(300);
You need to keep/get a reference to each of your EditText's then you can look up its value with .getText().toString() which you can store in whatever manner you like.
However if as you say
This number 10 is just example, the number may bigger than that.
If the number is going to be larger you should be using an Adapter and a ListView or something to hold your View objects. That will make it easier to get everything on to the screen. And will give you the benefit of view recycling.

How to Use String as a ResourceID in Android

I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.
Simply I want to iterate ids by using pre-string values.
Psuedo example:
String pre_value = "textView";
for(int i = 0; i < size; i++) {
String usable_resource_id = pre_value + Integer.toString(pre_value);
// So how to use these id like R.id.textView1
// Cast or something similar
}
Any suggestions?
You can use Resources#getIdentifier() to get the identifier from a string.
But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

submitting data to an array to be pulled later

So I've got the following:
NodeList nodeList = element.getElementsByTagName("rowset");
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Element entry = (Element) nodeList.item(i);
Element _titleE = (Element) entry.getElementsByTagName("row").item(0);
Node _title = _titleE.getAttributes().getNamedItem("name");
t1.setText(_title.getNodeValue());
}
}
I've got the following XML layout:
<row name="" characterID="" corporationName="" corporationID="" />
(couple of lines of these)
the ideal way would be to create an array right? and then call the data from the array?
EDIT:
What I'm trying to do is read an XML File and store the values so that they can be accessed later so I'm assuming the ideal way would be to use an array?
(as my girlfriends name is jenny, too, I will be guessing what you want)
If you just want to store one value, an array or a ArrayList is good for that. If you need to store all 4 given attributes of your row, you should think about creating a class (lets call it MyRow) that contains those values. Than you can put all your rows into one ArrayList with the type of your class.
Pseudocode:
ArrayList<MyRow> myRowList = new ArrayList<MyRow>();
while reading each row
MyRow row = new MyRow();
row.mName = getAttributes().getNamesItem("name");
row.mCharacterId = getAttributes().getNamesItem("characterID");
// more setting...
}
A last tip for the next time: take some time to explain and specify your next question. That will improve the answers you get as well.

Categories

Resources