【问题标题】:Image to ListView图像到 ListView
【发布时间】:2015-09-19 15:02:13
【问题描述】:

我在 Internet 上有 json 文件。此文件包含字段“图像”,此字段具有图像的 url。问题:如何将图像放入具有 2 个文本字段的 ListView?如何放置文本字段,我已经知道了,但是如何放置图像... 我看到了 AsyncTask 的解决方案,但我已经有一个类扩展 AsyncTask。 下面的 MainActivity 类,也许可以帮上忙。

public class MainActivity extends ListActivity {
private Context context;
    private static String url = "https://fierce-citadel-4259.herokuapp.com/hamsters";
    private static final String TITLE = "title";
    private static final String DESCRIPTION = "description";
    private static final String IMAGE = "image";
    ArrayList<HashMap<String,String>> jsonlist = new ArrayList<HashMap<String, String>>();
    ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class ProgressTask extends AsyncTask<String,Void,Boolean> {
        private ProgressDialog dialog;
        private ListActivity activity;
        public ProgressTask(MainActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }
private Context context;
        protected void onPreExecute(){
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }
        protected void onPostExecute(final Boolean success){
            if(dialog.isShowing()){
                dialog.dismiss();
            }
            ListAdapter adapter = new SimpleAdapter(MainActivity.this,jsonlist, R.layout.list_item,new String[]{TITLE,DESCRIPTION,IMAGE},new int[]{R.id.title,R.id.description,R.id.image});
setListAdapter(adapter);


        }

        protected Boolean doInBackground(String... args) {
           JSONParser jParser = new JSONParser();
            JSONArray json = jParser.getJSONFromUrl(url);
            for(int i =0;i<json.length();i++){
                try{
                    JSONObject c = json.getJSONObject(i);
                    String vtitle = c.getString(TITLE);
                    String vdescription = c.getString(DESCRIPTION);
                    String vimage = c.getString(IMAGE);
HashMap<String,String > map = new HashMap<>();
                    map.put(TITLE,vtitle);
                    map.put(DESCRIPTION,vdescription);
                    map.put(IMAGE,vimage);
                    jsonlist.add(map);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
}

【问题讨论】:

    标签: android json listview android-asynctask


    【解决方案1】:

    我建议您使用毕加索 (http://square.github.io/picasso/)。这很容易。 在 Android 中,您可以使用 ImageView 来显示图像。这是您应该作为参数传递给 Picasso 语句的元素。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您可以将图像下载到ImageViews,但由于ImageViews 与ListView 一起回收,因此有一些特殊注意事项。

      这篇文章将为您提供针对具体情况的代码图像下载指南:

      Multithreading For Performance | Android Developers Blog

      您还应该查看毕加索图片库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 2017-07-13
        相关资源
        最近更新 更多