如何为Android中的ListView项的每个字段添加标题

时间:2017-03-30 08:00:32

标签: android listview

我有一个带有自定义适配器的Listview,它显示人的数据,如

Barak Obama 2008

但是我想在名字姓氏日期中添加一个标题但正确地分配

草稿示例:

带标题的ListView项目

enter image description here

我如何实现这一目标?

谢谢

2 个答案:

答案 0 :(得分:0)

自定义ListView,您需要的所有内容,包含适配器和列表视图的行的xml布局

查看此链接说明自定义列表视图

https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

答案 1 :(得分:0)

您可以使用 TableLayout 将数据显示为您的要求。我复制了你的要求。这是代码。

1) MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lstview=(ListView)findViewById(R.id.listView);
    // Inflate header view
    ViewGroup headerView = (ViewGroup)getLayoutInflater().inflate(R.layout.header_layout, lstview,false);
    // Add header view to the ListView
    lstview.addHeaderView(headerView);
    // Get the string array defined in strings.xml file
    String[] date = {"29-03-17", "29-03-17"};
    String[] amount = {"5", "2"};
    String[] dept = {"14", "4"};
    // Create an adapter to bind data to the ListView
    CustomListViewAdapter adapter=new CustomListViewAdapter(this,R.layout.row_listview, R.id.tvDate, date, amount, dept);
    // Bind data to the ListView
    lstview.setAdapter(adapter);
}

2) CustomListViewAdapter.java

    public class CustomListViewAdapter extends ArrayAdapter<String> {
    int groupid;
    String[] date;
    String[] amount;
    String[] dept;
    ArrayList<String> desc;
    Context context;
    public CustomListViewAdapter(Context context, int vg, int id, String[] date, String[] amount, String[] dept){
        super(context,vg, id, date);
        this.context=context;
        groupid=vg;
        this.date=date;
        this.amount=amount;
        this.dept=dept;

    }
    // Hold views of the ListView to improve its scrolling performance
    static class ViewHolder {
        public TextView tvDate;
        public TextView tvAmount;
        public TextView tvDept;

    }

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

        View rowView = convertView;
        // Inflate the rowlayout.xml file if convertView is null
        if(rowView==null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView= inflater.inflate(groupid, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.tvDate= (TextView) rowView.findViewById(R.id.tvDate);
            viewHolder.tvAmount= (TextView) rowView.findViewById(R.id.tvAmount);
            viewHolder.tvDept= (TextView) rowView.findViewById(R.id.tvDept);
            rowView.setTag(viewHolder);

        }
        // Set text to each TextView of ListView item
        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.tvDate.setText(date[position]);
        holder.tvAmount.setText(amount[position]);
        holder.tvDept.setText(dept[position]);

        return rowView;
    }

}

3) HeaderLayout (header_layout.xml)

    <?xml version="1.0" encoding="utf-8"?>

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:stretchColumns="*"
     >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        >
        <TextView
            android:id="@+id/tvDate"
            android:layout_column="0"
            android:padding="10sp"
            android:gravity="left"
            android:layout_span="1"
            android:text="Date"
            android:layout_gravity="center">
        </TextView>
        <TextView
            android:id="@+id/tvAmount"
            android:layout_column="1"
            android:padding="10sp"
            android:gravity="left"
            android:layout_span="1"
            android:text="Amount"
            android:layout_gravity="center">
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvDept"
            android:layout_column="2"
            android:layout_span="1"
            android:text="Dept"
            android:layout_gravity="center" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvDelete"
            android:layout_column="3"
            android:layout_span="1"
            android:text="Delete"
            android:layout_gravity="center" />
    </TableRow>


</TableLayout>

4)每个ListView行的 RowLayout (row_listview.xml)

        <?xml version="1.0" encoding="utf-8"?>
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:stretchColumns="*">

        <TableRow
           android:layout_width="match_parent"
           android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvDate"
            android:layout_column="0"
            android:padding="10sp"
            android:gravity="left"
            android:layout_span="1"
            android:text="Date"
            android:layout_gravity="center">
        </TextView>
        <TextView
            android:id="@+id/tvAmount"
            android:layout_column="1"
            android:padding="10sp"
            android:gravity="left"
            android:layout_span="1"
            android:text="Amount"
            android:layout_gravity="center">
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvDept"
            android:layout_column="2"
            android:layout_span="1"
            android:text="Dept"
            android:layout_gravity="center" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:layout_column="3"
            android:layout_span="1"
            android:src="@android:drawable/ic_delete" />

          </TableRow>

         </TableLayout>

5)输出:

enter image description here

希望这有帮助。