Tôi đang cố xây dựng danh sách có chế độ xem tùy chỉnh cho các hàng, mỗi hàng sẽ bao gồm chế độ xem hình ảnh và hai chế độ xem văn bản.ArrayAdapter số phần tử trong tập dữ liệu là số
để làm như vậy tôi đã mở rộng lớp ArrayAdapter (gọi là PostersArrayAdapter) và ghi đè phương thức getView() để tạo kết nối đúng giữa dữ liệu và bố cục hàng. Tuy nhiên, khi tôi cố gắng xây dựng một PostersArrayAdapter với một mảng của lớp PosterData (thực hiện của tôi) với một số dữ liệu kết quả là bộ điều hợp rỗng, có nghĩa là getCount() trả về 0 và listView là rỗng.
ai cũng có thể đề xuất những gì tôi đang làm sai? Tôi đang dựa vào mã mà tôi tìm thấy ở đây - http://www.vogella.de/articles/AndroidListView/article.html
Cảm ơn bạn rất nhiều!
đây là mã có liên quan: (PosterData lớp chỉ là một lớp học với các lĩnh vực hai chuỗi)
public class PostersListActivity extends ListActivity {
final private int NUM_OF_PICS = 2;
private ContentGetter cg;
private PosterData[] posters;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cg = new ContentGetter(NUM_OF_PICS);
try
{
posters = cg.parseIndexFile();
int res = cg.DownloadPosterPics(1);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// Use our own list adapter
listView = (ListView)findViewById(android.R.id.list);
//listView.setAdapter((ListAdapter) new ArrayAdapter<String>(this,R.layout.list_item,R.id.title,titles));
PostersArrayAdapter postersAdapter = new PostersArrayAdapter(this, posters);
Log.d("PostersListActivity.onCreate()","Number of elementes in the data set of the adapter is " + postersAdapter.getCount());
listView.setAdapter(postersAdapter);
}
public class PostersArrayAdapter extends ArrayAdapter<PosterData> {
private final Activity context;
private final PosterData[] posters;
public PostersArrayAdapter(Activity context, PosterData[] posters) {
super(context, R.layout.list_item);
this.context = context;
this.posters = posters;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
class ViewHolder {
public ImageView logo;
public TextView title;
public TextView names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null, true);
holder = new ViewHolder();
holder.title = (TextView) rowView.findViewById(R.id.title);
holder.names = (TextView) rowView.findViewById(R.id.names);
holder.logo = (ImageView) rowView.findViewById(R.id.logo);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.title.setText(posters[position].getTitle());
holder.names.setText(posters[position].getNames());
holder.logo.setImageResource(R.drawable.icon);
return rowView;
}
}
}
Dưới đây là cách bố trí danh sách xem Tôi đang sử dụng:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/selectPoster"
android:layout_gravity="center_horizontal">
</TextView>
<ListView android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
Và đây là bố cục phần tử danh sách tôi đang sử dụng:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:layout_width="wrap_content" >
<ImageView android:id="@+id/logo"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="22px"
android:layout_marginTop="4px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px">
</ImageView>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/title"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px">
</TextView>
<TextView android:id="@+id/names"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Vâng, bây giờ nó trả lại số chính xác. Thậm chí tốt hơn, bây giờ danh sách hiển thị chính xác (trước khi danh sách trống). Tại sao việc ghi đè phương thức getCount giải quyết vấn đề này? –
Tôi nghĩ rằng đó là bởi vì bạn không gọi đúng super constructor –
Gọi một trong những quyền sẽ ngăn chặn sự cần thiết phải ghi đè getCount? Tại sao bạn nghĩ tôi không gọi đúng? Nguyên tắc nào dẫn bạn đến kết luận này? –