未显示在Android列表视图中的Hashmaps的ArrayList

时间:2013-01-19 17:59:04

标签: android arraylist hashmap adapter

我正在尝试使用Hashmaps(String,Integer)的arraylist填充listview。我没有得到任何错误,但列表没有显示任何内容。我已经查看了与此相似的其他问题,但在这些情况下,他们的arraylists中没有任何数据。从logcat我可以看到列表存在但视图没有显示。知道如何解决这个问题吗?

代码:

public class JsonActivity extends ListActivity{

 private ProgressDialog progressDialog;

    // JSON Node names

    private static final String TAG_ID = "id";
    private static final String TAG_ARTISTNAME = "artistname";

 // chartItemList is the array list that holds the chart items 
    ArrayList<HashMap<String, Integer>> chartItemList = new ArrayList<HashMap<String, 
        Integer>>();
    JsonParser Parser = new JsonParser();
 // JSONArray
    JSONArray chartItems = null;

    /** Called when the activity is first created. */    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressdialog);

        //url from where the JSON has to be retrieved
        String url = "http://web.com/test.php";

        //Check if the user has a connection

        ConnectivityManager cm = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null) {
            if (!info.isConnected()) {
                Toast.makeText(this, "Please check your connection and try again.", 
             Toast.LENGTH_SHORT).show();
            }

            //if positive, fetch the articles in background
            else new getChartItems().execute(url);
        }

        //else show toast
        else {
            Toast.makeText(this, "Please check your connection and try again.", 
            Toast.LENGTH_SHORT).show();
        }

    }

    class getChartItems extends AsyncTask<String, String, String> {

        // Shows a progress dialog while executing background task
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(JsonActivity.this);
            progressDialog.setMessage("Loading chart...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        //Gets the json data for chart items data and presents it in a list view
        @Override
        protected String doInBackground(String... args) {
            String url = "http://web.com/test.php";
            String json = Parser.getJSONFromUrl(url);

            int id;

            String artistname;

            try{

            chartItems = new JSONArray(json);

            JSONObject json_data=null;

             for(int i=0;i<chartItems.length();i++){

                json_data = chartItems.getJSONObject(i);

                artistname=json_data.getString("artistname");
                id=json_data.getInt("id");


                 HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
                 // adding each child node to HashMap key => value
                    hashMap.put(artistname,id);

                 // adding HashMap to ArrayList
                    chartItemList.add(hashMap);

             }


              ;
            }

            catch (JSONException e) {
                e.printStackTrace();
            }

            runOnUiThread(new Runnable() {
                public void run() {
                     System.out.println(chartItemList);
                  //updating list view with the parsed items

                    ListAdapter adapter = new SimpleAdapter(JsonActivity.this, chartItemList,
                            R.layout.listview,
                            new String[] {TAG_ARTISTNAME,TAG_ID }, new int[] 

                         {R.id.artistname,R.id.id });
                    setListAdapter(adapter);
                }
            });
            return null;
        }

        //Removes the progress dialog when the data has been fetched
        protected void onPostExecute(String args) {
            progressDialog.dismiss();
        }
    }


 }

查看:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">  
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#eee">
    <!-- Name Label -->
    <TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff0a0a"
        android:textSize="16sp"
        android:textStyle="bold"
        android:paddingTop="6dip"
        android:paddingBottom="2dip" />
    <TextView
        android:id="@+id/artistname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="10sp"
        android:paddingTop="6dip"
        android:paddingBottom="2dip" />
  </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

更改此行:

// adding each child node to HashMap key => value
 hashMap.put(artistname,id);

// adding each child node to HashMap key => value
 hashMap.put(TAG_ARTISTNAME, artistname);
 hashMap.put(TAG_ID, id);

HashMap 只会将值存储在 key =&gt;值对中。