0
A
回答
0
你必须创建3两件事:
代表您的ListView中的每一行XML布局,让我们row.xml调用它。它应该被放置在你的/ RES /布局文件夹,这样你就可以在Java访问为“R.layout.row”:要在其中显示的ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myRow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/myCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<TextView
android:text="Hello"
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
的ListActivity(或常规活动):
public class MyActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
ArrayList<MyObjectType> data = new ArrayList<MyObjectType>();
// Populate your data list here
MyCustomAdapter adapter = new MyCustomAdapter(this, data);
setListAdapter(adapter);
}
然后,你需要设计描述如何显示类型MyObjectType的对象的自定义适配器:
public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<MyObjectType> data;
public EventAdapter(Context context, ArrayList<MyObjectType> data){
// Caches the LayoutInflater for quicker use
this.inflater = LayoutInflater.from(context);
// Sets the events data
this.data= data;
}
public int getCount() {
return this.data.size();
}
public URL getItem(int position) throws IndexOutOfBoundsException{
return this.data.get(position);
}
public long getItemId(int position) throws IndexOutOfBoundsException{
if(position < getCount() && position >= 0){
return position;
}
}
public int getViewTypeCount(){
return 1;
}
public View getView(int position, View convertView, ViewGroup parent){
MyObjectType myObj = getItem(position);
if(convertView == null){ // If the View is not cached
// Inflates the Common View from XML file
convertView = this.inflater.inflate(R.layout.row, null);
}
((TextView)convertView.findViewById(R.id.myText)).setText(myObj.getTextToDisplay());
return convertView;
}
}
这应该让你开始,如果你需要更多的解释评论。