Mohamed Atef
Mohamed Atef

Reputation: 167

Counter in TextView within ListView

I want to create a ListView with TextView and button. When I press on item button the TextView start to update and other textviews don't update with it too. it should only the textview on the item with the button i clicked change. other textviews also update when i scroll within the listview

In other way I want to create MediaPlayer and handle the time for each song. I tried many ways, none of them working. Some of them update random texts, others update other texts when I scroll. I also tried another way with notifydata every update (every second) but it prevents me from pressing on other buttons (it hangs).

This is my adapter:

            import java.util.ArrayList;
            import android.annotation.SuppressLint;
            import android.content.Context;
            import android.os.Handler;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.BaseAdapter;
            import android.widget.ImageView;
            import android.widget.TextView;
            import android.view.View.OnClickListener;

            public class Adapter_G extends BaseAdapter {
                private static ArrayList<Cool_Item_G> itemDetailsrrayList;
                private Handler durationHandler;
                private LayoutInflater l_Inflater;
                Context context2;
                TextView texts;
                int cn = 0;

                public Adapter_G(Context context, ArrayList<Cool_Item_G> results) {
                    itemDetailsrrayList = results;
                    l_Inflater = LayoutInflater.from(context);
                    context2 = context;
                    durationHandler = new Handler();

                }

                public int getCount() {

                    try {
                        return itemDetailsrrayList.size();
                    } catch (Exception e) {
                        return 0;
                    }
                }

                public Object getItem(int position) {
                    return itemDetailsrrayList.get(position);
                }

                public long getItemId(int position) {
                    return position;
                }

                public View getView(final int position, View convertView, ViewGroup parent) {
                    final ViewHolder holder;

                    if (convertView == null) {
                        convertView = l_Inflater.inflate(R.layout.zerk_item, null);
                        holder = new ViewHolder();
                        holder.go = (ImageView) convertView.findViewById(R.id.go);
                        holder.cool = (TextView) convertView.findViewById(R.id.Cool);

                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }

                    holder.Cool.setText("" + itemDetailsrrayList.get(position).z);

                    holder.go.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            goNow(position, holder.Cool);
                        }
                    });

                    return convertView;
                }

                static class ViewHolder {
                    TextView cool;
                    ImageView go;

                }

                Handler m_handler;
                int zs = 0;
                Runnable m_handlerTask = null;

                public void goNow(final int position, final TextView text) {

                    m_handler = new Handler();
                    m_handlerTask = new Runnable() {
                        @Override
                        public void run() {

                            text.setText("" + zs);
                            zs++;
                            m_handler.postDelayed(m_handlerTask, 1000);

                        }
                    };
                    m_handlerTask.run();

                }
            }

Upvotes: 0

Views: 78

Answers (1)

faranjit
faranjit

Reputation: 1637

Try to change this

text.setText("" + zs);
zs++;

to

itemDetailsrrayList.get(position).z += itemDetailsrrayList.get(position).z;
notifyDataSetChanged();

Upvotes: 1

Related Questions