0

我有一个ArrayAdapter<MyService>和每个项目,listView我有一个按钮执行异步调用以通过 http 请求下载文件,这工作得很好。

现在,我在 Activity 中创建了一个“全部下载”按钮,我希望这个按钮能够执行 Activity 中的所有项目listView,但我不知道该怎么做。我应该遍历列表,获取按钮视图并调用它的点击吗?

我想提出一些建议。

4

2 回答 2

2

我不认为迭代和伪造点击是一个好主意。我将为“全部”按钮实现一个单独的过程,该过程获取支持适配器的数据(在您的情况下,它是数组,对)并执行一些逻辑以批量下载它或(更可取的 IMO),例如,将文件 ID 列表发送到服务器并使服务器返回 ZIP。

于 2013-05-14T11:14:52.327 回答
0

请检查我用来 Iteare BaseAdapter 上的 Click 事件的示例代码

package com.nttdata.app;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.nttdata.graphics.DrawShape;

public class CategoryAdapter extends BaseAdapter {

    private Context context =null;
    private String[] categoryValues=null;


    public CategoryAdapter(Context context, String[] categoryValues) {
        this.context=context;
        this.categoryValues= categoryValues;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return categoryValues.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.category, null);

        // set value into textview


         ShapeDrawable sd = DrawShape.GetRoundShape();
         sd.getPaint().setColor(Color.parseColor("#33B2D3"));

        Log.d("DEBUG - CategoryAdapter",String.valueOf(categoryValues[position]));

        Button textView = (Button) gridView.findViewById(R.id.category_item_label);
        textView.setText(categoryValues[position]);
        textView.setTextSize(16);
        textView.setTextColor(Color.parseColor("#FFFFFF"));
        textView.setBackgroundColor(Color.parseColor("#33B2D3"));
        textView.setTypeface(null,Typeface.BOLD);
        textView.setBackgroundDrawable(sd);

        textView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                try{    

                        Intent productsIntent = new Intent(context, ProductActivity.class);
                        Bundle param = new Bundle();
                        param.putInt("Categories", 1); //Your id
                        param.putCharSequence("CategoryName", categoryValues[position]);
                        productsIntent.putExtras(param);
                        MainTabActivity.productSpec.setContent(productsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                        MainTabActivity.tabHost.setCurrentTab(1);


                }catch (Exception e) {
                    Toast.makeText(context, "Error in Setting Values to Product Activity ", Toast.LENGTH_LONG).show();
                    Log.d("DEBUG_Dapter Exaception", e.getMessage().toString());
                }

               // Toast.makeText(context, categoryValues[position], Toast.LENGTH_LONG).show();

            }



            });

        // set image based on selected text


    } else {
        gridView = (View) convertView;
    }

    return gridView;

    }

}

活动中的使用

GridView categoryView = new GridView(CategoryActivity.this);
         categoryView.setNumColumns(2);
         categoryView.setGravity(Gravity.CENTER);
         categoryView.setColumnWidth(100);
         categoryView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
         categoryView.setBackgroundResource(R.drawable.bg);

         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);

        layoutParams.setMargins(12, 20, 6, 20);
        categoryView.setLayoutParams(layoutParams);



        categoryView.setAdapter(new CategoryAdapter(this, category_List));

        categoryView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
                Toast.makeText(
                           CategoryActivity.this,
                           ((Button) v.findViewById(R.id.category_item_label))
                           .getText(), Toast.LENGTH_SHORT).show();

            }
        });
于 2013-05-14T11:18:21.853 回答