【问题标题】:Unable to fetch data from Custom Adaptor in android无法从android中的自定义适配器获取数据
【发布时间】:2020-03-17 03:25:04
【问题描述】:

我创建了一个产品列表,这些产品在我创建的 CustomListAdaptor 中正确填充,但在我运行应用程序时视图没有呈现。

在下面的 Adaptor 中,prodList 被正确地填充为 Product 实体

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;


public class CustomProductListAdaptor extends ArrayAdapter {

    //to reference the Activity
    private final Activity context;

    private final List<Product> productList;


    public CustomProductListAdaptor(Activity context,List<Product> prodList){
        super(context,R.layout.productlist_item);
        this.context=context;
        this.productList = prodList;
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.productlist_item, null,true);
        Toast.makeText(getContext(),"In getView",Toast.LENGTH_LONG).show();

        //this code gets references to objects in the listview_row.xml file
        TextView nameTextField = (TextView) rowView.findViewById(R.id.product_name);
        TextView priceTextField = (TextView) rowView.findViewById(R.id.product_description);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.productImage);

        //this code sets the values of the objects to values from the list
        try {
            nameTextField.setText(productList.get(position).getName());
            priceTextField.setText(productList.get(position).getPrice()+"/-");
            Uri imageUri = Uri.parse(productList.get(position).getImgUrl());
            Picasso.with(context).load(imageUri).placeholder(R.drawable.selectphoto).into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowView;
    }


}

【问题讨论】:

    标签: android android-studio android-listview custom-lists adaptor


    【解决方案1】:

    将项目视图布局高度设置为 wrapcontent

    为recyclerview设置一个layoutmanager

    【讨论】:

    • 项目视图布局高度已经是 wrapcontent only。而不是回收站视图,我使用的是列表视图。早些时候我正在获取数据,但是通过多个数组。现在我修改了返回产品列表的逻辑。适配器也是如此,但它没有被渲染。我想 getView 方法的某些部分不起作用。
    • 你检查了logat,也许它捕获了url解析错误
    • 在 logcat 中,我得到一个异常 java.lang.NullPointerException: Attempt to get length of null array 但我调试了流程并且数据正在进入适配器。有没有办法可以调试 getView 方法?我写了 1 条祝酒词,但那也没有出现。
    • 你能发布日志吗?
    • LayerDrawable 添加了无效的drawable! Drawable 已经属于另一个所有者,但不公开一个常量状态。 java.lang.RuntimeException at android.graphics.drawable.LayerDrawable$ChildDrawable.(LayerDrawable.java:1857) at android.graphics.drawable.LayerDrawable$LayerState.(LayerDrawable.java:1977)
    猜你喜欢
    • 1970-01-01
    • 2015-12-10
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2016-09-11
    相关资源
    最近更新 更多