2

我需要一些帮助,我正在使用查看器从动态阵列适配器显示。

我有列表视图。

每行包含 ;

  • 标题(文本视图),
  • 副标题(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

4 回答 4

2

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

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

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

获取视图方法:

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

然后我会像这篇文章一样使用 AsyncTask

于 2014-11-14T08:49:07.197 回答
2

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

于 2014-11-13T16:05:27.103 回答
1

我认为您在列表项单击事件中采取了行动,例如,

   watchListView.setOnItemClickListener(new OnItemClickListener() {

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

如果是这样,焦点会更喜欢列出项目单击而不单击按钮,

所以最好将按钮替换为 textview(设计为按钮)

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

于 2014-11-13T15:41:34.023 回答
0

也许我错了,但尝试改变你的getItemId方法,它不应该将位置返回为id

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

我认为它会起作用。

于 2014-11-13T16:03:52.260 回答