为什么我的Android ListAdapter只显示一个项目? [英] Why does my Android ListAdapter only show one item?
问题描述
在我的Android应用程序,我试图建立我的第一个自定义listadapter。我现在设法得到它显示一个项目,但不幸的是只有一个项目在有列表中的8个。我的适配器是这样的:
In my Android app I'm trying to build my very first custom listadapter. I now managed to get it to display one item, but unfortunately one item only where there are 8 in the list. My adapter looks like this:
public class PossibilitiesAdapter extends BaseAdapter {
private AdapterCallbacks callbacks;
private List<String> possibilitiesList = new ArrayList<String>();
private LayoutInflater inflater;
public PossibilitiesAdapter(Context context) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setPossibilitiesList(List<String> possibilitiesList) {
for (String possibility : possibilitiesList) {
addItem(possibility);
}
}
public void addItem (final String item) {
possibilitiesList.add(item);
notifyDataSetChanged();
}
private class ViewHolder {
TextView possibilityTitle;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_posibility, viewGroup, false);
holder.possibilityTitle = (TextView) convertView.findViewById(R.id.text_possibility);
holder.possibilityTitle.setText(possibilitiesList.get(position));
return convertView;
}
@Override
public int getCount() {
return possibilitiesList.size();
}
// And here are some more things like getItemId() and getItem()
}
和我从我的片段设置possibilitiesList像这样:
and I set the possibilitiesList from my fragment like so:
List<String> list = Arrays.asList(getResources().getStringArray(R.array.the_possibilities));
Log.e(this, new Integer(list.size()).toString()); // Outputs 8, so there are enough in the array.
adapter.setPossibilitiesList(list);
我fragment_possibilities.xml看起来是这样的:
My fragment_possibilities.xml looks like this:
<ListView
android:id="@+id/list_possibilities"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
和我list_item_possibility.xml:
and my list_item_possibility.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_possibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
所以,问题是,它只是显示的第一个项目,仅此而已。有谁知道是什么问题,我该如何解决?
So the problem is that it only displays the first item, and nothing more. Does anybody know what the problem is and how I can fix it?
推荐答案
您嵌套一个滚动型内部列表?这将是一个典型的行为,它仅示出了一个项目。尽量避免它o使用下面的方法。
Are you nesting your list inside a ScrollView? It would be a typically behaving that it only shows one item. Try to avoid it o use the following method.
/**
* read all adapter views and calculate total length
*
* @param listView
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
setListViewHeight(listView, totalHeight);
}
/**
* set listview height
*
* @param listView
* @param height
*/
public static void setListViewHeight(ListView listView, int height) {
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = height + (listView.getDividerHeight() * (listView.getAdapter().getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
没有这是行不通的,正如我所说,你不能把一个ListView里面一个滚动型。
No that will not work, as I said, you cant put a ListView Inside a ScrollView.
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_possibilities"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这篇关于为什么我的Android ListAdapter只显示一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!