简体   繁体   中英

ListActivity with custom object android

I want to populate a list activity class with my custom object Person , I wrote the following code

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    liPerson = new ArrayList<Person>();
    person = new Person("Pritom Kumar Mondal", "Software Engineer", "bd");
    liPerson.add(person);
    person = new Person("Pritom Kumar Mondal", "Software Engineer", "bd");
    liPerson.add(person);
    person = new Person("Pritom Kumar Mondal", "Software Engineer", "bd");
    liPerson.add(person);
    person = new Person("Pritom Kumar Mondal", "Software Engineer", "bd");
    liPerson.add(person);
    Person[] persons = (Person[]) liPerson.toArray();
    setListAdapter(new MyAdapter(this, 
            android.R.layout.simple_list_item_1,
            persons));
}

private class MyAdapter extends ArrayAdapter<Person> {
    private Person[] persons;
    public MyAdapter(Context context, int resource, Person[] persons) {
        super(context, resource, persons);
        // TODO Auto-generated constructor stub
        this.persons = persons;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_item, parent, false);
        //String[] items = getResources().getStringArray(R.array.countries);
        Person p = persons[position];
        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.textView1);

        tv.setText(p.getName());

        String mDrawableName = "bd";
        int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
        iv.setImageResource(resID);


        return row;
    }
}

It does not work. I can populate a list activity from my xml file, but I want this as my custom object.

Just pass the whole ArrayList to the ArrayAdapter. You can then get your object with getItem(position)."your method here"

ArrayList<Person> personList = new ArrayList<Person>();

    ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,
            R.layout.person_item, personList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row;

            if (null == convertView) {
                row = mInflater.inflate(R.layout.person_item, null);
            } else {
                row = convertView;
            }

            TextView tView = (TextView) row.findViewById(R.id.tvName);
            tView.setText(getItem(position).getName()); // This is where you get the name from your object

            return row;
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM