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

时间:2015-08-05 01:43:01

标签: java android adapter

我按下自定义适配器内的按钮后需要更新进度条。 只有当我宣布视图值为最终我才能做到这一点。但它不适合我。 有什么方法可以做到这一点? 我的自定义适配器源:

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

}

2 个答案:

答案 0 :(得分:0)

视图变量在类中是全局的,因此无需使其成为最终的。 您应该将progressBar作为final,并在按钮onClick方法

中更改其进度

答案 1 :(得分:0)

据我所知,当我阅读您的代码时,您想要启动下载指示符(在这种情况下,指标是您的进度条)。所以你可以尝试这种方式:

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。更改这样的课程:

public class DownloadListAdapter extends BaseAdapter实现了DownloadManagerListener {

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

3。 Chang 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
}

如果我错过了解您的代码/您的目的,请告诉我们。

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

希望有所帮助!