如何在自定义ListView中使用Button OnClick和View Holder

时间:2014-11-13 15:32:34

标签: android android-listview android-viewholder

我需要一些帮助,我正在使用一个视图来显示动态arrayadapter。

我有列表视图。

每行包含;

  • 标题(Textview),
  • 子标题(TextView),
  • 进度条
  • 下载按钮(按钮)。

我想在单击“下载”按钮时显示进度条并隐藏“下载”按钮。单击第一行中的下载按钮时,将显示第一个进度条,但也显示第8个进度条。

这是我的代码。我做错了什么?

    public class TabInComingAdaptor extends BaseAdapter {

    public static class ViewHolder {
        TextView title;
        TextView desc;
        Button DownloadButton;
        ProgressBar pB;
    }

    private ArrayList<rowObject> data;
    private LayoutInflater inflater = null;
    private Application ap;
    // final private Activity currentActivity;
    Button progressButton1;
    int CurrentUser;

    public TabInComingAdaptor(Activity activity, Application application,
            ArrayList<rowObject> GelenFakslar) {
        // currentActivity = activity;
        ap = application;
        data = GelenFakslar;
        inflater = (LayoutInflater) ap
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(final int position, View vi, ViewGroup parent) {
        ViewHolder viewHolder;
        if (vi == null) {
            vi = inflater.inflate(R.layout.tab_incoming_row, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) vi.findViewById(R.id.RowTitle);
            viewHolder.desc = (TextView) vi.findViewById(R.id.RowDesc);
            viewHolder.DownloadButton = (Button) vi
                    .findViewById(R.id.RowDownloadButton);
            viewHolder.pB = (ProgressBar) vi
                    .findViewById(R.id.RowDownloadProgress);

            viewHolder.DownloadButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    LinearLayout vwParentRow = (LinearLayout) v.getParent();
                    v.setVisibility(View.GONE);
                    ProgressBar zxcv = (ProgressBar) vwParentRow.getChildAt(0);
                    zxcv.setVisibility(View.VISIBLE);
                    vwParentRow.refreshDrawableState();
                }
            });
            vi.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) vi.getTag();
        }

        viewHolder.title.setText(data.get(position).getBaslik());
        viewHolder.desc.setText(data.get(position).getTarih());

        return vi;
    }
}

4 个答案:

答案 0 :(得分:5)

这是我的解决方案。再次感谢@waqaslam和@ SweetWisherツ寻求帮助。

行对象(我的所有变量都有getter和setter):

public class rowObject {
    int Rowid;
    String title;
    String desc;
    String FileUrl;
    String FilePath;
    int ButtonClicked;
}

GetView方法:

public View getView(final int position, View vi, ViewGroup parent) {

    if (vi == null) {
        vi = inflater.inflate(R.layout.tab_incoming_row, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.title = (TextView) vi.findViewById(R.id.RowTitle);
        viewHolder.desc = (TextView) vi.findViewById(R.id.RowDesc);
        viewHolder.DownloadButton = (Button) vi
                .findViewById(R.id.RowDownloadButton);
        viewHolder.pB = (ProgressBar) vi
                .findViewById(R.id.RowDownloadProgress);
        vi.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) vi.getTag();
    }

    viewHolder.title.setText(data.get(position).getBaslik());
    viewHolder.desc.setText(data.get(position).getTarih());
    viewHolder.DownloadButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            data.get(position).setButtonClicked(1);
            new DownloadTask(currentActivity, position).execute();
        }
    });

    if (data.get(position).getButtonClicked() == 1) {
        viewHolder.DownloadButton.setVisibility(View.GONE);
        viewHolder.pB.setVisibility(View.VISIBLE);
    } else {
        viewHolder.DownloadButton.setVisibility(View.VISIBLE);
        viewHolder.pB.setVisibility(View.GONE);
    }
    return vi;
}

然后我将使用像This Post

这样的AsyncTask

答案 1 :(得分:2)

我认为您在列表项单击事件中有动作,例如

   watchListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) { 
               .....................   
     }
   });

如果是这样焦点将更喜欢列出项目,请单击否按钮,

更好的是将按钮替换为textview(像按钮一样设计)

试试这个,它肯定会帮助你......

答案 2 :(得分:2)

问题是您的OnClickListener viewHolder.DownloadButton也正在被回收。您应该在viewHolder.DownloadButton.setOnClickListener条件之外调用if/else,以便每次回收视图时都会设置一个新的侦听器。

答案 3 :(得分:0)

也许我错了,但是尝试更改您的getItemId方法,它不应该将位置恢复为id

public long getItemId(int position) {
    return 0;
}

我认为它会奏效。