submitting data to an array to be pulled later - android

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.

Related

RecyclerView which display and return different Values

I am displaying list of check-boxes in horizontal RecyclerView.
It display values such as {"Rd" , "Gr" , "Yl"} but when user selects any of this value it should return {"RED" , "GREEN" , "YELLOW")
How can I bind these two value that show and return differently?
I am taking display values from R.string-arry
I created another string-array of actual values, and when user checked any of checkboxes I get that ID and replaced it with actual values.
For e.g. If user has selected "Gr" I get ID=1 then replaced with actual string-array
But this only works when code-color and original-color are in order. In my app I sometimes use Red,green,blue or sometimes green,yellow,blue. So, this won't help me.
From what I understood, you need a mapping between GR and Green, RD and Red etc.
You can try using a Hashmap.
HashMap<String,String> colourMap = new HashMap<>();
colourMap.put("GR","GREEN");
colourMap.put("RD","RED");
And then you can retrieve the respective value for your colour code:
String colour = colourMap.get("GR");
You can use a HashMap which will bind the two arrays as key value pair
public HashMap<String,String> bindColors() {
HashMap<String,String> map = new HashMap<>();
int length = orginalColors.length;
for (int i = 0; i < length; i++) {
map.put(orginalColors[i], codeColors[i]);
}
return map;
}
And for getting the Colors in code.
HashMap<String, String> keyPair = bindColors();
orginalColorsNewArray = keyPair.keySet().toArray(new String[keyPair.keySet().size()]);
codeColorsNewArray = keyPair.values().toArray(new String[keyPair.values().size()]);
or use .get() function
keyPair.get("YELLOW")
Now it will be easier for you to access the codes by id/position
Hope this helps.

Wrong arrangement of data from 'for loop'

I have the following code. I added my text fields dynamically. My desired result shown in Genymotion 5.0 (Google Nexus 5) but when I run my app in other devices/actual device the textfields get shuffled. Please help, Thanks in advance.
JSONObject jsonObject = new JSONObject(question.getSublabels());
final EditText[] editTextSublabels = new EditText[jsonObject.length()];
for (int i = 0; i < jsonObject.length(); i++) {
String names = jsonObject.names().get(i).toString();
editTextSublabels[i] = (EditText) LayoutInflater.from(activity).inflate(R.layout.sublabels, null);
editTextSublabels[i].setId(i);
editTextSublabels[i].setHint(jsonObject.getString(names));
sublabelsContainers.addView(editTextSublabels[i], params);
}
You cannot and should not rely on the ordering of elements within a JSON object.
In JSON, an object is defined thus:
An object is an unordered set of name/value pairs.
If you want order to be preserved, you need to redefine your data structure or put it inside a jsonarray
see http://www.json.org.
A JSONObject is a type of map. It does not preserve ordering. If you want to preserve ordering using JSON, you will need to use an array (and matching JSONArray in Java).

Array not printing what it should be - android

When I try print the values to a textfield they come out as [Ljava.lang.String;#405af10
try {
JSONObject jObject = new JSONObject(byteArrayOutputStream.toString());
JSONArray Monday = jObject.getJSONArray("Monday");
String time = "";
String module = "";
String lecturer = "";
listAdapter = new ArrayAdapter<String[]>(this, R.layout.simplerow);
for (int i = 0; i < Monday.length(); i++)
{
time = Monday.getJSONObject(i).getString("time");
module = Monday.getJSONObject(i).getString("module");
lecturer = Monday.getJSONObject(i).getString("lecturer");
listAdapter.add(new String[] {time, module, lecturer});
}
mainListView.setAdapter(listAdapter);
} catch (Exception e) {
e.printStackTrace();
}
in the simplerow xml file I have
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
So each time a time, module or lecturer is printed it should be in it's own textview box., but it's not working and I don't know why or how to fix it.
It is most likely because your ArrayAdapter is set to accept in a String array (String []).
listAdapter = new ArrayAdapter<String[]>(this, R.layout.simplerow); //Parameterized for String []
Therefore you have a list of String arrays, which is basically equivalent to a 2D array.
This means, when the ArrayAdapter is setting the Text to the TextView you specified, it is calling toString() on the whole array object, hence the hashcode.
Consider making your own ArrayAdapter and override the getView() method so that it does what you want to do. There are plenty of tutorials online, such as this one.
I suggest that you make a POJO to hold your data. The POJO can have a name such as CourseInfo which clearly defines its meaning. Similarly, the members of CourseInfo will clearly define what they represent. This is preferred to an array because you can clearly define the meaning of each piece of data. Typically, Java programmers frown upon using an array which holds heterogenous data. In this case, even though the type of each piece of data is the same, the meaning of each element of the array differs.
This solution has the advantage that you can change
listAdapter = new ArrayAdapter<String[]>(this, R.layout.simplerow);
to
listAdapter = new ArrayAdapter<CourseInfo>(this, R.layout.simplerow);

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.

For-loop not working the way i want it to

I have a extremely minor issue that I can't seem to figure out. I'm trying to extract data based on a type of value from an ArrayList> and place it into another ArrayList. The issue is that the for-loop only runs once, which in this case i need it to traverse the entire array and then place the data into the unSuppressedData arraylist.
Below is the for-loop:
for (int x = 0; x < suppressedStatus.length; x++) {
for (int i = 0; i < availData.size(); i++) {
Hashtable<String,String> checkAvail = availData.get(i);
String itemStatus = checkAvail.get("loanStatus");
if (unSuppressedData.contains(checkAvail) == false) {
if (!(itemStatus.equals(suppressedStatus[x]))) {
Log.d("Item Status", itemStatus);
Log.d("Suppressed Status", suppressedStatus[x]);
unSuppressedData.add(checkAvail);
//break;
}
}
}
}
suppressedStatus is a String array
availData is the arraylist i want to extract data from
unSuppressedData is the arraylist i want to place the data in
I believe that it only runs once is due to this line of code:
if (unSuppressedData.contains(checkAvail) == false) {
But i need to this line to check whether my unSuppressdData has the data, if no then will add the data from availData arraylist into unSuppressedData arraylist.
Could it be that i'm writing this piece of code wrongly? Appreciate any insights shed on this.
A good collection type for this sort of thing is the LinkedHashSet. Because it's a set, each element can only be added once. Being a hash, the contains test is quick. Being 'linked' the resulting set is iterated in insertion order.

Categories

Resources