繁体   English   中英

如何在列表视图中获取图像?

[英]How do I get an image in my listview?

目前,我正在使用刚刚导入的ArrayAdapter 正如问题所述,我想将图像添加到列表视图中。 我正在使用JSONObjects将信息加载到列表中,并将其存储到ArrayLists中。 我知道我当前的方法是从JSONObjects中删除html标签。

公共类首页扩展活动{

ListView lView;
TextView tView;
ArrayAdapter lAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    lView = (ListView) findViewById(android.R.id.list);
    //tView = (TextView) findViewById(android.R.id.);
    loadList(lView);



}

public void loadList(ListView lView){
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 
    ServiceHandler sh = new ServiceHandler();

    // Making a request to url and getting response
    String jsonStr = sh.makeServiceCall("URL", ServiceHandler.GET);

    Log.d("Response: ", "> " + jsonStr);
    ArrayList<String> titles = new ArrayList<String>();
    ArrayList<String> body = new ArrayList<String>();

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


            // Getting JSON Array node
            JSONArray entries = jsonObj.getJSONArray("entries"); 
            for (int i = 0; i < entries.length(); i++) {
                JSONObject c = entries.getJSONObject(i);
                String text = c.getString("introtext"); 
                if(text == "null"){
                    text = "No text here" + "\n" + "\n";

                }
                else {
                    text = android.text.Html.fromHtml(text).toString();
                }
                String title= c.getString("title");
                String full = title + "\n" + "\n" + text;
                titles.add(full);
                //body.add(text);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }

    lAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, titles);
    lView.setAdapter(lAdapter);
}

}

任何建议都会有很大帮助

谢谢

您将需要创建一个定义了自己的布局的自定义适配器。 在此布局中,您可以包括所需的任何内容(包括可以放置图像的ImageView)。

有很多示例,但是在代码中要知道的重要一点是,您将在此处提供自己的适配器,并对其进行一些不同的设置:

lAdapter = new ArrayAdapter<String>(this, R.layout.my_custom_adapter, titles);

首先创建与您要连续显示的布局相同的布局,然后为自定义适配器创建一个类,例如:

public class MyAdapter extends ArrayAdapter<T>{
}

其中T代表实体类,代表您的数据模型。 现在在覆盖方法下:

public View getView(final int position, View convertView, ViewGroup parent) {}

膨胀为该行创建的布局,并根据需要设置每个视图。 不要忘记对图像使用延迟加载以将其加载到imageview上。

希望对您有帮助。

  • 尝试使用Base Adapter
  • 使用base adapter具有imageview的自定义Xml imageview
  • 您可以根据需要放置任何视图(图片,文本视图等)
  • 最后使用picassoimageloader通过对基本适配器的JSON响应填充图像

希望 对您有所帮助如果您需要更多帮助请回到我身边 您编程 愉快

您必须创建一个自定义适配器而不是ArrayAdapter ,以便可以对每一行使用自己的布局,并在那里执行任何“图像加载”。 尝试创建一个从框架扩展BaseAdapter抽象类的类。 另外,在网上搜索或在此处(在stackoverflow中),您将找到大量有关如何创建自定义适配器的教程。

如果您不熟悉ListViews和自定义适配器,那么下载和加载图像可能会有些麻烦,因此建议您为该工作查找两个第三方库: PicassoUniversal-Image-Loader ,每个库都有示例如何与自定义适配器一起使用它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM