問題所以,我需要在這裏聲明幾件事情,而我完全失去了。失蹤所有的東西都在評論的代碼,我用這個例子讓我BaseAdapter工作:http://www.piwai.info/android-adapter-good-practices/我遇到了BaseAdapter
下面的代碼:`公共類MainActivity擴展活動{
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>
你有什麼問題嗎? –
我不知道我需要使用我提供的示例在代碼中編寫/聲明什麼。我知道我錯過了什麼,因爲不同的變量名 – robigroza