在listView中添加一个新行

时间:2014-04-15 10:25:29

标签: android listview

我有一个listView,在listview中,我想在listView中添加一些新行。对于第一行,我想添加标题,而第一行的底部是sum和其他。

这是我的代码:

public class MyClaimActivity extends ListActivity {
    protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MyClaimActivity.class.getSimpleName();
protected JSONObject mBlogData;

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

    if (isNetworkAvailable()) {
        GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask(); 
        getBlogPostsTask.execute();
    }
    else {
        Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
    }
    }

    private boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager)  
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false; 
    if (networkInfo != null && networkInfo.isConnected()) { 
        isAvailable = true; 
    }
    return isAvailable; 
    }

    private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(Object... arg0) {
        int responseCode = -1;
        JSONObject jsonResponse =null;


        try {
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                Reader reader = new InputStreamReader(inputStream);
                int contentLength = connection.getContentLength();
                char[] charArray = new char[contentLength];
                reader.read(charArray);
                String responseData = new String(charArray);

                jsonResponse = new JSONObject(responseData);

            }
            else {
                Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
            }
        }
        catch (MalformedURLException e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        catch (IOException e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        catch (Exception e) {
            Log.e(TAG, "Exception caught: ", e);
        }

        return jsonResponse;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        // TODO Auto-generated method stub
        mBlogData = result;
        updateClass();
    }

    private void updateClass() {
        if (mBlogData==null) {
            //TODO: Handle error
        } else {
            try {
                JSONArray jsonPosts = mBlogData.getJSONArray("posts");
                mBlogPostTitles = new String[jsonPosts.length()];

                for(int i = 0 ; i<jsonPosts.length();i++)
                {
                    JSONObject post = jsonPosts.getJSONObject(i);
                    String eol = System.getProperty("line.separator");  
                    String titles = "Title :" +post.getString("title")+eol+"Tumbnail :"+post.getString("thumbnail");
                    titles = Html.fromHtml(titles).toString();
                    mBlogPostTitles[i] = titles;

                }
                ArrayAdapter<String> adapter= new ArrayAdapter<String>(MyClaimActivity.this, android.R.layout.simple_list_item_1,mBlogPostTitles);
                setListAdapter(adapter);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.d(TAG, "ketemu");
            }
        }

    }
}

代码:

JSONObject post = jsonPosts.getJSONObject(i);
                String eol = System.getProperty("line.separator");  
                String titles = "Title :" +post.getString("title")+eol+"Tumbnail :"+post.getString("thumbnail");
                titles = Html.fromHtml(titles).toString();
                mBlogPostTitles[i] = titles;

我想添加新行,使用eol,但它不能。我使用“\ n”,它仍然不能。我如何修复,在“标题”之后添加一个新行,并在换行符中显示Tumbnail。谢谢你的回答。

我的代码的结果是这样的,但是我想,每当我找到Tumbnail,它都会添加换行符: 喜欢 : 标题:xxx(换行符)。 TUMBNAIL:XXX。

enter image description here

0 个答案:

没有答案