gpt4 book ai didi

android - 具有单独行布局的 ListView 无法正确生成 View

转载 作者:行者123 更新时间:2023-11-30 02:01:31 27 4
gpt4 key购买 nike

您好,我创建了一个 ListView ,其中包含四个布局,我的数据集是图像、文本、音频和视频。所以我相应地增加布局。一旦数据来自 Api,图像被缓存,音频视频和文本分别存储在文件夹和 sqlite 中。但是当我第二次打开时, ListView 随机或有时无序显示数据。甚至有时不显示全部数据。这是我的适配器代码。

public class ListViewAdapter extends BaseAdapter {

ArrayList<ListModel> myList = new ArrayList<ListModel>();
LayoutInflater inflater;
Context context;
int flag = 0;
private static final int TYPE_ITEM1 = 1;
private static final int TYPE_ITEM2 = 2;
private static final int TYPE_ITEM3 = 3;
private static final int TYPE_ITEM4 = 4;
private String url;
private String vPath;
private MediaPlayer mp;



public ListViewAdapter(Context context, ArrayList<ListModel> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);

}


int type;

@Override
public int getItemViewType(int position) {
ListModel listModel = myList.get(position);
String data = listModel.getType();
/* if (data.equals("Text")) {
return type1;
} else if (data.equals("Image")) {
return type2;

}return 0;*/

if (data.equals("Text")) {
type = TYPE_ITEM1;
} else if (data.equals("Image")) {
type = TYPE_ITEM2;
} else if (data.equals("Audio")) {
type = TYPE_ITEM3;
}else if(data.contains("Video")){
type=TYPE_ITEM4;
}

return type;

}

@Override
public int getViewTypeCount() {
return myList.size() + 1;
}


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


@Override
public ListModel getItem(int position) {
// return myList.get(position);

if (position >= myList.size()) {
return null;
}
return myList.get(position);
}

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


@Override
public View getView(final int position, View v, ViewGroup parent) {
ViewHolder holder = null;
TextView textView = null;
ImageView imageView = null;
VideoView vPlayer = null;
Button pause = null;
Button play = null;

int type = getItemViewType(position);
System.out.println("getView " + position + " " + v + " type = " + type);
if (v == null) {
holder = new ViewHolder();
if (type == TYPE_ITEM1) {
v = inflater.inflate(R.layout.list_text, null);
textView = (TextView) v.findViewById(R.id.text);
} else if (type == TYPE_ITEM2) {
v = inflater.inflate(R.layout.list_image, null);
imageView = (ImageView) v.findViewById(R.id.imgView);
} else if (type == TYPE_ITEM3) {
v = inflater.inflate(R.layout.list_audio, null);
play = (Button) v.findViewById(R.id.btn_play);
pause = (Button) v.findViewById(R.id.stop);
} else if (type == TYPE_ITEM4) {
v = inflater.inflate(R.layout.list_video, null);
vPlayer = (VideoView) v.findViewById(R.id.video_player);

}
holder.textView = textView;
holder.videoPlayer = vPlayer;
holder.imageView = imageView;
holder.play = play;
holder.stop = pause;
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
ListModel model = myList.get(position);
if (holder.play != null) {
holder.play.setId(position);

}if (holder.videoPlayer!=null){
holder.videoPlayer.setId(position);
}if (holder.stop!=null){
holder.stop.setId(position);
}

String datatype = model.getType();

if (datatype.equals("Text")) {
holder.textView.setText(model.getData());
} else if (datatype.equals("Image")) {
UrlImageViewHelper.setUrlDrawable(holder.imageView, model.getData());
} else if (datatype.equals("Audio")) {
url = model.getData();
}else if (datatype.equals("Video")){
vPath=model.getData();

}
if (holder.play != null) {
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int i = view.getId();
ListModel model = myList.get(i);
String audioUri = model.getData();
new PlayMusicFromPath().execute(audioUri);
}
});
if (holder.stop!=null){
holder.stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});}
if (holder.videoPlayer!=null) {

final ViewHolder finalHolder = holder;
holder.videoPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int videoId = view.getId();
ListModel model1 = myList.get(videoId);
String videoUrl = model1.getData();
finalHolder.videoPlayer.setVideoPath(videoUrl);



}
});
}

}

return v;
}


public static class ViewHolder {
public TextView textView;
public ImageView imageView;
public Button play, stop;
public VideoView videoPlayer;


}


public void audioPlayer(String fileName) {
//set up MediaPlayer
mp = new MediaPlayer();

try {
mp.setDataSource(fileName);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}

class PlayMusicFromPath extends AsyncTask<String, String, String> {

// Show Progress bar before downloading Music
@Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method

}

// Download Music File from Internet
@Override
protected String doInBackground(String... f_url) {
audioPlayer(f_url[0]);

return null;
}

// Once Music File is downloaded
@Override
protected void onPostExecute(String file_url) {
System.out.print(file_url);

}
}

观点如下list_Video 包含一个视频 View list_audio 包含两个按钮播放和暂停。list_text 包含一个 textView和 list_image 包含一个 ImageView Logcat

addInArray been called, this = android.widget.ListView{443bacd0 VFED.VC. .F....ID 0,0-540,838 #7f0a006d app:id/listview}call stack =
java.lang.Throwable: addInArray
at android.view.ViewGroup.addInArray(ViewGroup.java:3786)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3740)
at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3687)
at android.widget.ListView.setupChild(ListView.java:1862)
at android.widget.ListView.makeAndAddView(ListView.java:1815)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1631)
at android.widget.AbsListView.onLayout(AbsListView.java:2150)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1877)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1653)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
at android.view.View.layout(View.java:15131)
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)

我也看到了这些线条..myprojectname while:A105 to many

最佳答案

在 View 类型计数中,您需要提供不同类型的 View 计数,而不是列表计数。在你的情况下,它应该是四个(文本、图像、音频和视频)

@Override
public int getViewTypeCount() {
return 4;
}

关于android - 具有单独行布局的 ListView 无法正确生成 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376513/

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