user1411823
user1411823

Reputation: 25

Why isn't the data showing up on my listview?

It's connecting and pulling the data from the json array. Its adding two listview items but not showing any text?

JSON array {"tournaments":[{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":"Kansas","Category":"Roulette"},{"Title":"my title","Contact":"my contact","Description":"my description","City":"my city","State":"Connecticut","Category":"Three Card Poker"}]}

And some of my code in java...

    JSONObject json = JSONfunctions.getJSONfromURL("http://kleinefrosch.com/retrieve.php");

    try{

        JSONArray  tourneys = json.getJSONArray("tournaments");

        for(int i=0;i<tourneys.length();i++){                       
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = tourneys.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("Title:" , e.getString("Title"));
            map.put("Contact:" , e.getString("Contact"));
            map.put("Description:" , e.getString("Description"));
            map.put("City:" , e.getString("City"));
            map.put("State:" , e.getString("State"));
            map.put("Country:" , e.getString("Country"));
            map.put("Category:" , e.getString("Category"));

            mylist.add(map);            
        }       
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "Title","Contact","Description","City","State","Country","Category" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(JsonActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

        }
    });
}

}

Upvotes: 0

Views: 136

Answers (3)

Thamilvanan
Thamilvanan

Reputation: 385

Post your Xml code, are you using any Customized Listview? If it is means add some Dark color for Textview.

Upvotes: 0

Bhavin
Bhavin

Reputation: 6010

Try to remove map.put("id", String.valueOf(i));

Hope it runs then.

EDITED

Your Mistake is Here

map.put("Title:" , e.getString("Title")); 

it should be

map.put("Title" , e.getString("Title"));

As it is Key which will be Matched in SimpleAdapter. Like this remove : from each string value.

So please make sure that both the Keys match perfectly.

Upvotes: 1

Ricardo Simmus
Ricardo Simmus

Reputation: 334

Assuming that JSON string parsing is completed without any exceptions, I think that you should use this construction:

setListAdapter(new ArrayAdapter<String>(Context context, int resource, int textViewResourceId, List<T> objects);

For more info about go to developers guide

Upvotes: 1

Related Questions