gpt4 book ai didi

java - Android,ListView和ArrayAdaptater,getView只调用一次

转载 作者:行者123 更新时间:2023-12-02 07:21:30 26 4
gpt4 key购买 nike

我正在尝试在 ListView 上显示一些数据。我的问题是 ListView 中只显示一项。

我在这里找到了很多主题,但没有一个对我有帮助。

我发现 getView() 方法仅被调用一次(我检查了我的列表,它的大小是 178)。

这是我的代码:

public class RiverListArrayAdaptater extends ArrayAdapter<LinkedList<River>> {

private Context context;
private LinkedList<River> list;

public RiverListArrayAdaptater(Context context,LinkedList<River> list) {
super(context,R.layout.list_river);
this.context = context;
this.list = list;
}

public View getView(int pos, View convertView, ViewGroup parent) {
// Get variables
LayoutInflater inflater = (LayoutInflater) cont ext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_river, parent, false);
TextView textViewName = (TextView) rowView.findViewById(R.id.name);
TextView textViewValue = (TextView) rowView.findViewById(R.id.value);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textViewName.setText(list.get(pos).getName());
textViewValue.setText(list.get(pos).getDebit());
imageView.setImageResource(R.drawable.down);

return rowView;
}

}

我在onCreate()中的代码:

    LinkedList<River> list = parser.parse(getter.getData());
RiverListArrayAdaptater adaptater = new RiverListArrayAdaptater(this,list);
adaptater.notifyDataSetChanged();
adaptater.add(list);
list1.setAdapter(adaptater);

这是我的两个 XML 文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ListView
android:id="@+id/listRiver"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</RelativeLayout>

和:

    <?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"
android:padding="5dp" >

<ImageView
android:id="@+id/logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:contentDescription="@+id/up_or_down"
>
</ImageView>

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>

<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" >
</TextView>

</LinearLayout>

感谢您的宝贵时间...

最佳答案

您需要重写 ArrayAdapter 中的 getCount 并返回列表中有多少项。

检查source for ArrayAdapter了解更多信息。

关于java - Android,ListView和ArrayAdaptater,getView只调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14160504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com