我在使用BaseAdapter时遇到了麻烦

时间:2015-01-10 18:23:37

标签: android listview android-listview baseadapter listadapter

所以,我需要在这里宣布一些事情,我完全迷失了。丢失的所有内容都在代码中注释,我正在使用此示例使我的BaseAdapter工作:http://www.piwai.info/android-adapter-good-practices/

以下是代码:`public class MainActivity extends Activity {

ListView mListView;
String[] mTitle={"item1","item2"};
String[] mDetail={"info1","info2"};
int[] mImage ={R.drawable.ic_launcher,R.drawable.ic_launcher};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mListView=(ListView)findViewById(R.id.list);
    mListView.setAdapter(new dataListAdapter(mTitle, mDetail, mImage));
}

class dataListAdapter extends BaseAdapter {

    private List<>  //this needs to be finished

    String[] Title, Detail;
    int[] imge;

    dataListAdapter() {
        Title = null;
        Detail = null;
        imge=null;
        context = null;
    }

    public dataListAdapter(String[] text, String[] text1,int[] text3) {
        Title = text;
        Detail = text1;
        imge = text3;
        context = null;
    }

    public final Context context;

    public dataListAdapter(Context mContext){
        this.context = mContext;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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



        TextView titleView, detailView;
        ImageView imageView;

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.custom, parent,false);

            titleView = (TextView) convertView.findViewById(R.id.title);
            detailView = (TextView) convertView.findViewById(R.id.detail);
            imageView = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(R.id.title, titleView);
            convertView.setTag(R.id.detail, detailView);
            convertView.setTag(R.id.image, imageView);
        }else{
            titleView = (TextView) convertView.getTag(R.id.title);
            detailView = (TextView) convertView.getTag(R.id.detail);
            imageView = (ImageView) convertView.getTag(R.id.image);
        }

        /*Something goes here */ = getItem(position);

        //and here all needs to be declared
        titleView.setText();
        detailView.setText();
        imageView.setImageDrawable();

        return convertView;
    }
}`

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

</ListView>

和custom.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="255dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Video1"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#339966"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/detail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="video1"
                android:textColor="#606060" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

由于您使用数组而不是自定义对象,因此可以忽略getItem

以下是如何使用数组:

titleView.setText(Title[position]);
detailView.setText(Detail[position]);
imageView.setImageDrawable(context.getResources().getDrawable(imge[position]));

修改

在构造函数中,您不设置上下文,因此它为null。这是它应该如何:

public dataListAdapter(String[] text, String[] text1,int[] text3, Context ctx) {
    Title = text;
    Detail = text1;
    imge = text3;
    context = ctx;
}

onCreate中创建适配器,如下所示:

mListView.setAdapter(new dataListAdapter(mTitle, mDetail, mImage, this));