我的简单适配器不会填充我的列表视图

My Simple Adapter Wont Populate my listview

我正在创建一个 android 应用程序,我的代码将循环遍历 json 数据,如果找到与我放入的字符串的匹配项(在本例中为 "Guil Hernandez" ) ,然后它会将该名称添加到哈希图的数组列表中。然后我用一个简单的适配器填充我的列表视图。一切正常,但我的列表视图不会出现。我做这个排序 "algorithm" 错了吗?另外,如果您知道进行排序以找到匹配项的更好方法……请告诉我。我对此还是陌生的。提前致谢!

    private void handleResponse() {

    if (mNameDataJson == null ) {
       // TODO: handle error
    } else {
        try {
            JSONArray namesArray = mNameDataJson.getJSONArray("posts");
            ArrayList<HashMap<String , String> > nameArrayList = new ArrayList<HashMap<String, String>>();

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

                JSONObject unit = namesArray.getJSONObject(i);
                String name = unit.getString(KEY_NAME);
                name = Html.fromHtml(name).toString();
                String title = unit.getString(KEY_TITLE);
                title = Html.fromHtml(title).toString();

                HashMap<String , String> hashMap = new HashMap<String, String>();

                if (name == "Guil Hernandez") {

                    hashMap.put(KEY_NAME, name);
                    hashMap.put(KEY_TITLE, title);

                    nameArrayList.add(hashMap);
                } else {
                    Log.v(TAG , "no match");
                }

            }

            String[] keys = { KEY_NAME , KEY_TITLE };
            int[] ids = {android.R.id.text1 , android.R.id.text2};

            SimpleAdapter adapter = new SimpleAdapter(MyActivity.this , nameArrayList , android.R.layout.simple_list_item_2,
                    keys , ids);
            setListAdapter(adapter);



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

    }

}

完整代码在这里:

public class MyActivity 扩展了 ListActivity {

private JSONObject mNameDataJson;

private final String TAG = MyActivity.class.getSimpleName();

private final String KEY_NAME = "author";
private final String KEY_TITLE = "title";

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


    GetNameData getNameData = new GetNameData();
    getNameData.execute();


}

private void handleResponse() {

    if (mNameDataJson == null ) {
       // TODO: handle error
    } else {
        try {
            JSONArray namesArray = mNameDataJson.getJSONArray("posts");
            ArrayList<HashMap<String , String> > nameArrayList = new ArrayList<HashMap<String, String>>();

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

                JSONObject unit = namesArray.getJSONObject(i);
                String name = unit.getString(KEY_NAME);
                name = Html.fromHtml(name).toString();
                String title = unit.getString(KEY_TITLE);
                title = Html.fromHtml(title).toString();

                HashMap<String , String> hashMap = new HashMap<String, String>();

                if (name == "Guil Hernandez") {

                    hashMap.put(KEY_NAME, name);
                    hashMap.put(KEY_TITLE, title);

                    nameArrayList.add(hashMap);
                } else {
                    Log.v(TAG , "no match");
                }

            }

            String[] keys = { KEY_NAME , KEY_TITLE };
            int[] ids = {android.R.id.text1 , android.R.id.text2};

            SimpleAdapter adapter = new SimpleAdapter(MyActivity.this , nameArrayList , android.R.layout.simple_list_item_2,
                    keys , ids);
            setListAdapter(adapter);



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

    }

}


private class GetNameData extends AsyncTask<Object, Void, JSONObject> {
    JSONObject jsonResponse = null;

    @Override
    protected JSONObject doInBackground(Object... objects) {

        String nameUrl = "http://blog.teamtreehouse.com/api/get_recent_summary/?count=20";


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(nameUrl)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String responseString = response.body().string();
                Log.v(TAG , responseString);

                try {
                    jsonResponse = new JSONObject(responseString);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        return jsonResponse;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {

        mNameDataJson = jsonObject;
        handleResponse();


    }
}

}

如果要比较字符串,请使用 equals()。像这样:

if (name.equals("Guil Hernandez")) {

     hashMap.put(KEY_NAME, name);
     hashMap.put(KEY_TITLE, title);

     nameArrayList.add(hashMap);
}