如何用一个以上的textview填充一个listview中的多个数据? [英] how to populate multiple data in a listview with more than one textview?

查看:59
本文介绍了如何用一个以上的textview填充一个listview中的多个数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android新手.我在一个项目上工作,我有一个问题.我如何从 ArrayList< the_class> 中获取数据并将其填充到 listview 中?有一个包含6个textview的xml文件,必须用类项的上下文来填充.谢谢

I am new to android. I am working at a project and i have a question. How can i take data from a ArrayList<the_class> and populate them to a listview? There is an xml file that contains 6 textviews that must be filled with the context of the class items. Thank you

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:text="origin"
            android:textSize="19dp"
            android:id="@+id/origin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="destination"
            android:textSize="19dp"
            android:paddingLeft="15dp"
            android:id="@+id/destination"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

* xml文件的一部分.

*a part of the xml file.

推荐答案

实际上非常简单.

我创建了一个模型

public class Model {

    String top, bottom;

    public Model(String top, String bottom) {
        this.top = top;
        this.bottom = bottom;
    }

    public String getTop() {
        return top;
    }

    public void setTop(String top) {
        this.top = top;
    }

    public String getBottom() {
        return bottom;
    }

    public void setBottom(String bottom) {
        this.bottom = bottom;
    }
}

然后,我需要为其创建一个列表视图适配器.

Then, I need to create a listview adapter for it.

public class ListAdapter extends ArrayAdapter<Model> {


    private Context activityContext;
    private List<Model> list;
    public static final String TAG = "ListView";

    public ListAdapter(Context context, List<Model> list){
        super(context, R.layout.single_listview, list);
        this.activityContext = context;
        this.list = list;
    }


    @Override
    public View getView(final int position, View view, ViewGroup viewGroup){

        final ViewHolder viewHolder;

        if (view == null) {
            view = LayoutInflater.from(activityContext).inflate(R.layout.single_listview, null);
            viewHolder = new ViewHolder();

            viewHolder.top = (TextView) view.findViewById(R.id.top);
            viewHolder.bottom = (TextView) view.findViewById(R.id.bottom);

            viewHolder.top.setText(list.get(position).getTop());
            viewHolder.bottom.setText(list.get(position).getBottom());

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        return view;
    }

    private static class ViewHolder {

        TextView top;
        TextView bottom;
    }


}

在我的主要活动中,我执行以下代码

In my main activity, i do the following code

public class MainActivity extends AppCompatActivity {

    ListView listview;

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

        listview = (ListView) findViewById(R.id.listview);

        List<Model> list = new ArrayList<>();
        list.add(new Model("top-one", "bot-one"));
        list.add(new Model("top-two", "bot-two"));
        list.add(new Model("top-three", "bot-three"));
        list.add(new Model("top-four", "bot-four"));
        list.add(new Model("top-five", "bot-five"));
        list.add(new Model("top-six", "bot-six"));
        list.add(new Model("top-seven", "bot-seven"));
        list.add(new Model("top-eight", "bot-eight"));

        ListAdapter adapter = new ListAdapter(listview.getContext(), list);
        listview.setAdapter(adapter);
    }
}

主活动布局xml内部

<?xml version="1.0" encoding="utf-8"?>
<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="com.fuhnatik.customlistview.MainActivity">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

最后,在listview适配器布局xml内

finally, inside of the listview adapter layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip"
    android:orientation="vertical">


    <TextView
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="TOP SECTION HERE"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="BOTTOM SECTION HERE"
        android:textSize="12sp" />



</LinearLayout>

结果:

这篇关于如何用一个以上的textview填充一个listview中的多个数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆