在 Android 中为 ArrayList 设置自定义适配器

Setting up Custom adapter for ArrayList in Android

我正在尝试为我的 activity 实现自定义适配器,它将从两个 ArrayList 填充列表视图。但是我完全不知道下一步该怎么做? 这是我的代码:

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

我无法在这里实现 getView() 方法..任何人有什么想法吗?

创建 class 用于保存数据。

public class Data {
    private String body;
    private String address;

    public Data(String body, String address) {
        this.body= body;
        this.address = address;
    }

    public String getBody() { return body; }

    public String getAddress() { return address; }
}

然后将数据加载到该对象中并将它们添加到列表中。

public class Offers extends Activity {
    //...
    ArrayList<Data> dataList = new ArrayList<Data>();
    //...
    @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") || body.contains("unlimited") || body.contains("OFF") || body.contains("off") || body.contains("lowest") || body.contains("save") || body.contains("CHEAPER"))
            {
                dataList.add(new Data(body, address));
            }
        }

        latesttrans.setAdapter(new MyCustomAdapter(this, dataList));
    }

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

然后您可以创建 ArrayAdapter 而不是 BaseAdapter

public class MyCustomAdapter extends ArrayAdapter<Data> {

    public class ViewHolder {
        TextView address;
        TextView body;
    }

    public MyCustomAdapter(Context context, List<Data> objects) {
        super(context, R.layout.single_row_offers, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        ViewHolder viewHolder;

        if(convertView == null) {
            row = LayoutInflater.from(getContext()).inflate(R.layout.single_row_offers, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.address = row.findViewById(R.id.textViewSingleOffer1);
            viewHolder.body = row.findViewById(R.id.textViewSingleOffer2);

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

        Data item = getItem(position);

        viewHolder.address.setText(item.getAddress());
        viewHolder.body.setText(item.getBody());

        return row;
    }
}

您的适配器:

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