- Forum-Beiträge: 20
03.05.2015, 14:35:06 via Website
03.05.2015 14:35:06 via Website
Bin ieder al Ratlos, ich kann ja über "mCommentList.get(position)"
auf den Teil von mmCommentList zugreifen.
Nur dieser beinhaltet spenden und username...
wie bekomme ich jetzt nur den usernamen?
hoffe ihr könnt mir Helfen ich komme leider nicht weiter...
public void updateJSONdata() {
// Instantiate the arraylist to contain all the JSON data.
// we are going to use a bunch of key-value pairs, referring
// to the json element name, and the content, for example,
// message it the tag, and "I'm awesome" as the content..
mCommentList = new ArrayList<HashMap<String, String>>();
// Bro, it's time to power up the J parser
JSONParser jParser = new JSONParser();
// Feed the beast our comments url, and it spits us
// back a JSON object. Boo-yeah Jerome.
JSONObject json = jParser.getJSONFromUrl(READ_SPENDEN_URL);
// when parsing JSON stuff, we should probably
// try to catch any exceptions:
try {
// I know I said we would check if "Posts were Avail." (success==1)
// before we tried to read the individual posts, but I lied...
// mComments will tell us how many "posts" or comments are
// available
mComments = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i <= mComments.length(); i++) {
String username;
String spenden;
if (i==0){
username = "Spieler";
spenden = "Spendensoll";
}
else {
JSONObject c = mComments.getJSONObject(i - 1);
// gets the content of each tag
username = c.getString(TAG_USERNAME);
spenden = c.getString(TAG_SPENDEN);
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_USERNAME, username);
map.put(TAG_SPENDEN, spenden);
// adding HashList to ArrayList
mCommentList.add(map);
// annndddd, our JSON data is up to date same with our array
// list
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
final ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_spenden, new String[] { TAG_USERNAME,
TAG_SPENDEN }, new int[] {R.id.username,
R.id.spenden });
// I shouldn't have to comment on this one:
setListAdapter(adapter);
// Optional: when the user clicks a list item we
//could do something. However, we will choose
//to do nothing...
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(parent.getPositionForView(view)){
case 0:{
Log.e("User1", mCommentList.get(position).toString());
break;
}
case 1:{
Log.e("User2", mCommentList.get(position).toString());
break;
}
case 2:{
Log.e("User3", mCommentList.get(position).toString());
break;
}
case 3:{
Log.e("User4", mCommentList.get(position).toString());
break;
}
}
// This method is triggered if an item is click within our
// list. For our example we won't be using this, but
// it is useful to know in real life applications.
}
});
}
— geändert am 03.05.2015, 22:07:53
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.