Android向列表视图动态添加项目

Android向列表视图动态添加项目

本文介绍了使用arrayadapter Android向列表视图动态添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Custom类在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传递)中的数据填充该项目数组.

解决方案

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

Im using Custom class to fill Adapter on ListViewClass looks like that:

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

}

In my activity Im using this code

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

Now how to add new items to items array. Finaly I want to fill that items array with data from json data (passed from PHP)

解决方案

I did it, just changed some code to that:

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

这篇关于使用arrayadapter Android向列表视图动态添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:32