繁体   English   中英

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

[英]Update view inside custom adapter, after pressing the button

按下自定义适配器内的按钮后,我需要更新进度栏。 只有当我将视图值声明为final时,我才能这样做。 但这不适合我。 有什么正确的解决方案来做到这一点? 我的自定义适配器源:

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

}

view变量在该类中是全局变量,因此无需使其最终化。 您应该将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.删除视图视图; 从成员关闭类并在getview方法中。 因为当填充列表视图时,视图引用最后一个称为getView的项目。 因此,适配器类的视图成员是没有意义的。 2.更改类,如下所示:

公共类DownloadListAdapter扩展BaseAdapter实现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.在您的dowload方法上:

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

如果我误解了您的代码/您的目的,请告诉我!

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

希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM