从嵌套数组中提取值

时间:2016-03-09 16:35:10

标签: java android arrays json loops

我有一个名为societies的数组:

List<Society> societies = new ArrayList<>();

这包含以下数据:

[{"society_id":1,"name":"TestName1","email":"Test@email1","description":"TestDes‌​1"},
{"society_id":2,"name":"TestName2","email":"Test@email2","description":"TestD‌​es2"},
{"society_id":3,"name":"TestName3","email":"Test@email3","description":"Tes‌​tDes3"}}

我将使用它来填充ListView但是在编写循环时遇到问题,该循环会将每个值数组分配到ListView中的位置。 我想找到一种方法从数组中提取值并使用循环将它们分配给每个列表项,是否有人可以帮助我?

我的代码(应该足够,但如果您需要查看更多内容请咨询):

public class SocietySearch extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_society_search);


        List<Society> societies = new ArrayList<>();
        ServerRequests serverRequest1 = new ServerRequests(SocietySearch.this);
        serverRequest1.GetSocietyDataAsyncTask(societies, new GetSocietyCallback() {
            @Override
            public void done(List<Society> societies) {
                ListView lv = (ListView) findViewById(R.id.ListView);
                List<ListViewItem> items = new ArrayList<>();

                items.add(new ListViewItem() {{
                    ThumbnailResource = R.drawable.test;
                    Title = societies.socName;
                    Subtitle = societies.socDes;
                }});

                CustomListViewAdapter adapter = new CustomListViewAdapter(SocietySearch.this, items);
                lv.setAdapter(adapter);
            }
        });
    }


    class ListViewItem {
        public int ThumbnailResource;
        public String Title;
        public String Subtitle;
    } 

适配器类:

public class CustomListViewAdapter extends ArrayAdapter {

    LayoutInflater inflater;
    List<SocietySearch.ListViewItem> items;

    public CustomListViewAdapter(Activity context, List<SocietySearch.ListViewItem> items) {
        super(context, R.layout.item_row);

        this.items = items;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //Auto-generated method stub

        ListViewItem item = items.get(position);

        View vi = convertView;

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row, null);

        ImageView test = (ImageView) vi.findViewById(R.id.imgThumbnail);
        TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
        TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);

        test.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.Subtitle);


        return vi;
    }
}

3 个答案:

答案 0 :(得分:1)

您在寻找自定义解决方案吗?因此,您需要将每个ListViewItem状态绑定到相应的列,然后您必须创建自定义解决方案,如here。您需要为每一行都有一个自定义布局,并扩展ArrayAdapter,在那里绑定一行的每一列。

这是你想知道的吗?如果没有,请你更具体一点。

答案 1 :(得分:1)

所以我们得出结论,我们需要让for循环遍历SocietySearch类中的所有Society类:

@Override
public void done(List<Society> societies) {
    ListView lv = (ListView) findViewById(R.id.ListView);
    List<ListViewItem> items = new ArrayList<>();
    for(Society s : societies) {
        items.add(new ListViewItem() {{
            ThumbnailResource = R.drawable.test;
            Title = s.socName;
            Subtitle = s.socDes;
        }});
    }
    CustomListViewAdapter adapter = new CustomListViewAdapter(
        SocietySearch.this, items);
    lv.setAdapter(adapter);
}`

我们还必须修复ArrayAdapter实现:

public class CustomListViewAdapter extends ArrayAdapter {

    LayoutInflater inflater;
    List<SocietySearch.ListViewItem> items;

    public CustomListViewAdapter(Activity context, List<SocietySearch.ListViewItem> items) {
        super(context, R.layout.item_row, **items**); // the constructor 
        //needs the reference of the list, even though we use our variable to 
        //populate the rows. I guess it has to know how many elements it contains to
        //iterate then through getView method, which is called for each row

        this.items = items;
        this.inflater = (LayoutInflater) context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    }

答案 2 :(得分:0)

您正在创建一个匿名类,并尝试在其对象初始值设定项中指定值。对象初始值设定项没有对societies的引用,这就是您收到编译错误的原因。试试这个:

class ListViewItem {
    private final int ThumbnailResource;
    private final String Title;
    private final String Subtitle;

    public ListViewItem(int thumbnail, String title, String subtitle) {
        ThumbnailResource = thumbnail;
        Title = title;
        Subtitle = subtitle;
    }
} 

添加列表项时:

items.add(new ListViewItem(R.drawable.test, societies.socName, societies.socDes);