gpt4 book ai didi

java - 自定义 ListView 适配器不适用于 List> 而不是 String[]

转载 作者:行者123 更新时间:2023-11-29 21:31:37 26 4
gpt4 key购买 nike

我在 fragment 中有一个 listView。

我从网上获取一些 json 并将其传递到 List<List<String>> .

由于某种原因,该列表不起作用..没有崩溃,logcat 中也没有任何可疑的东西..

它只是不在 ListView 中显示任何内容。

适配器的调用(来自 fragment ):

setListAdapter(new MobileArrayAdapter(getActivity(), items));

这是我的适配器代码:

包 com.example.free;导入 java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
public List<List<String>> items;
//private final static String[] a={"s"};

public MobileArrayAdapter(Context context, List<List<String>> items) {
super(context, R.layout.list_item);
this.context = context;
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
ProgressBar loader = (ProgressBar) rowView.findViewById(R.id.loader);
ImageView image = (ImageView) rowView.findViewById(R.id.itemimgae);
TextView views = (TextView) rowView.findViewById(R.id.views);
TextView likes=(TextView) rowView.findViewById(R.id.likes);
TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
TextView desc=(TextView) rowView.findViewById(R.id.desc);
TextView pub =(TextView) rowView.findViewById(R.id.pub);
TextView idnum =(TextView) rowView.findViewById(R.id.idnum);

Log.e("title",items.get(1).get(1));

title.setText(" "+items.get(1).get(position));
views.setText(items.get(6).get(position));
likes.setText(items.get(7).get(position));
upvote.setText(items.get(8).get(position));
downvote.setText(items.get(9).get(position));
desc.setText(items.get(4).get(position));
pub.setText(items.get(5).get(position));
idnum.setText(items.get(0).get(position));


return rowView;
}
}

items满了..我检查了一下,但由于某种原因数据没有到达适配器..

这里有什么问题吗?

编辑:

10-12 14:23:02.880: W/dalvikvm(23285): threadid=1: thread exiting with uncaught exception (group=0x4107b930)
10-12 14:23:02.900: E/AndroidRuntime(23285): FATAL EXCEPTION: main
10-12 14:23:02.900: E/AndroidRuntime(23285): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.util.ArrayList.get(ArrayList.java:304)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.example.free.MobileArrayAdapter.getView(MobileArrayAdapter.java:49)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.ListView.onMeasure(ListView.java:1158)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2438)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.View.measure(View.java:15525)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.os.Handler.handleCallback(Handler.java:725)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.os.Looper.loop(Looper.java:137)
10-12 14:23:02.900: E/AndroidRuntime(23285): at android.app.ActivityThread.main(ActivityThread.java:5227)
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 14:23:02.900: E/AndroidRuntime(23285): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-12 14:23:02.900: E/AndroidRuntime(23285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-12 14:23:02.900: E/AndroidRuntime(23285): at dalvik.system.NativeStart.main(Native Method)

编辑#2:

{List of lists:
{Titles list(Strings):bla,bla,bla},
{desc list(Strings): bla, bla,bla},
{another list(Strings): bla, bla,bla},
}

这是一张照片

image

最佳答案

您链接到错误的构造函数,并且您的类声明错误。

改变:

public class MobileArrayAdapter extends ArrayAdapter<String>

到:

public class MobileArrayAdapter extends ArrayAdapter<List<String>>

并改变:

super(context, R.layout.list_item);

到:

super(context, R.layout.list_item, 0, items);

然后您可以转储自己的 items数据成员和调整getView()相应地。

发生的事情是ArrayAdapter对你的List<String>一无所知, 因此它将返回 getCount()共 0 个。

0super()在这里调用是安全的,因为您没有链接到父类(super class),选择处理 getView() 中的所有内容自己。

但是,这指出了您的 getView() 中的问题实现:

  • 请回收您的行,使用convertView如果不是 null

  • 考虑使用 getActivity().getLayoutInflater()而不是总是创建一个新的,以防 Activity 缓存 LayoutInflater或为您提供专门的服务

  • " "+items.get(1).get(position)与设计您的行布局相比,这可能不是一个好主意 TextView位于您想要的位置

  • 鉴于您的行中有多少小部件,请考虑使用 ViewHolder缓存那些 findViewById() 的模式通话

关于java - 自定义 ListView 适配器不适用于 List<List<String>> 而不是 String[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333419/

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