0

This is my adapter class i m beginer in android and i want to add multiple textview in listview using view holder and also a button should be there

   package com.example.navigationdrawerexample;

    /**
     * Created by Dev2 on 12/7/2015.
     */

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class listadapterfdgdfg extends ArrayAdapter<String> {
    customButtonListener customListner;

    public interface customButtonListener {
        public void onButtonClickListner(int position,String value);
    }

    public void setCustomButtonListner(customButtonListener listener)
    {
        this.customListner = listener;
    }

    private Context context;

    private ArrayList<String> data = new ArrayList<String>();
    private ArrayList<String> Price = new ArrayList<String>();

    public listadapterfdgdfg(Context context, ArrayList<String> dataItem,ArrayList<String> price) {
        super(context, R.layout.child_row,dataItem);
        this.data = dataItem;
        this.Price=price;
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        ViewHolder viewHolder2;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.child_row, null);
            viewHolder = new ViewHolder();
            viewHolder2 = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
            viewHolder.prices =(TextView)convertView.findViewById(R.id.pricemine);

            //viewHolder2.prices= (TextView)convertView.findViewById(R.id.pricemine);
            viewHolder.button = (Button) convertView.findViewById(R.id.childButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder2 = (ViewHolder) convertView.getTag();
        }
        final String temp = getItem(position);

        viewHolder.text.setText(temp);

        final String temp1 =getItem(position);

        viewHolder.prices.setText(temp1);

        viewHolder.button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (customListner != null) {
                    customListner.onButtonClickListner(position,temp);
                }
            }
        });

        return convertView;
    }

    public class ViewHolder {
        TextView text;
        TextView prices;
        Button button;
    }
}

here I call this class in that way

I add textview of price in xml also add price array list in constructor of listadapter it shows data of dataitems in screeen it shows same item name as price i know the problem but dont know how to tackle this the problem is here

public listadapterfdgdfg(Context context, ArrayList<String>   dataItem,ArrayList<String> price) {
        super(context, R.layout.child_row,dataItem);
        this.data = dataItem;
        this.Price=price;
        this.context = context;
    }

It show price as if i declare price data it show itmename as if I declare itemlis data

enter image description here

wan to add price of items

Cœur
  • 37,241
  • 25
  • 195
  • 267
Usman Asif
  • 111
  • 1
  • 1
  • 9
  • take a look at http://www.tutorialspoint.com/android/android_list_view.htm – Sathish Kumar Dec 11 '15 at 07:33
  • I think you should use one Model class to hold both data and price strings. then you can use BaseAdapeter and ovveride the getView method. one viewHolder class, itemOnClickListner, Class name with java naming convention. – NaserShaikh Dec 11 '15 at 07:41
  • why are you declaring 2 view holder objects? use 1 that contains all the views, for filling data in view, get an item from price array, set it in price view, then get item from name array and set it in name array, don't use getTag 2 times, also use 1 class for data, contains 2 arrays, one for price and 1 for data – Yazan Dec 13 '15 at 09:17
  • or even better, create an object class say `MyDataObject` that hold 2 memebers, price and name, and pass arraylist to the adapter, and use it to populate view – Yazan Dec 13 '15 at 09:22
  • i exactly think the same thing but when i declare arraylist public listadapterfdgdfg(Context context, ArrayList dataItem) super(context, R.layout.child_row, dataItem.toString()); In this the problem is super(method) only want ArrayList not any other arraylist like arraylisT(view holder) @Yazan – Usman Asif Dec 15 '15 at 05:23
  • 1) you don't need arraylist 2) you need to change the adapter class type from to so the super() will accept the new array list – Yazan Dec 15 '15 at 07:39
  • i have posted a full answer explains all changes you need to do – Yazan Dec 15 '15 at 07:47

1 Answers1

1

here is a full answer of what i told you:

1- the model class for data

public class MyData{
    private String name = "";
    private double price = 0;

    //Setters and Getters...
}

2- the adapter class, rename to MyDataListAdapter

public class MyDataListAdapter extends ArrayAdapter<MyData> {
    private ArrayList<MyData> data = null;

    public MyDataListAdapter(Context context, ArrayList<MyData> dataList) {
        super(context, R.layout.child_row,dataList);
        this.data = dataList;
        this.context = context;
    }//constructor


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.child_row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
            viewHolder.prices =(TextView)convertView.findViewById(R.id.pricemine);
            viewHolder.button = (Button) convertView.findViewById(R.id.childButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //you can use data.get(position) too
        final MyData myDataItem = getItem(position);

        viewHolder.text.setText(myDataItem.getName());
        viewHolder.prices.setText(myDataItem.getPrice());
        viewHolder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (customListner != null) {
                    //pass name or price, or pass myDataItem object and handle it at onButtonClickListner()
                    customListner.onButtonClickListner(position,myDataItem.getName());
                }

            }
        });

        return convertView;
    }

    public class ViewHolder {
        TextView text;
        TextView prices;
        Button button;
    }
}//MyDataListAdapter

now from activity or any place, instantiate adapter

ArrayList<MyData> alldata = new ArrayList();
alldata.add(dataItem1); //.. add all items needed
MyDataListAdapter adapter = new MyDataListAdapter (context, alldata);
mListView.setAdapter(adapter);
Yazan
  • 6,074
  • 1
  • 19
  • 33