按下按钮后更新自定义适配器内的视图

Update view inside custom adapter, after pressing the button

我需要在按下自定义适配器内的按钮后更新进度条。 只有当我将视图值声明为最终值时,我才能这样做。但它不适合我。 执行此操作的正确解决方案是什么? 我的自定义适配器来源:

public class DownloadListAdapter extends BaseAdapter implements DownloadManagerListener{

    Context ctx;
    LayoutInflater lInflater;
    ArrayList<Product> objects;
    View view;

    DownloadListAdapter(Context context, ArrayList<Product> products) {
        ctx = context;
        objects = products;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.item, parent, false);
        }

        Product p = getProduct(position);

        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        progressBar.setProgress(p.size);

        Button btSet = (Button) view.findViewById(R.id.btSet);

        btSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //this does not work
                progressBar.setProgress(100);
                //this doesn't work too, it will work only when view will declared as final
                ProgressBar pb = (ProgressBar) view.findViewById(R.id.progressBar);
                pb.setProgress(100);

            }
        });

        return view;
    }

    @Override
    public void OnDownloadStarted(long taskId) {
        //This method should also have access to view
    }

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

    @Override
    public Object getItem(int position) {

        return objects.get(position);
    }

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


    Product getProduct(int position) {
        return ((Product) getItem(position));
    }

}

视图变量在 class 中是全局变量,因此无需将其设为最终变量。 您应该将 progressBar 设置为 final 并在按钮 onClick 方法中更改其进度

据我阅读您的代码时了解,您想开始下载指示器(在本例中,指示器是您的进度条)。所以你可以这样试试:

1. 我假设这是您的 DownloadManagerListener 或者您可以将其更改为:

public interface DownloadManagerListener {

void OnDownloadStarted(long taskId, View view);

}

2. 像这样更改按钮侦听器:

btSet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    ProgressBar progress = ((LinearLayout) v.getParent())
                        .findViewById(R.id.progressBar);
    OnDownloadStarted(0, progress);

    }
});

在此我假设您的布局 (R.layout.item) 是这样的:

<LinelearLayout>
...
<ProgressBar android:id="@+id/progressBar"/> 
...
<Button android:id="@+id/btSet"/>
</LinearLayout>

3. 最后你的方法 OnDownloadStarted 是这样的:

public void OnDownloadStarted(long taskId, View view) {
    ProgressBar progressBar  = (ProgressBar) view;
    progressBar.setProgress(0);
}

更新 1:

基于作者评论。我建议以下几点:

1.移除View视图;来自成员 off class 和 getview 方法。因为当列表视图被填充时,查看对调用 getView 的最后一项的引用。所以adapter的视图成员class是没有意义的。 2. 改变 class 像这样:

public class DownloadListAdapter extends BaseAdapter implements DownloadManagerListener{

Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
HashMap<Integer, ProgressBar> mProductDownloaderMap = new HashMap<Integer, ProgressBar>();

3. 像这样更改 getView() 方法:

    if (convertView == null) {
    convertView = lInflater.inflate(R.layout.item, parent, false);
}

Product p = getProduct(position);

progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

Button btSet = (Button) view.findViewById(R.id.btSet);

mProductDownloaderMap.put(position, progressBar);

btSet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        OnDownloadStarted(position);// position is indicated taskID

    }
});

4. 在您的下载方法上:

public void OnDownloadStarted(long taskId) {
    ProgressBar progressBar = mProductDownloaderMap.get(taskId);
    progressBar.setProgress(0);
    //Your download code here
}

如果我误解了你的code/your目的,请告诉我!

如果我的回答适合你的问题,也不要忘记标记这是答案

希望对您有所帮助!