xml ListView CustomAdapter

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml ListView CustomAdapter相关的知识,希望对你有一定的参考价值。

public class CustomAdapter extends BaseAdapter {

    Activity activity;
    ArrayList<Product> data;
    LayoutInflater layoutInflater;

    public CustomAdapter(Activity activity, ArrayList<Product> data) {
        this.activity = activity;
        this.data = data;
        layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View root = convertView;
        if(root == null){
            root = layoutInflater.inflate(R.layout.product_item,null);
        }
        ImageView product_image = root.findViewById(R.id.product_image);
        TextView product_title = root.findViewById(R.id.product_title);
        TextView product_summary = root.findViewById(R.id.product_summary);

        product_image.setImageResource(data.get(position).getImage());
        product_title.setText(data.get(position).getTitle());
        product_summary.setText(data.get(position).getSummary());
        return root;
    }
}
public class Product {
    private int id;
    private int image;
    private String title;
    private String summary;

    public Product(int id, int image, String title, String summary) {
        this.id = id;
        this.image = image;
        this.title = title;
        this.summary = summary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
}
//listview
final ListView dataListView = findViewById(R.id.dataListView);
//gridview 
//final GridView dataListView = findViewById(R.id.dataGridView);

ArrayList<Product> products = new ArrayList<>();
products.add(new Product(1,R.drawable.apple,"Apple","Apppppppppppple"));
products.add(new Product(2,R.drawable.android,"Android","Anddrrooooiiiiid"));
products.add(new Product(3,R.drawable.apple,"Apple","Apppppppppppple"));
products.add(new Product(4,R.drawable.android,"Android","Anddrrooooiiiiid"));
products.add(new Product(5,R.drawable.apple,"Apple","Apppppppppppple"));
products.add(new Product(6,R.drawable.android,"Android","Anddrrooooiiiiid"));
products.add(new Product(7,R.drawable.apple,"Apple","Apppppppppppple"));
CustomAdapter customAdapter = new CustomAdapter(this,products);
dataListView.setAdapter(customAdapter);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/product_image"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:textAlignment="center"
            android:text="Product Title"
            android:textColor="#000"
            android:textSize="24sp"
            android:id="@+id/product_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TextView
            android:textAlignment="center"
            android:text="Product Summary"
            android:textSize="14sp"
            android:id="@+id/product_summary"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
/*
    <GridView
        android:numColumns="2"
        android:id="@+id/dataGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
*/
    <ListView
        android:id="@+id/dataListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

以上是关于xml ListView CustomAdapter的主要内容,如果未能解决你的问题,请参考以下文章

android listview中的文字大小能在string.xml里设置吗

删除 ListView 分隔符(在 xml 布局文件中)[重复]

在WPF的ListView中用xml绑定了数据源,怎么在xml中的内容变化后让ListView中的视图实时改变

xml wapmass-Android的ListView控件,DetailsActivity.xml

Android XML解析listview

如何在 C# WPF 中从 ListView/XML 中完全删除一个项目?