Android使用GSON在listview中显示记录

时间:2014-02-03 07:51:13

标签: php android mysql

目前我正在研究谷歌的GSON。我按照this教程。它正在发挥作用。我现在要做的是在列表视图中显示记录。所以我没有使用自定义列表视图。

这是我到目前为止所尝试的:

private void handlePostsList(final List<Post> posts) {
    this.posts = posts;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for(Post post : PostsActivity.this.posts) {
                //Toast.makeText(PostsActivity.this, post.verse_title, Toast.LENGTH_SHORT).show();
                Log.d(TAG, "" + post.verse_title);

                //posts.addAll(post.verse_title);

                adapter = new ArrayAdapter<Post>(PostsActivity.this, 
                        android.R.layout.simple_list_item_1, posts);

                lv.setAdapter(adapter);

            }

        }
    });
}

private void failedLoadingPosts() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(PostsActivity.this, "Failed to load Posts. Have a look at LogCat.", 
                    Toast.LENGTH_SHORT).show();
        }
    });
}


private class PostFetcher extends AsyncTask<Void, Void, String> {
    private static final String TAG = "PostFetcher";
    public static final String SERVER_URL = "http://mlssabio.x10.mx/test/webservice-gson/test.php";
    //"http://kylewbanks.com/rest/posts";

    @Override
    protected String doInBackground(Void... params) {
        try {
            //Create an HTTP client
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(SERVER_URL);

            //Perform the request and check the status code
            HttpResponse response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();

                try {
                    //Read the server response and attempt to parse it as JSON
                    Reader reader = new InputStreamReader(content);

                    GsonBuilder gsonBuilder = new GsonBuilder();
                    //gsonBuilder.setDateFormat("M/d/yy hh:mm a");
                    gson = gsonBuilder.create();
                    List<Post> posts = new ArrayList<Post>();
                    posts = Arrays.asList(gson.fromJson(reader, Post[].class)); 


                    content.close();

                    handlePostsList(posts);
                } catch (Exception ex) {
                    Log.e(TAG, "Failed to parse JSON due to: " + ex);
                    failedLoadingPosts();
                }
            } else {
                Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode());
                failedLoadingPosts();
            }
        } catch(Exception ex) {
            Log.e(TAG, "Failed to send HTTP POST request due to: " + ex);
            failedLoadingPosts();
        }
        return null;
    } 

}

Post.class:

public class Post {

@SerializedName("id")
public long ID;
@SerializedName("verse_title")
public String verse_title;
public String verse_content;
public String lang;

public List tags;

public Post() {

}

}

但它显示的是com.r .....而不是标题。你能告诉我我做错了吗?感谢。

1 个答案:

答案 0 :(得分:1)

toString课程中实施Post并返回标题。

也没有必要

 adapter = new ArrayAdapter<Post>(PostsActivity.this, 
                    android.R.layout.simple_list_item_1, posts);
  lv.setAdapter(adapter);
在for循环中