我的应用程序工作正常,但我试图了解ListAdapter的工作原理,更具体地说是调用getCount和getView方法的时间和方式。
我理解:
我调试了我的应用程序以查看调用这些方法的时间和方式。 这是我发现的:
content_history_list.xml
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="2dp" />
activity.java
class ListViewItem {
public String cost, date, category;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_history_list);
List<ListViewItem> listViewItems = new ArrayList<ListViewItem>();
// here i fill up my list with items
ListView listview = (ListView) findViewById(R.id.listView);
listview.setAdapter(new CustomAdapterHistoryList(this, listViewItems));
customlistitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/customlistiem_Category"
android:layout_width="100sp"
android:layout_height="36sp"
android:gravity="center_vertical"
android:textSize="12sp"
android:text="Category" />
<TextView
android:id="@+id/customlistiem_Cost"
android:layout_width="60sp"
android:layout_height="36sp"
android:gravity="center_vertical"
android:textSize="12sp"
android:background="@color/customlistitem_Cost"
android:text="Cost" />
<TextView
android:id="@+id/customlistiem_Description"
android:layout_width="130sp"
android:layout_height="36sp"
android:textSize="12sp"
android:text="Description"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/customlistiem_Date"
android:layout_width="80sp"
android:layout_height="36sp"
android:textSize="12sp"
android:gravity="center_vertical"
android:background="@color/customlistitem_Date"
android:text="Date" />
CustomAdapterHistoryList.java
public class Holder {
TextView tvCost, tvCategory, tvDate;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d(TAG, "Calling getView...Position: " + position);
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.customlistitem, null);
ListViewItem item = result.get(position);
holder.tvCost = (TextView) rowView.findViewById(R.id.customlistiem_Cost);
holder.tvCategory = (TextView) rowView.findViewById(R.id.customlistiem_Category);
holder.tvDate = (TextView) rowView.findViewById(R.id.customlistiem_Date);
holder.tvCost.setText(item.cost);
holder.tvCategory.setText(item.category);
holder.tvDate.setText(item.date);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result.get(position).category.toString() + " " + result.get(position).cost.toString() + " " + result.get(position).date.toString(), Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
期待听到您的反馈!感谢!!!!