簡體   English   中英

使用來自 url Picasso 在 android 中崩潰的圖像在 listView 中添加圖像

[英]Add Image in listView using image from url Picasso crash in android

Bonjour 我花了幾天時間尋找適合我的情況的答案,你能幫忙嗎這是我在任何時候使用畢加索添加 ligne 時使用的代碼,程序崩潰它適用於可繪制的靜態圖像我只想顯示與圖像對應的文本

    public void Afficher_les_vues(String url_in) {

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(url_in)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            if (response.isSuccessful()) {

                myResponse = response.body().string();
                Annonces.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                        try {
                            JSONObject reader = new JSONObject(myResponse);

                            JSONArray eglises = reader.getJSONArray("Annonces_adv");


                            arrayList.clear();


                            for (int i = 0; i < eglises.length(); i++) {

                                JSONObject eglise = eglises.getJSONObject(i);
                                String titre = eglise.getString(titret);
                                String description = eglise.getString(descriptionet);
                                int ref = Integer.parseInt(eglise.getString(reft));
                                String image = eglise.getString(imaget);
                                String url_image = eglise.getString(imaget);

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


                                data.put(titret, titre);
                                data.put(descriptionet, description);
                                data.put(reft, String.valueOf(ref));
                                data.put(imaget, image);
                                data.put(url_imaget, url_image);

                                arrayList.add(data);


                                ImageView imageViewlogo = findViewById(R.id.imageViewLogoNews);
                                //Picasso.get().load(url_image).into(imageViewlogo);

                                //new GetImageFromUrl(imageViewlogo).execute(url_image);


                                ListAdapter adapter = new SimpleAdapter(Annonces.this, arrayList, R.layout.listview_layout
                                        , new String[]{titret, descriptionet, reft, url_imaget}, new int[]{R.id.titre_de, R.id.description_de,
                                        R.id.reference, R.id.url_image});


                                lv.setAdapter(adapter);

                            }


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


                    }
                });


            }

當我使用畢加索在一個簡單的 ImageView 中而不是在 listView 中顯示和圖像時,它工作正常

在此先感謝您,我與 android studio Blessings 合作

您需要解決的主要問題是不為每個循環實例創建適配器,也不從適配器外部訪問行視圖(ImageView)。

1.不要讓適配器在循環內

您應該首先構建要顯示的數據數組,然后在構建數組的循環之后創建適配器並將其設置在 ListView 上。 當您有自定義適配器時,使用小型數據類來保存您需要在每一行中顯示的數據會很有幫助(請參閱答案后面的RowData

arrayList.clear(); // ArrayList<RowData>()

for (int i = 0; i < eglises.length(); i++) {

    JSONObject eglise = eglises.getJSONObject(i);
    String titre = eglise.getString(titret);
    String description = eglise.getString(descriptionet);
    int ref = Integer.parseInt(eglise.getString(reft));
    String url_image = eglise.getString(imaget);

    RowData data = new RowData(titre, description, String.valueOf(ref), url_image);

    arrayList.add(data);
}

// You CANNOT access row view here - in this context findViewById searches the main
// view heirarchy, and will not find views in your ListView rows

// Make the adapter *AFTER* the loop where you fill the array
// you will need to make your own custom adapter to load the image
ListAdapter adapter = new CustomAdapter(Annonces.this, R.layout.listview_layout, arrayList);


lv.setAdapter(adapter);

2. 不要試圖訪問適配器外部的行視圖

您的行視圖只能在適配器本身的getView內訪問。 這是您應該加載圖像的地方。 如果您之前使用的是標准適配器,則需要實現自己的自定義適配器來執行此操作。 這里有如何做到這一點的說明。 下面的適配器接受一個RowData項的 ArrayList 並使用它來填充每一行。

public class CustomAdapter extends ArrayAdapter<RowData> {
    private Context mContext;
    private int mResource;

    public MainAdapter(@NonNull Context context, int resource, @NonNull ArrayList<RowData> objects) {
        super(context, resource, objects);
        mContext=context;
        mResource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if( convertView == null ) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
        }

        // Get the views for this row (these must be in the
        // layout xml you passed in to the adapter constructor)
        TextView title = convertView.findViewById(R.id.titre_de);
        TextView description = convertView.findViewById(R.id.description_de);
        TextView reference = convertView.findViewById(R.id.reference);
        ImageView imageViewlogo = convertView.findViewById(R.id.imageViewLogoNews);

        // Get the RowData class for this row
        RowData data = getItem(position);

        // Set the text fields
        title.setText(data.title);
        description.setText(data.description);
        reference.setText(data.reference);

        // Start Picasso loading into the ImageView for this row
        Picasso.get().load(data.url).into(imageViewlogo);
        
        return convertView;
    }
}

做一個小數據類

當你有一個自定義適配器時,創建一個自定義數據類來保存你想要在每一行中顯示的數據是很有用的。 這避免了丟失鍵等可能出現的錯誤……您可以使用普通的舊地圖或傳入多個列表。

public class RowData {
    final String title;
    final String description;
    final String reference;
    final String url;

    RowData(String title, String desc, String ref, String url) {
        this.title = title;
        this.description = desc;
        this.reference = ref;
        this.url = url;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM