更新自定义适配器中的按钮

时间:2014-09-09 09:26:02

标签: java android listview baseadapter

问题是我正在尝试按列表视图更新所有按钮,按钮位于我已编写的CustomAdapter中,但按钮未更新。所以我的想法是改变ListView中所有按钮的文本,并在点击按钮后显示计时器,任何人都可以告诉我我做错了什么?这是代码:

    public class CustomAdapter extends BaseAdapter  {

    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    int secs, mins;

    public CustomAdapter(Context context,
                           ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        final Button buttonShare;
        TextView title;
        ImageView poster;
        View itemView = null;

        if (convertView == null) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.ads_layout, parent, false);
        } else {
            itemView = convertView;
        }

        resultp = data.get(position);

        // Настраиваем текстовые поля
        title = (TextView) itemView.findViewById(R.id.title);
        buttonShare = (Button) itemView.findViewById(R.id.postButton);


        // ImageView
        poster = (ImageView) itemView.findViewById(R.id.adImage);

        title.setText(resultp.get(AdsFragment.TAG_TITLE));
        imageLoader.DisplayImage(resultp.get(AdsFragment.TAG_PHOTO), poster);
        buttonShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Boolean posted = AdsFragment.getPosted();
                if (posted == false) {
                    context.startService(new Intent(context, BroadcastService.class));
                    Log.i("SERVICE", "Started service");
                    cdt.start();
                    //updateGUI(buttonShare);

                } else {
                    Log.i("POST", "WAS POSTED!!!");
                    new AlertDialog.Builder(context).setTitle("LALKA")
                            .setMessage("LALKA").setCancelable(true).show();
                }

            }
        });

        CountDownTimer cdt = new CountDownTimer(20000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                secs = (int) (millisUntilFinished / 1000);
                mins = secs / 60;
                secs = secs % 60;
                buttonShare.setText(" " + mins + " : " + String.format("%02d", secs));

            }

            @Override
            public void onFinish() {

            }

        };


        return itemView;
    }
}

3 个答案:

答案 0 :(得分:1)

看看这个方法notifyDataSetChanged()

答案 1 :(得分:1)

尝试:

buttonShare.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        Boolean posted = AdsFragment.getPosted();
        if (posted == false) {
            context.startService(new Intent(context, BroadcastService.class));
            Log.i("SERVICE", "Started service");
            CountDownTimer cdt = new CountDownTimer(20000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                secs = (int) (millisUntilFinished / 1000);
                mins = secs / 60;
                secs = secs % 60;
                buttonShare.setText(" " + mins + " : " + String.format("%02d", secs));

            }
            cdt.start();
            //updateGUI(buttonShare);

        } else {
            Log.i("POST", "WAS POSTED!!!");
            new AlertDialog.Builder(context).setTitle("LALKA")
                    .setMessage("LALKA").setCancelable(true).show();
        }

    }
        });

答案 2 :(得分:1)

要更新适配器中的数据,您需要使用notifyDataSetChanged并查看需要使用的视图invalidateviews