How can I use Button OnClick in custom ListView with view holder
I need help, I am using a viewer to display from a dynamic array.
I have a list.
Each line contains;
- Title (Text Representation),
- subtitle (TextView),
- Progress bar
- Download button (button).
I want to show a progress bar and hide the download button when the download button is clicked. When the download button on the first line is pressed, the first progress bar is shown, but the 8th progress bar is shown.
This is my code. what am I 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;
}
}
source to share
This is my decision. Thanks again @waqaslam and @SweetWisher ツ for the help.
Row Object (I have getters and setters for all 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
source to share
I think you have an action in the click events of a list item, for example
watchListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
.....................
}
});
If so the focus prefers the list item, click the button without clicking the button,
So it's better to replace the button with a text box (like a button)
Try this, it will surely help you ...
source to share