滚动列表给出意想不到的结果

时间:2014-12-04 12:48:37

标签: android android-listview custom-lists

我已经在BaseAdapter的帮助下制作了一个自定义列表视图。现在我有一个条件需要检查并根据该条件我必须隐藏列表中的按钮取消隐藏。在开始时它正常工作但是当我滚动列表时按钮不在它想要的位置。我知道这是由于列表的重新压缩,但请你建议我如何克服它,因为我是新手。

代码

public class RecentDocumentAdapter extends BaseAdapter {

    FragmentManager fragmentManager;
    Activity activity;
    DbHandler dbHandler;
    DbModel dbModel;
    private Context context;
    private AlertDialog.Builder builder;
    private String[] shareArray;
    private Dialog dialog;
    private FragmentTransaction fragmentTransaction;
    private ArrayList<DbModel> alGetRecentFiles;
    private Bundle bundle;


    public RecentDocumentAdapter(Context context, Activity activity, ArrayList<DbModel> alGetRecentFiles) {
        this.context = context;
        this.activity = activity;
        this.alGetRecentFiles = alGetRecentFiles;
        bundle = new Bundle();
    }

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.recent_docs_custom_list, viewGroup, false);
            holder.docsImage = (ImageView) view.findViewById(R.id.iv_recent_docs_image);
            holder.tvTitle = (TextView) view.findViewById(R.id.tv_recent_name);
            holder.tvPostedOn = (TextView) view.findViewById(R.id.tv_recent_posted_on);
            holder.btShare = (Button) view.findViewById(R.id.bt_recent_share);
            holder.btFav = (Button) view.findViewById(R.id.bt_recent_fav);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

if (alGetRecentFiles.get(i).getCheckType() == 2) {
                holder.btShare.setVisibility(View.GONE);
            }


        holder.tvTitle.setText(alGetRecentFiles.get(i).getTitle() + alGetRecentFiles.get(i).getFileExtension());
        holder.tvPostedOn.setText(alGetRecentFiles.get(i).getPostedOn());

        if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".jpg") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".jpeg") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".png") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".gif") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".tiff")) {
            holder.docsImage.setImageResource(R.drawable.image);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".doc") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".docx")) {
            holder.docsImage.setImageResource(R.drawable.docx);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".xls") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".xlsx")) {
            holder.docsImage.setImageResource(R.drawable.excel);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".ppt") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".pptx")) {
            holder.docsImage.setImageResource(R.drawable.ppt);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".pdf")) {
            holder.docsImage.setImageResource(R.drawable.pdf);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase(".txt")) {
            holder.docsImage.setImageResource(R.drawable.txt);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("3gp") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("mp4") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("mkv") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("wmv") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("flv") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("avi") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("mpeg") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("mov") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("divx") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("div")) {
            holder.docsImage.setImageResource(R.drawable.video);
        } else if (alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("mp3") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("wma") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("aac") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("wav") || alGetRecentFiles.get(i).getFileExtension().equalsIgnoreCase("m4a")) {
            holder.docsImage.setImageResource(R.drawable.audio);
        } else {
            holder.docsImage.setImageResource(R.drawable.help);
        }


        if (alGetRecentFiles.get(i).getFavourites().equals("1")) {
            holder.btFav.setVisibility(View.VISIBLE);


        } else {
            holder.btFav.setVisibility(View.GONE);
        }


        holder.btShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bundle.putString("docId", alGetRecentFiles.get(i).getDocId());
                bundle.putString("title", alGetRecentFiles.get(i).getTitle());
                bundle.putString("path", alGetRecentFiles.get(i).getFilePath());
                bundle.putString("ext", alGetRecentFiles.get(i).getFileExtension());
                bundle.putString("fileName", alGetRecentFiles.get(i).getFileName());
                bundle.putInt("hasPassword", alGetRecentFiles.get(i).getHasPassword());
                bundle.putString("password", alGetRecentFiles.get(i).getPassword());
                fragmentTransaction = activity.getFragmentManager().beginTransaction();
                ShareWithinpocketDocs shareWithinpocketDocs = new ShareWithinpocketDocs();
                shareWithinpocketDocs.setArguments(bundle);
                fragmentTransaction.replace(R.id.frag_container, shareWithinpocketDocs, "shareWithinPocketDocs");
                fragmentTransaction.addToBackStack("shareWithinPocketDocsBack").commit();
            }
        });

        return view;
    }

    private class ViewHolder {
        private ImageView docsImage;
        private TextView tvTitle, tvPostedOn;
        private Button btShare, btFav;

    }
}

1 个答案:

答案 0 :(得分:1)

尝试在其他情况下显示共享按钮:

if (alGetRecentFiles.get(i).getCheckType() == 2) {
    holder.btShare.setVisibility(View.GONE);
}else{
    holder.btShare.setVisibility(View.VISIBLE);
}

注意:使用equals()进行字符串比较而不是==