2

我使用自定义类来填充 ListView 类上的适配器,如下所示:

package com.example.raidplanner;

public class RaidWpis {
    private int id;
    private int id_gildia;
    private String nazwa;
    private int schemat;
    private int data_zapis;
    private int data_start;
    private int opis;
    private int id_officer;
    private int nick_officer;
    private int typ;

    public RaidWpis(int id,String nazwa) {
        setNazwa(nazwa);
        setId(id);
    }

    public int getId(){
        return id; 
    }       

    public void setId(int id){
        this.id = id; 
    }

    public String getNazwa() {
        return nazwa;}

    public void setNazwa(String nazwa) {
        this.nazwa = nazwa;
    }

    public String toString() {
        return this.nazwa;
    }
    public String toString2() {
        return this.id+" - "+nazwa;
    }

}

在我的活动中,我使用此代码

RaidWpis[] items = {
    new RaidWpis(1, "aaaa"),
    new RaidWpis(3, "bbbb"),
    new RaidWpis(6, "cccc"),
    new RaidWpis(11, "dddd"),
    new RaidWpis(17, "eeee"),
};

mainListView = (ListView) findViewById( R.id.mainListView );

ArrayAdapter<RaidWpis> raidList = new ArrayAdapter<RaidWpis>(this, R.layout.simplerow, items);

// Create ArrayAdapter using the raid list.
mainListView.setAdapter(raidList);

现在如何将新项目添加到项目数组。最后,我想用来自 json 数据的数据(从 PHP 传递)填充该项目数组

4

1 回答 1

2

我做到了,只是将一些代码更改为:

ArrayList<RaidWpis> raid_list = new ArrayList<RaidWpis>();
    raid_list.add(new RaidWpis(1, "aaaa"));
    raid_list.add(new RaidWpis(3, "bbb"));
    raid_list.add(new RaidWpis(5, "ccc"));
    ArrayAdapter<RaidWpis> raidList=new ArrayAdapter<RaidWpis>(this, R.layout.simplerow, raid_list);  

于 2013-01-15T18:59:10.680 回答