My Simple Adapter Wont Populate my listview

女生的网名这么多〃 提交于 2019-12-11 23:48:05

问题


I am creating an android application and my code will loop through the json data and if finds a match to the string that i have placed in ( in this case "Guil Hernandez") , then it will add that name to an array list of hashmaps. I then populate my listview with a simple adapter. Everything is working properly, but my listview will not appear. Am i doing this sorting "algorithm" wrong? Also if you know of a better way to do the sorting to find a match..PLEASE LET ME KNOW. i am still new to this. Thank you in advance!

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

    }

}

full code here :

public class MyActivity extends 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:


If you want to compare strings use equals(). Like this:

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

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

     nameArrayList.add(hashMap);
}


来源:https://stackoverflow.com/questions/28400522/my-simple-adapter-wont-populate-my-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!