Listview 不会第二次生成具有不同布局的视图?

Listview not generating views on second time with different layouts?

你好我创建了一个列表视图,其中我膨胀了四个布局,我的数据集是图像、文本、音频和视频。所以我膨胀布局 accordingly.Once 数据来自 Api,图像被缓存,音频视频和文本分别存储在文件夹和 sqlite 中。但是当我第二次打开时,列表视图随机或有时无序显示数据。甚至有时不显示全部数据。这是我的适配器代码。

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);

    }
}

你只设置Buttontype == 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 对象