1

Question is i'm trying to update all the buttons in by listview, buttons are in CustomAdapter which i've written, but buttons are not being updated. So the idea is to change the text of all buttons in the ListView and show the timer after button was clicked, can anyone tell what i'm doing wrong? Here's the code:

    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;
    }
}
Navdeep Soni
  • 449
  • 4
  • 15
k.nadonenko
  • 628
  • 2
  • 7
  • 23

3 Answers3

1

Have a look at this methode notifyDataSetChanged()

LE GALL Benoît
  • 7,159
  • 1
  • 36
  • 48
1

Try:

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

    }
        });
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
  • It will update only button which i clicked, but i need all the buttons from the listview. – k.nadonenko Sep 09 '14 at 09:44
  • for that you will need to run a for loop for every list item button to `setText()` inside the CountdownTimer `View v = listViewItems.getChildAt(position - listViewItems.getFirstVisiblePosition());` Take a look at [this post](http://stackoverflow.com/a/9987714/3326331) – Sagar Pilkhwal Sep 09 '14 at 10:03
1

to update the data in your adapter you need to use notifyDataSetChanged and regarding with the views you need to use invalidateviews

Ker p pag
  • 1,568
  • 12
  • 25