将JSON数组附加到列表视图

时间:2017-03-30 01:13:49

标签: java android json listview import

我是Java的新手,请原谅任何愚蠢或明显的错误。我有一个活动,从PHP文件中提取一个json编码的字符串,并将其放入一个简单的列表视图。我得到了可怕的红色波浪线,对我来说是一个神秘的错误信息。这是我的代码。我感谢任何协助。

package --Hidden--;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class tracklist extends Activity implements OnItemClickListener {
    private static final String URL = "http://rickthompson.com/json/fetchtracks.php";

    ListView lv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tracklist);

        lv = (ListView) findViewById(R.id.displayTrackList);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            R.layout.rowlayout, R.id.label, profileUserOptions);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

        Bundle bundle = getIntent().getExtras();
        final String selectedSubGenre = bundle.getString("option");


        /////////////  login script
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.names().get(0).equals("tracks")) {
                        Toast.makeText(getApplicationContext(), "tracks " +    jsonObject.getString("tracks"), Toast.LENGTH_LONG).show();

                        ///  save user id in prefs
                        JSONArray contacts =   jsonObject.getJSONArray("tracks");
                        HashMap<String, String> trackview = null;
                        for (int i = 0; i < contacts.length(); i++) {
                            JSONObject c = contacts.getJSONObject(i);
                            String isrc = c.getString("isrc");

                            trackview = new HashMap<>();
                            trackview.put("isrc", isrc);
                        }

                        trackview.toString();

                        lv = (ListView)  findViewById(R.id.displayTrackList);
                        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.rowlayout, R.id.label, trackview);
                        lv.setAdapter(adapter);
                        lv.setOnItemClickListener(this);
                    } else {
                        Toast.makeText(getApplicationContext(), "Error" + jsonObject.getString("error"), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> hashMap = new HashMap<>();
                SharedPreferences userPrefs = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                String userid = userPrefs.getString("memberid", "");

                hashMap.put("u", userid);
                Bundle bundle = getIntent().getExtras();
                final String selectedSubGenre = bundle.getString("option");
                hashMap.put("sg", selectedSubGenre);
                return hashMap;
            }
       };

       requestQueue.add(request);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), "something clicked", Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:0)

JSONObject jsonObject = new JSONObject(response);
 if(jsonObject.names().get(0).equals("tracks")){
//rest of the code
}

在上面的代码中,我希望你能得到&#34; name&#34;数组应该是

    JSONArray jso3 = new JSONArray (jsonObject.getString("names"));

请提供错误详情,以便我为您提供详细说明。