Manikanta Reddy
Manikanta Reddy

Reputation: 641

how to display the textviewCount in listview items in android?

I have totally four items in my listview like wash dry stream and image,i will the all the information from the server and display in listview

                  Rs 50          Rs 50        Rs 50
    image1        wash1          dry1        stream1

                  Rs 50          Rs 50        Rs 50
    iamge2        wash2          dry2        stream2


                  Rs 50          Rs 50        Rs 50
    image3        wash3          dry3        stream3


                  Rs 50          Rs 50        Rs 50
    iamge4        wash4          dry4        stream4

like that

first row items

in my xml i will take like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="0dp"
            android:layout_height="77dp"
            android:layout_weight="0.2" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".05"
            android:src="@drawable/divider" />

        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvVal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/circle_item"
                android:gravity="center"
                android:padding="3dp"
                android:text="ff"
                android:textColor="@android:color/white"
                android:textSize="16dp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="99.99"
                android:textColor="#000000"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Laundry"
                android:textColor="#3A9ED4"
                android:textSize="10dp" />
        </LinearLayout>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".05"
            android:src="@drawable/divider" />

        <LinearLayout
            android:id="@+id/ll2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvVal1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/circle_item"
                android:gravity="center"
                android:padding="3dp"
                android:text="ff"
                android:textColor="@android:color/white"
                android:textSize="16dp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/tv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="99.99"
                android:textColor="#000000"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tv4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Dry Clean"
                android:textColor="#3A9ED4"
                android:textSize="10dp" />
        </LinearLayout>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".05"
            android:src="@drawable/divider" />

        <LinearLayout
            android:id="@+id/ll3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="0.25"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvVal2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/circle_item"
                android:gravity="center"
                android:padding="3dp"
                android:text="ff"
                android:textColor="@android:color/white"
                android:textSize="16dp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/tv5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="99.99"
                android:textColor="#000000"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/tv6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Stream Iron"
                android:textColor="#3A9ED4"
                android:textSize="10dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

i will take four layouts each item will be placed in single layout

Adapter class

public class CustomAdapter extends BaseAdapter {

    ArrayList<String> p_id;
    ArrayList<String> p_name;
    Context context;
    ArrayList<String> imageId;
    ArrayList<String> wash_v;
    ArrayList<String> dry_v;
    ArrayList<String> iron_v;
    Holder holder;
    Typeface tf;
    ProgressDialog dialog;
    Intent i;
    float washItemCount = 0.0f;
    int count = 0;
    private static LayoutInflater inflater = null;

    Handler handler;
    Runnable myRunnable;

    public CustomAdapter(Context con, ArrayList<String> pid,
            ArrayList<String> pname, ArrayList<String> pimg,
            ArrayList<String> wash, ArrayList<String> dry,
            ArrayList<String> iron) {
        // TODO Auto-generated constructor stub
        p_id = pid;
        context = con;
        p_name = pname;
        imageId = pimg;
        wash_v = wash;
        dry_v = dry;
        iron_v = iron;
        // tf = Typeface.createFromAsset(con.getAssets(), "TimmanaRegular.ttf");
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Log.i("Image Size", "" + pimg.size());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imageId.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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

    public class Holder {
        TextView tv1, tv2, tv3, tvVal, tvVal1, tvVal2;
        ImageView img;
        LinearLayout ll1, ll2, ll3;
    }

    @SuppressLint("UseValueOf")
    @Override
    public View getView(final int position, final View convertView,
            ViewGroup parent) {
        holder = new Holder();

        final View rowView;

        rowView = inflater.inflate(R.layout.dynamic, null);
        holder.ll1 = (LinearLayout) rowView.findViewById(R.id.ll1);
        holder.ll2 = (LinearLayout) rowView.findViewById(R.id.ll2);
        holder.ll3 = (LinearLayout) rowView.findViewById(R.id.ll3);
        holder.tv1 = (TextView) rowView.findViewById(R.id.tv1);
        holder.tv2 = (TextView) rowView.findViewById(R.id.tv3);
        holder.tv3 = (TextView) rowView.findViewById(R.id.tv5);
        holder.img = (ImageView) rowView.findViewById(R.id.ivImage);

        holder.tvVal = (TextView) rowView.findViewById(R.id.tvVal);
        holder.tvVal1 = (TextView) rowView.findViewById(R.id.tvVal1);
        holder.tvVal2 = (TextView) rowView.findViewById(R.id.tvVal2);

        holder.tv1.setText(wash_v.get(position));
        holder.tv2.setText(dry_v.get(position));
        holder.tv3.setText(iron_v.get(position));
        // holder.img.setImageDrawable(imageId.get(position));

        holder.ll1.setTag(new Integer(position));
        holder.ll2.setTag(new Integer(position));
        holder.ll3.setTag(new Integer(position));

        i = new Intent("com.example.safewash");
        final String image = imageId.get(position).toString();
        Log.i("Image in String", "" + image);

        holder.ll1.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {

                int colorStart = R.color.green;
                int colorEnd = Color.WHITE;

                ValueAnimator animator = ObjectAnimator.ofInt(v,
                        "backgroundColor", colorStart, colorEnd);
                animator.setDuration(800);
                animator.setEvaluator(new ArgbEvaluator());
                animator.setRepeatCount(0);
                animator.start();

                String washItem = wash_v.get(position);
                int washItems = Integer.parseInt(washItem);

                if (washItems != 0) {

                    washItemCount = Float.parseFloat(washItem);
                    i.putExtra("washItemCount", washItemCount);
                    context.sendBroadcast(i);
                    holder.tvVal.setVisibility(View.VISIBLE);
                    int countVal = ++count;

                    holder.tvVal.setText("" + countVal);

                }

            }

        });
        holder.ll2.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {
                String washItem = dry_v.get(position);
                int colorStart = R.color.green;
                int colorEnd = Color.WHITE;

                ValueAnimator animator = ObjectAnimator.ofInt(v,
                        "backgroundColor", colorStart, colorEnd);
                animator.setDuration(800);
                animator.setEvaluator(new ArgbEvaluator());
                animator.setRepeatCount(0);
                animator.start();
                washItemCount = Float.parseFloat(washItem);
                int washItems = Integer.parseInt(washItem);
                if (washItems != 0) {

                    washItemCount = Float.parseFloat(washItem);
                    i.putExtra("washItemCount", washItemCount);
                    context.sendBroadcast(i);
                    holder.tvVal.setVisibility(View.VISIBLE);
                    int countVal = ++count;
                    holder.tvVal1.setText("" + countVal);
                }



            }

        });

        holder.ll3.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {

                int colorStart = R.color.green;
                int colorEnd = Color.WHITE;

                ValueAnimator animator = ObjectAnimator.ofInt(v,
                        "backgroundColor", colorStart, colorEnd);
                animator.setDuration(800);
                animator.setEvaluator(new ArgbEvaluator());
                animator.setRepeatCount(0);
                animator.start();
                String washItem = iron_v.get(position);
                washItemCount = Float.parseFloat(washItem);
                int washItems = Integer.parseInt(washItem);
                if (washItems != 0) {

                    washItemCount = Float.parseFloat(washItem);
                    i.putExtra("washItemCount", washItemCount);
                    context.sendBroadcast(i);
                    holder.tvVal.setVisibility(View.VISIBLE);

                    int countVal = ++count;
                    holder.tvVal2.setText("" + countVal);


                }
            }
        });

        ImageDownloadTask imageDownloadTask = new ImageDownloadTask();
        imageDownloadTask.execute(image);

        return rowView;
    }

    public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... urls) {
            Bitmap map = null;
            for (String url : urls) {
                map = downloadImage(url);

                Log.i("BitMap in DoinBackGround::", "" + map);
            }
            return map;
        }

        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(Bitmap result) {

            BitmapDrawable ob = new BitmapDrawable(context.getResources(),
                    result);

            holder.img.setBackgroundDrawable(ob);
            super.onPostExecute(result);
        }

    }

    public Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);

            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream getHttpConnection(String urlString) throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }

}

Here my problem is

when i click on image1 in firstrow i display the count no as 1 like for every click the number will be increase like wise.....

                1              1            1
              Rs 50          Rs 50        Rs 50
image1        wash1          dry1        stream1

                1              2           4 
              Rs 50          Rs 50        Rs 50
iamge2        wash2          dry2        stream2


                5              2            2
              Rs 50          Rs 50        Rs 50
image3        wash3          dry3        stream3


                2              3            4   
              Rs 50          Rs 50        Rs 50
iamge4        wash4          dry4        stream4

any one help me guys...... thanks in advance..

Upvotes: 0

Views: 67

Answers (2)

arun
arun

Reputation: 1758

Replace the following code

holder.tvVal1.setText("" + countVal);

with this

TextView tvVal1 = (TextView) v.findViewById(R.id.tvVal1);
int countVal = Integer.parseInt(tvVal1.getText().toString());
tvVal1.setText("" + (countVal+1));    

tvVal1 default text 0 same for each tvVal2 and tvVal3

Upvotes: 1

AnswerDroid
AnswerDroid

Reputation: 1893

You have to set OnClickListener to that and inside the onClick method just maintain a global counter initialised to zero.
Everytime; increment it by one.

Upvotes: 0

Related Questions