ListView设置适配器

作者: wxyass 分类: Android 发布时间: 2018-03-15 14:58

ListView的中级使用

1 为ListView设置适配器

/**
 * Adapter
 */
class DoctorAdapter extends BaseAdapter { 

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

    @Override
    public AppraisalInfo getItem(int position) {
        return appraisalInfoLst.get(position);
    }

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

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

        DoctorHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(MainActivity.this, R.layout.item_one_list, null);
            holder = new DoctorHolder();
            holder.date = (TextView) convertView.findViewById(R.id.item_one_tv_date);
            holder.point = (TextView) convertView.findViewById(R.id.item_one_tv_point);
            holder.symptom = (TextView) convertView.findViewById(R.id.item_one_tv_symptom);
            holder.treat = (TextView) convertView.findViewById(R.id.item_one_tv_treat);
            holder.recorde = (Button) convertView.findViewById(R.id.item_one_tv_recorde);
            convertView.setTag(holder);
        } else {
            holder = (DoctorHolder) convertView.getTag();
        }

        final String appraisalkey = appraisalInfoLst.get(position).getAppraisalkey();
        final String totalpoints = appraisalInfoLst.get(position).getTotalpoints();
        final String symptom = appraisalInfoLst.get(position).getSymptom();
        final String treat = appraisalInfoLst.get(position).getTreat();
        final String createdate = appraisalInfoLst.get(position).getCreatedate();

        String date = createdate.substring(0, 4) + "-" + createdate.substring(4, 6);

        holder.date.setText(date + "  评分: " + totalpoints);
        holder.symptom.setText(symptom);
        holder.treat.setText("建议: " + treat);
        holder.recorde.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("appraisalkey", appraisalkey);
                bundle.putString("createdate", createdate);
                bundle.putString("totalpoints", totalpoints);
                bundle.putString("symptom", symptom);
                bundle.putString("treat", treat);
                openActivity(HistoryResultAty.class, bundle);
            }
        });
        return convertView;
    }
}

static class DoctorHolder {
    TextView date;
    TextView point;
    TextView symptom;
    TextView treat;
    Button recorde;
}  

设置适配器条目的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null" >

    <!-- 时间 得分 -->
    <TextView
        android:id="@+id/item_one_tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:background="@drawable/bg_oneallhistory_item_date"
        android:paddingLeft="30dp"
        android:layout_marginTop="30dp"
        android:paddingTop="7dp"
        android:textColor="#FFFFFF"
        android:text="时间 得分"
        android:textSize="22sp" />

    <LinearLayout
        android:id="@+id/item_one_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_one_tv_date"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_oneallhistory_item_kuang"
        android:orientation="vertical"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        >

        <!-- 结果 -->
        <TextView
            android:id="@+id/item_one_tv_point"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:text="结果"
            android:textColor="#222222"
            android:textSize="24sp" />

        <!-- 症状 -->
        <TextView
            android:id="@+id/item_one_tv_symptom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:text="症状"
            android:textColor="#222222"
            android:textSize="24sp" />

        <!-- 治疗 -->
        <TextView
            android:id="@+id/item_one_tv_treat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:text="治疗"
            android:textColor="#222222"
            android:textSize="24sp" />
    </LinearLayout>

    <Button
        android:id="@+id/item_one_tv_recorde"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/item_one_ll"
        android:layout_alignRight="@id/item_one_ll" 
        android:layout_marginBottom="-20dp"
        android:background="@drawable/bt_oneall_xiangqing" />

</RelativeLayout>

2 在Activity中调用适配器

ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new DoctorAdapter());  
// 条目点击监听
listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, "点击了第" + position + "行", 0).show();
    }
});  

3 另外: 条目的不同按钮点击,请参考另一篇博客:listview条目中按钮点击事件的实现

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注