您好我创建了一个列表视图,在我夸大了四个布局中,我的数据集是图像,文本,音频和视频。所以我相应地给布局充气。一旦数据来自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")) {
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);
}
}
答案 0 :(得分:0)
您只能在Button
时设置type == TYPE_ITEM3
游戏,但始终致电holder.play.setOnClickListener
。因此,当play不为null时,你要么只调用setOnClickListener
方法,e.i:
if(holder.play != null){
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//mycode
}
});
}
或者您始终将播放按钮设置为Button
对象