簡體   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