Search code examples
androidlistviewarraylistbaseadaptercustom-adapter

Setting up Custom adapter for ArrayList in Android


I am trying to implement custom adapter for my activity which will populate the listview from two ArrayLists. However I am totally confused as to what to do next ? Here is my code :

public class Offers extends Activity 
{
    ListView latesttrans;
    Boolean flagValue = false;

    ArrayList<String> listItems = new ArrayList<String>();
    ArrayList<String> listAddress = new ArrayList<String>();
    String[] addr;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offers);
        latesttrans = (ListView) findViewById(R.id.listView1);

        Uri latesttransuri = Uri.parse("content://sms/inbox");
        Cursor latesttranscursor = getContentResolver().query(latesttransuri, new String[]{"_id","address","date","body"}, null, null, null);

        if(flagValue == false)
        {
            latesttranscursor.moveToFirst();
            flagValue = true;
        }

        String address = null,body = null;
        int i=0;

            while(latesttranscursor.moveToNext())
            {
                address=latesttranscursor.getString(1);
                long date = latesttranscursor.getLong(2);
                String sdate = millisToDate(date);
                body = latesttranscursor.getString(3);

                if(body.contains("FREE"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("unlimited"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("unlimited"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("OFF"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("off"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("lowest"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("save"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }
                else if(body.contains("CHEAPER"))
                {
                    listItems.add(i,body);
                    listAddress.add(i,address);
                    i++;
                }

            }

        latesttrans.setAdapter(new MyCustomAdapter(this));
    }
    public String millisToDate(long currentTime) 
    {
        String finalDate;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(currentTime);
        Date date = calendar.getTime();
        finalDate = date.toString();
        return finalDate;
    }
} 

    class SingleRow2
    {
        String title;
        String description;
        SingleRow2(String title, String description)
        {
            this.title=title;
            this.description=description;
        }
    }
    class MyCustomAdapter extends BaseAdapter
    {
        int i=0;
        Boolean flagValue = false;
        ArrayList<SingleRow2> list;
        Context context;

        public MyCustomAdapter(Offers offers) 
        {
            // TODO Auto-generated constructor stub
        }

        public int getCount() 
        {
            return list.size();
        }

        public Object getItem(int arg0) 
        {
            // TODO Auto-generated method stub
            return list.get(arg0);
        }

        public long getItemId(int position) 
        {
            return position;
        }
        public View getView(int arg0, View arg1, ViewGroup viewGroup) 
        {
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.single_row_offers,viewGroup,false);

            TextView addresstext = (TextView) row.findViewById(R.id.textViewSingleOffer1);
            TextView bodytext = (TextView) row.findViewById(R.id.textViewSingleOffer2);


            return (row);
        }
}

I am unable to implement the getView() method here ..Anyone any ideas ?


Solution

  • Your adapter :

    class TestAdapter extends BaseAdapter {
    
        ArrayList<String> listItem;
        ArrayList<String> listAddress;
    
        Context mContext;
        //constructor
        public TestAdapter(Context mContext, ArrayList<String> listItem, 
        ArrayList<String> listAddress) {
            this.mContext = mContext;
            this.listItem = listItem;
            this.listAddress = listAddress;
        }
    
        public int getCount() {
            return listItem.size();
        }
    
        public Object getItem(int arg0) {
            return null;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View arg1, ViewGroup viewGroup) 
        {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.single_row_offers, viewGroup, false);
    
            TextView addresstext = (TextView) row.findViewById(R.id.textViewSingleOffer1);
            TextView bodytext = (TextView) row.findViewById(R.id.textViewSingleOffer2);
    
            bodytext.setText(listItem.get(position));
            addresstext.setText(listAddress.get(position));
    
    
            return row;
        }
    }
    
    latesttrans.setAdapter(new TestAdapter(this, listItem,listAddress));