在我的Android应用中,我正在尝试构建我的第一个自定义listadapter。我现在设法让它显示一个项目,但遗憾的是只有列表中有8个项目的一个项目。我的适配器看起来像这样:
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,如下所示:
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如下所示:
<ListView
android:id="@+id/list_possibilities"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
和我的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>
所以问题是它只显示第一个项目,仅此而已。有谁知道问题是什么以及如何解决它?
答案 0 :(得分:4)
您是否将列表嵌套在ScrollView中?它通常表现为只显示一个项目。尽量避免使用以下方法。
/**
* 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放在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>
答案 1 :(得分:1)
你的getView方法实现错误,更确切地说是视图持有者模式。 我猜你看到了相同数量的条目,但都有相同的内容
试试这个:
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_posibility, viewGroup, false);
holder = new Holder();
holder.possibilityTitle = (TextView) convertView.findViewById(R.id.text_possibility);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.possibilityTitle.setText(possibilitiesList.get(position));
return convertView;
}
并且我认为你的setPossibilitesMethod看起来很奇怪。 id可能使用类似这样的东西
public void setPossibilitiesList(List<String> possibilitiesList) {
this.possibilitiesList = possibilitiesList;
notifyDataSetChanged();
}
public void addItem (final String item) {
possibilitiesList.add(item);
notifyDataSetChanged();
}
public void addItems (List<String> possibilitiesList) {
this.possibilitiesList.addAll(possibilitiesList);
notifyDataSetChanged();
}
您可以缩短检索inflaterService的代码,如下所示:
public PossibilitiesAdapter(Context context) {
inflater = LayoutInflater.fromContext(context);
}
此外你没有必要施展它
<强>更新强>
@Override
public int getCount(){
for (String possibility : this.possibilities){
System.out.println(possibility);
}
int size = this.possibilities();
System.out.println("getCount: " + size);
return size;
}
答案 2 :(得分:1)
更改方法addItem并从那里删除notify,在metghod setList中为for后添加notify
答案 3 :(得分:1)
最可能的情况是,当您执行possibilitiesList.size()
方法时,您将getCount()
作为 1 。打印日志以检查值。