gpt4 book ai didi

java - 我的 ListView 太慢了。并在向上滚动时崩溃

转载 作者:行者123 更新时间:2023-12-02 05:31:51 25 4
gpt4 key购买 nike

我有一个应用程序,其 ListView 有 15 行。如果有新的查询要搜索,我将 adapter 设置为 null 并清除 Allvideos 变量并使用

创建一个新查询

`new ArrayList();

当用户点击最后一行“更多结果”时,我将项目添加到Allvideos。但是在我的应用程序中进行一些搜索并将该项目添加到列表中后,我的应用程序速度很慢,当我尝试向上滚动时它崩溃了。这是我的适配器

public class VideosAdapter extends BaseAdapter {

// The list of videos to display
public static List<Video> videos;
// An inflator to use when creating rows
private LayoutInflater mInflater;
private Context context;

/**
* @param context this is the context that the list will be shown in - used to create new list rows
* @param videos this is a list of videos to display
*/
public VideosAdapter(Context context, List<Video> videos) {
VideosAdapter.videos = videos;
this.mInflater = LayoutInflater.from(context);
this.context = context;
}

@Override
public int getCount() {
return videos.size();
}

@Override
public Object getItem(int position) {
return videos.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public Object getItem(int position) {
return videos.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Video video = videos.get(position);
ViewHolder viewHolder;
if(video.getTitle()==null && video.getDuration()==-1)
{
convertView = mInflater.inflate(R.layout.more_results, null);
View thumbnail = convertView.findViewById(R.id.thumbnail);
thumbnail.setVisibility(View.INVISIBLE);
return convertView;
}
//animation = AnimationUtils.loadAnimation(context, R.anim.push_up_in);
// If convertView wasn't null it means we have already set it to our list_item_user_video so no need to do it again
else if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item_user_video, null);
viewHolder = new ViewHolder();
viewHolder.thumb = (UrlImageView) convertView.findViewById(R.id.list_image);
viewHolder.duration=(TextView) convertView.findViewById(R.id.duration);
viewHolder.title = (TextView) convertView.findViewById(R.id.userVideoTitleTextView);

convertView.setTag(viewHolder);
}
else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolder) convertView.getTag();
}
// We are using a custom imageview so that we can load images using urls
// For further explanation see: http://blog.blundell-apps.com/imageview-with-loading-spinner/

//TextView size=(TextView) convertView.findViewById(R.id.size);




int durationmin=video.getDuration()/60;
int durationsec=video.getDuration()%60;

if(durationsec<10) viewHolder.duration.setText(durationmin+":0"+durationsec);
else{
viewHolder.duration.setText(durationmin+":"+durationsec);
}
// thumb image

// Get a single video from our list
// Set the image for the list item
viewHolder.thumb.setImageDrawable(video.getThumbUrl());
// Set the title for the list item

//ytapi.com düzelene kadar böyle kalacak.

//size.setText(df.format(MainActivity.getSizeOf(MainActivity.getDownloadUrl(videoid)))+" MMB");

viewHolder.title.setText(video.getTitle());

/*animation.setDuration(1000);
convertView.startAnimation(animation);
animation = null;*/

return convertView;
}

我使用此代码将项目添加到列表并设置适配器。

    if(Allvideos.isEmpty())
{
Allvideos=videos;
}
else
{
//Remove extra more results row
Allvideos.remove(Allvideos.size()-1);
for(int i=0;i<videos.size();i++)
{
Allvideos.add(videos.get(i));

}
}

VideosAdapter adapter = new VideosAdapter(getContext(), Allvideos);
//adapter.notifyDataSetChanged();
setAdapter(adapter);

我的logcat是这样的。

08-22 15:53:40.557: E/InputEventReceiver(619): Exception dispatching input event.
08-22 15:53:40.657: E/AndroidRuntime(619): FATAL EXCEPTION: main
08-22 15:53:40.657: E/AndroidRuntime(619): java.lang.NullPointerException
08-22 15:53:40.657: E/AndroidRuntime(619): at com.skymaster.tut.ui.adapter.VideosAdapter.getView(VideosAdapter.java:108)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.ListView.makeAndAddView(ListView.java:1769)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.ListView.fillUp(ListView.java:706)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.ListView.fillGap(ListView.java:645)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5040)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3197)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3471)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.View.dispatchTouchEvent(View.java:7127)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
08-22 15:53:40.657: E/AndroidRuntime(619): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
08-22 15:53:40.657: E/AndroidRuntime(619): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:268)
08-22 15:53:40.657: E/AndroidRuntime(619): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.View.dispatchPointerEvent(View.java:7307)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4205)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4245)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.Choreographer.doFrame(Choreographer.java:523)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.os.Handler.handleCallback(Handler.java:615)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.os.Looper.loop(Looper.java:137)
08-22 15:53:40.657: E/AndroidRuntime(619): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-22 15:53:40.657: E/AndroidRuntime(619): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 15:53:40.657: E/AndroidRuntime(619): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 15:53:40.657: E/AndroidRuntime(619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-22 15:53:40.657: E/AndroidRuntime(619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 15:53:40.657: E/AndroidRuntime(619): at dalvik.system.NativeStart.main(Native Method)

这一行发生错误 java:108我从 site(video.getDuration()) 获取持续时间,其第二种类型是 360 秒而不是 6 分钟。

duration.setText(durationmin+":"+durationsec);  

为了平滑滚动,我使用了 ViewHolder 但它没有解决我的问题。正如您在我的代码中看到的那样,每行的布局中都有 URLIMAGEVIEW 。使用此项目,我从每行的 url 加载不同的图像。当图像加载时,会出现微调器,直到加载完成。另外,URLIMAGEVIEW 位于另一个 Thread 中,而不是 ui Thread 中。 URLIMAGEVIEW 可能是我的应用程序出现错误或速度变慢的原因吗?

最佳答案

为什么将视频作为静态场;这是没有必要的,也是一种不好的做法。

此外,当显示列表中的项目时,请考虑扩展ArrayAdapter而不是BaseAdapter;这样你只需要覆盖 getView;而不是 getPosition、getCount 等。

if(Allvideos.isEmpty())
{
Allvideos=videos;
}
else
{
//Remove extra more results row
Allvideos.remove(Allvideos.size()-1);

考虑在此处使用页脚,而不是每次都添加和删除最后一项。作为奖励,它使您的 getView 方法更简单。

    for(int i=0;i<videos.size();i++)
{
Allvideos.add(videos.get(i));

您可能想研究一下 addAll 方法。

    }
}

VideosAdapter adapter = new VideosAdapter(getContext(), Allvideos);

您不需要每次都重新创建适配器。相反,请执行以下操作:

//fields
private ArrayList<Video> videos;
private ArrayAdapter mAdapter;
private View footer;

protected void onCreate(Bundle savedInstanceState) {
ListView list = findViewById(R.id.list);
footer = getLayoutInflater().inflate(R.layout.more_results, null);
list.addFooterView(footer);
videos = new ArrayList<Video>
mAdapter = new VideosAdapter(this, videos);
list.setAdapter(mAdapter);
}

public addItemsToList(List<Video> items){
videos.addAll(items);
mAdapter.notifyDataSetChanged();
}

作为奖励,页脚始终位于列表的末尾。如果您想删除页脚,您可以切换页脚的可见性,这也很好。

关于您的错误;添加页脚后问题将得到解决。当前问题是由您的 moreResults 布局引起的。

convertView = mInflater.inflate(R.layout.more_results, null);
View thumbnail = convertView.findViewById(R.id.thumbnail);
thumbnail.setVisibility(View.INVISIBLE);
return convertView;

请注意,如果您的适配器下次尝试回收该 View ,convertView 将不为 null;但是,此处未设置标签。因此,出现了 nullPointerException。

关于java - 我的 ListView 太慢了。并在向上滚动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25456705/

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