【问题标题】:Update buttons in CustomAdapterCustomAdapter 中的更新按钮
【发布时间】:2014-09-09 09:26:02
【问题描述】:

问题是我正在尝试通过 listview 更新所有按钮,按钮在我编写的 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;
    }
}

【问题讨论】:

  • 你有 //updateGUI(buttonShare); 评论了方法调用
  • @SagarPilkhwal 我暂时不使用此方法,我需要在所有按钮中启动计时器。
  • 所以你想在每个按钮被点击时启动一个倒计时?
  • @SagarPilkhwal 是的,并从 listview 更新所有按钮上的文本。

标签: java android listview baseadapter


【解决方案1】:

看看这个方法notifyDataSetChanged()

【讨论】:

  • 谢谢,你的回答对我很有帮助:)
【解决方案2】:

试试:

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

    }
        });

【讨论】:

  • 它只会更新我点击的按钮,但我需要列表视图中的所有按钮。
  • 为此,您需要为 CountdownTimer setText() 内的每个列表项按钮运行 for 循环 View v = listViewItems.getChildAt(position - listViewItems.getFirstVisiblePosition()); 看看 this post
【解决方案3】:

要更新适配器中的数据,您需要使用notifyDataSetChanged,而关于视图,您需要使用invalidateviews

【讨论】:

  • 谢谢你的回答对:)非常有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
相关资源
最近更新 更多