Listview有两个textview和一个复选框

时间:2016-01-28 16:49:29

标签: android android-layout listview

![enter image description here

这就是我设置listview行的方法。我想分别在大文本和小文本中使用t1,t2和一个复选框。

这是我的数据源类以及自定义适配器类。

我在getView方法中尝试了很多不能正常工作的东西,我无法理解实际应该去哪里....请有人帮忙吗...我希望我已经描述得很好......如果不,请让我知道......

public class Data{
String t1;
String t2;
Boolean isSelected = false;

public Data(String t1,String t2) {
    this.t1 = t1;
    this.t2 = t2;
}

public String t1() {
    return t1;
}
public String t2() {
    return t2;
}
public Boolean getIsSelected() {
    return isSelected;
}

}

class CustomAdapter extends BaseAdapter{

Context mContext;
ArrayList<Data> mDataVariable;


public CustomAdapter(Context context, ArrayList<Data> dataVariable) {
    this.mContext = context;
    this.mDataVariable= dataVariable;

}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return null;
}
}

这是我尝试弹出列表视图的活动......

public class MyActivity{
    ArrayList<Data> mData;
    ArrayAdapter<CustomAdapter> mCustomAdapter;
    ListView listView;

}

onCreate{
listView= (ListView) findViewById(R.id.ActivityListview);
    mData= new ArrayList<mData>();

    mData.add(new Data("g1", "its g1"));
    mData.add(new Data("g2", "its g2"));
    mData.add(new Data("g3", "its g3"));
    mData.add(new Data("g4", "its g4"));

    mCustomAdapter = new ArrayAdapter<CustomAdapter>(this, R.layout.activity_listview, mData); 

    listView.setAdapter(mCustomAdapter);
}

我认为我设置得很好,但getView方法是我需要做的所有转换,我现在面临麻烦......

...指导

提前谢谢.....

1 个答案:

答案 0 :(得分:0)

定义xml文件row_layout

<?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="match_parent">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/checkbox">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/big_text"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/small_text"/>
    </LinearLayout>

</RelativeLayout>

并像这样更新您的CustomAdapter

import android.content.Context;
import android.content.res.TypedArray;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.newshunt.pracitce.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.zip.Inflater;





class CustomAdapter extends BaseAdapter {

  Context mContext;
  ArrayList<ContactsContract.Data> mDataVariable;


      public CustomAdapter(Context context, ArrayList<ContactsContract.Data> dataVariable) {
        this.mContext = context;
        this.mDataVariable = dataVariable;

      }

      @Override
      public int getCount() {
        return 0;
      }

      @Override
      public Object getItem(int position) {
        return null;
      }

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

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

        RecyclerView.ViewHolder holder;

        LayoutInflater inflator = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        convertView = inflator.inflate(R.layout.row_layout, null);

        TextView bigText = (TextView) convertView
            .findViewById(R.id.big_text);
        TextView smallText = (TextView) convertView
            .findViewById(R.id.small_text);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

        return convertView;
      }
    }