How can i use Button OnClick in Custom ListView with View Holder

亡梦爱人 提交于 2019-12-24 04:15:08

问题


I need some help, i am using a viewholder to display from a dynamic arrayadapter.

I have list view .

Each row contains ;

  • Title (Textview),
  • sub title(TextView),
  • Progress bar
  • Download Button (Button).

I want to show progress bar and hide Download Button when Download Button is clicked. When download button in first row clicked, first progress bar is showing but 8th progress bar is showing too.

This is my code. what i am doing wrong?

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

回答1:


The problem is that your OnClickListener for viewHolder.DownloadButton is being recycled too. You should call viewHolder.DownloadButton.setOnClickListener outside of the if/else condition so that it sets a new listener each time a view is recycled.




回答2:


That's my solution. Thanks again @waqaslam and @ SweetWisher ツ for help.

Row Object (i have getters and setters for all of my variables):

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

GetView Method:

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

And then i will use AsyncTask like This Post




回答3:


I think you have action in list item click events like,

   watchListView.setOnItemClickListener(new OnItemClickListener() {

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

If so focus will prefer to list item click no the button click,

So better you replace the button to textview(desgined like button)

Try this, It will surely help you out...




回答4:


Maybe I am wrong, but try to change your getItemId method, it shouldn't return the position as id:

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

I think it will work.



来源:https://stackoverflow.com/questions/26912523/how-can-i-use-button-onclick-in-custom-listview-with-view-holder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!