如果必须从从JSON解析的url加载图像,如何在listview中设置像背景一样的图像?

时间:2015-02-12 09:38:40

标签: android json listview

我有一个程序从服务器解析JSON文件,并对其对象进行列表查看。我需要为列表的每个元素创建背景图像(甚至是标题附近的缩略图),并且必须从URL下载此图像。

private static String url = "my url here";

private static final String TAG_NAME = "name";
private static final String TAG_AUTHOR = "author";
private static final String TAG_POSTS = "posts";
private static final String TAG_ID = "id";
private static final String TAG_TITLE = "title";
private static final String TAG_DATE = "date";
private static final String TAG_CONTENT = "content";
private static final String TAG_THUMBNAIL_URL = "thumbnail";

JSONArray posts = null;

ArrayList<HashMap<String, String>> postList;

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

    postList = new ArrayList<HashMap<String,String>>();

    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String c_id = ((TextView)view.findViewById(R.id.id)).getText().toString();
        String c_title = ((TextView)view.findViewById(R.id.title)).getText().toString();
        String c_date = ((TextView)view.findViewById(R.id.date)).getText().toString();
        String c_content = ((TextView)view.findViewById(R.id.content)).getText().toString();
        String a_name = ((TextView)view.findViewById(R.id.name)).getText().toString();
        ImageView image = (ImageView) findViewById(R.id.thumb);

            Intent in = new Intent(getApplicationContext(), SimplePostActivity.class);

            in.putExtra(TAG_AUTHOR, a_name);
            in.putExtra(TAG_ID, c_id);
            in.putExtra(TAG_TITLE, c_title);
            in.putExtra(TAG_DATE, c_date);
            in.putExtra(TAG_CONTENT, c_content);

            startActivity(in);

            Bitmap bMap = BitmapFactory.decodeFile("/sdcard/" + c_id + ".jpeg");
            image.setImageBitmap(bMap);

        }
    });

    new GetData().execute();
}

public class GetData extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

    Parser parser = new Parser();
    String jsonStr = parser.makeServiceCall(url, Parser.GET);

        Log.d("Response: ", "> " +jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                posts = jsonObj.getJSONArray(TAG_POSTS);

                for (int i = 0; i < posts.length(); i++) {
                    JSONObject num = posts.getJSONObject(i);

                    String id = num.getString(TAG_ID);
                    String title = num.getString(TAG_TITLE);
                    String date = num.getString(TAG_DATE);
                    String content = num.getString(TAG_CONTENT);

                    JSONObject author_object = num.getJSONObject(TAG_AUTHOR);

                    String name = author_object.getString(TAG_NAME);

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

                    post.put(TAG_NAME, name);
                    post.put(TAG_ID, id);
                    post.put(TAG_TITLE, title);
                    post.put(TAG_DATE, date);
                    post.put(TAG_CONTENT, content);

                    postList.add(post);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("Parser", "Couldn't get any data from the url");
        }

        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        if(pDialog.isShowing())
           pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(MainActivity.this, postList, R.layout.list_item,
                new String[] {TAG_NAME, TAG_ID, TAG_DATE, TAG_TITLE, TAG_CONTENT},
                new int[] {R.id.name, R.id.id, R.id.title, R.id.date, R.id.content});
        setListAdapter(adapter);
    }

}

有人可以告诉我该怎么做吗?我甚至不知道如何开始......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">

<TextView
    android:id="@+id/title"
    android:textColor="#0fffff"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"/>
<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />
<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/></RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你必须:

  1. 将ImageView添加到您的布局中。
  2. 为了显示图像,您必须实现自己的listview适配器并调用在getView方法中加载的图像。
  3. 使用Picasso library进行后台下载。它的用法很简单:

    Picasso.with(上下文).load(&#34; http://i.imgur.com/DvpvklR.png&#34)。到(ImageView的);