两个数组列表自定义列表视图中的一个列表行(两个不同的textview)

时间:2018-01-19 04:14:34

标签: android listview

对于自定义arraylist,我有两个不同的listview,如何让它与两个不同的textview成一行。这是我的xml。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.25"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_marginStart="@7dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_duty_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:layout_marginStart="7dp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个,

持有人类:

public class DataHolder {

    private String name,duty_name;

    public DataHolder(String name, String duty_name) {
        this.name = name;
        this.duty_name = duty_name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDuty_name() {
        return duty_name;
    }

    public void setDuty_name(String duty_name) {
        this.duty_name = duty_name;
    }
}

数组列表:

ArrayList<DataHolder>list=new ArrayList<>();

基类:

public class MyBaseAdapter extends BaseAdapter {

    ArrayList<DataHolder>list myList;
    LayoutInflater inflater;
    Context context;

    public MyBaseAdapter(Context context, ArrayList<DataHolder>list myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

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

    @Override
    public ListData getItem(int position) {
        return myList.get(position);
    }

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

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

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        ListData currentListData = getItem(position);

        mViewHolder.tv_name.setText(currentListData.getName());
        mViewHolder.tv_duty_name.setText(currentListData.getDuty_name());

        return convertView;
    }

    private class MyViewHolder {
        TextView tv_name, tv_duty_name;

        public MyViewHolder(View item) {
            tv_name = (TextView) item.findViewById(R.id.tv_name);
            tv_duty_name = (TextView) item.findViewById(R.id.tv_duty_name);
        }
    }
}

layout_list_item:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.25"
android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_marginStart="@7dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_duty_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:layout_marginStart="7dp" />
</LinearLayout>