ListView将仅显示初始项目

时间:2019-02-15 13:34:30

标签: android listview

我正在尝试将自定义ArrayAdapter用于我在其中一项活动中的listView。我正在使用的数据量非常大,因此要排除这个问题,我将一个ArrayList<ArrayList<String>>硬编码为简单地传递8个项目以显示在我的listView上,但是同样的问题也发生了,只有前四个是不能滚动到其他项目。

我也知道listView当前没有显示图像,我打算在解决此问题后将其链接起来。

这是将显示相关listView的主要活动:

ProductSelectionActivity.java

public class ProductSelectionActivity extends AppCompatActivity {

ListView productListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_selection);

    ArrayList<ArrayList<String>> productsList = new ArrayList<ArrayList<String>>();

    ArrayList<String> prodIds = new ArrayList<String>();
    ArrayList<String> prodNames = new ArrayList<String>();
    ArrayList<String> prodCosts = new ArrayList<String>();
    ArrayList<String> prodMainImages = new ArrayList<String>();

    prodIds.add("id1");
    prodIds.add("id2");
    prodIds.add("id3");
    prodIds.add("id4");
    prodIds.add("id5");
    prodIds.add("id6");
    prodIds.add("id7");
    prodIds.add("id8");

    prodNames.add("name1");
    prodNames.add("name2");
    prodNames.add("name3");
    prodNames.add("name4");
    prodNames.add("name5");
    prodNames.add("name6");
    prodNames.add("name7");
    prodNames.add("name8");

    prodCosts.add("cost1");
    prodCosts.add("cost2");
    prodCosts.add("cost3");
    prodCosts.add("cost4");
    prodCosts.add("cost5");
    prodCosts.add("cost6");
    prodCosts.add("cost7");
    prodCosts.add("cost8");

    prodMainImages.add("image1");
    prodMainImages.add("image2");
    prodMainImages.add("image3");
    prodMainImages.add("image4");
    prodMainImages.add("image5");
    prodMainImages.add("image6");
    prodMainImages.add("image7");
    prodMainImages.add("image8");

    productsList.add(prodIds);
    productsList.add(prodNames);
    productsList.add(prodCosts);
    productsList.add(prodMainImages);

    // Create the listView arrayAdapter
    ProductListAdapter arrayAdapter =
            new ProductListAdapter(this, R.layout.product_list_items, productsList);
    // Set the adapter
    productListView = (ListView) findViewById(R.id.products_list);
    productListView.setAdapter(arrayAdapter); 

}}

以下是适配器的类:

ProductListAdapter.java

public class ProductListAdapter extends ArrayAdapter<ArrayList<String>> {

// Initialise list of products
ArrayList<ArrayList<String>> products;

public ProductListAdapter(Context context, int resource, ArrayList<ArrayList<String>> objects) {
    super(context, resource, objects);
    products = objects;
}

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

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.product_list_items, parent, false);
    }

    // Get product listing info with reference to position that has been passed as param
    ArrayList<String> prodIds = products.get(0);
    String prodId = prodIds.get(position);

    ArrayList<String> prodNames = products.get(1);
    String prodName = prodNames.get(position);

    ArrayList<String> prodCosts = products.get(2);
    String prodCost = prodCosts.get(position);

    ArrayList<String> prodMainImages = products.get(3);
    String prodMainImage = prodMainImages.get(position);

    // Getting id for textview and imageview
    TextView textViewName = (TextView) convertView.findViewById(R.id.prod_name_text);
    TextView textViewCost = (TextView) convertView.findViewById(R.id.prod_cost_text);

    // Setting values to ids
    textViewName.setText(prodName);
    textViewCost.setText("£"+prodCost);

    return convertView;
}}

ProductSelectionActivity的布局:

activity_product_selection.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductSelectionActivity">



    <ListView
        android:id="@+id/products_list"
        android:layout_width="396dp"
        android:layout_height="508dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.961" />

</android.support.constraint.ConstraintLayout>

适配器的布局:

product_list_items.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:orientation="horizontal"
    android:weightSum="100" >

    <ImageView
        android:id="@+id/prod_main_image"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_weight="60"
        app:srcCompat="@color/background_holo_light" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="40"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/prod_name_text"
            android:layout_width="278dp"
            android:layout_height="60dp"
            android:text="@string/prod_name_text" />

        <TextView
            android:id="@+id/prod_cost_text"
            android:layout_width="278dp"
            android:layout_height="60dp"
            android:text="@string/prod_cost_text" />
    </LinearLayout>

</LinearLayout>

我认为问题是通过消除我认为可能是问题的其他因素而在布局文件中解决的;例如确保所有数据都正确传递(我可以通过Logcat识别出来)。

如果我还有其他需要澄清的地方,请告诉我。

4 个答案:

答案 0 :(得分:1)

ListView将显示您在适配器中传递的项目。因此,您要传递一个包含4个元素的ArrayList。这就是为什么ListView仅显示4个项目的原因。如果将更多元素添加到productsList ArrayList,则可以显示的项目数将增加。

另一件事,只是为了改善您的工作。不必为每个产品详细信息分别创建单独的ArrayList,而只需为您的产品创建一个模型并向其中加载数据即可。然后将ArrayList以及所需数量的产品项传递到自定义适配器。

class Product {
    String productId;
    String productName;
    String productCost;
    String productImage;

    Product(String id, String name, String cost, String image) {
        productId = id;
        productName = name;
        productCost = cost;
        productImage = image;
    }
}

现在,您可以在ProductSelectionActivity中像这样使用它。

ArrayList<Product> productList = new ArrayList();
productList.add(new Product("id1", "name1", "cost1", "image1");
//Do this for all other products

ProductListAdapter arrayAdapter = new ProductListAdapter(this, R.layout.product_list_items, productList);
// Set the adapter
productListView = (ListView) findViewById(R.id.products_list);
productListView.setAdapter(arrayAdapter); 

最后一步是更新您的适配器以处理产品的ArrayList

public class ProductListAdapter extends ArrayAdapter<Product> {
    // Initialise list of products
    ArrayList<Product> products;

    public ProductListAdapter(Context context, int resource, ArrayList<Product> objects) {
        super(context, resource, objects);
        products = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.product_list_items, parent, false);
        }

        //Get the product item for this position
        Product product = products.get(0);

        TextView textViewName = (TextView) convertView.findViewById(R.id.prod_name_text);
        TextView textViewCost = (TextView) convertView.findViewById(R.id.prod_cost_text);

        // Setting values to ids
        textViewName.setText(product.productName);
        textViewCost.setText("£" + product.productCost);

        return convertView;
    }
}

另一项改进是使用ViewHolder而不是简单的视图。

答案 1 :(得分:1)

您可以创建Wrapper(POJO)类来保存您的产品信息,并将其用于列表适配器。与使用ArrayList<ArrayList<String>>相比,通过列表进行处理将是更好的方法。

首先创建一个模型(Pojo)类来保存产品信息。

class Product {

    String prodId;
    String prodName;
    String prodCost;
    String prodMainImage;

    public Product(String prodIds, String prodNames, String prodCostsl, String prodMainImages) {
        this.prodId = prodIds;
        this.prodName = prodNames;
        this.prodCost = prodCostsl;
        this.prodMainImage = prodMainImages;
    }
}

ProductSelectionActivity.java

public class ProductSelectionActivity extends AppCompatActivity {

ListView productListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_selection);

    ArrayList<Product> productsList = new ArrayList<>();

    productsList.add(new Product("id1", "name1", "cost1", "image1"));
    productsList.add(new Product("id2", "name2", "cost2", "image2"));
    productsList.add(new Product("id3", "name3", "cost3", "image3"));
    productsList.add(new Product("id4", "name4", "cost4", "image4"));

    ProductListAdapter arrayAdapter =
            new ProductListAdapter(this, R.layout.product_list_items, productsList);
    // Set the adapter
    productListView = (ListView) findViewById(R.id.products_list);
    productListView.setAdapter(arrayAdapter);
}}

ProductListAdapter.java

public class ProductListAdapter extends ArrayAdapter<Product> {

    // Initialise list of products
    ArrayList<Product> products;

    public ProductListAdapter(Context context, int resource, ArrayList<Product> objects) {
        super(context, resource, objects);
        products = objects;
    }

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

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.product_list_items, parent, false);
        }

        Product product = products.get(position);

        // Getting id for textview and imageview
        TextView textViewName = convertView.findViewById(R.id.prod_name_text);
        TextView textViewCost = convertView.findViewById(R.id.prod_cost_text);

        // Setting values to ids
        textViewName.setText(product.prodName);
        textViewCost.setText("£"+product.prodCost);

        return convertView;
    }
}

就是这样。它应该可以正常工作。尽管如今,RecyclerView的性能和管理越来越多地被使用。此代码也可以用RecyclerView替换。让我知道是否要将其转换为RecyclerView。我可以帮你。

答案 2 :(得分:1)

伙计,首先使用RecyclerView,不必担心数据大小。其次,创建一个包含您的数据的类:

class Data {
    int id;
    String name;
    String imageUrl;

    public Data(int id, String name, String imageUrl) {
        this.id = id;
        this.name = name;
        this.imageUrl = imageUrl;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getImageUrl() {
        return imageUrl;
    }
}

请研究有关如何使用RecyclerView以及为什么RecyclerView比ListView更好的信息。

答案 3 :(得分:0)

在product_list_items布局中将 wrap_content 设置为根布局的高度

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:weightSum="100" >

<ImageView
    android:id="@+id/prod_main_image"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_weight="60"
    app:srcCompat="@color/background_holo_light" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/prod_name_text"
        android:layout_width="278dp"
        android:layout_height="60dp"
        android:text="@string/prod_name_text" />

    <TextView
        android:id="@+id/prod_cost_text"
        android:layout_width="278dp"
        android:layout_height="60dp"
        android:text="@string/prod_cost_text" />
</LinearLayout>