Im looking to run a query on my external database, i got all the coding done up to this point. Although the coding works, I need it to put the “name” and the “cphone” in an array of some sort so i can add it to the custom list adapter as a double row.
”””””””””””””””””’
name
”””””””””””””””””’
cphone
”””””””””””””””””’
to this
”””””””””””””””””’
name cphone
”””””””””””””””””’
Log.d("RESULT", result);
try {
JSONArray jArray = new JSONArray(result);
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
String name = json_data.getString("username");
String cphone = json_data.getString("password");
items.add(name);
items.add(cphone);
Log.d(name, "Sever Output");
}
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(
getdata.this, R.layout.stashrow, items);
ListView sList = (ListView) findViewById(R.id.liststash);
sList.setAdapter(mArrayAdapter);
} catch (Exception e) {
Log.e("errorLog", "Error makingView: " + e.toString());
}
the problem stands in the item.add(), i can only put one value in at a time so it makes it 2 different lines. If I try adding another textview to the stashrow, it errors out. Is there a better way of doing this?
The basic ArrayAdapter isn’t configured to do very much. If you’re content with the simple formatting of
”””””””””””””””””’
name cphone
”””””””””””””””””’
Then you might as well just concatenate the strings together:
For anything more complicated then that, you’re better off defining your own data object (like a
Userfor instance, with agetName()andgetCPhone()) and then subclassing ArrayAdapter to translate the objects in the array into the well formatted ListView you really want – like so:Hope that helps!