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

时间:2015-02-08 23:15:11

标签: java android json hashmap simpleadapter

我正在创建一个Android应用程序,我的代码将遍历json数据,如果找到与我放入的字符串匹配(在本例中为“Guil Hernandez”),那么它会将该名称添加到数组中哈希映射列表。然后我用一个简单的适配器填充我的listview。一切正常,但我的列表视图不会出现。我这样排序“算法”错了吗?此外,如果你知道一个更好的方法来进行排序找到匹配..请告诉我知道。我还是新手。提前谢谢!

    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();
        }

    }

}

完整代码:

公共类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();


    }
}

}

1 个答案:

答案 0 :(得分:0)

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

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

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

     nameArrayList.add(hashMap);
}