列表视图中显示包名称的项目

时间:2017-12-12 16:04:44

标签: android listview

我想将我的json数据显示给listview。这是我尝试的

   private void loadIntoListView(String json) throws JSONException {
        List<Itemadapter> items = new ArrayList<Itemadapter>();
        JSONArray jsonArray = new JSONArray(json);
        String[] ba = new String[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            String code     = obj.getString("ItemCode");
            String name     = obj.getString("ItemName");
            String photo    = obj.getString("PhotoName");
            items.add(new Itemadapter(code, name,photo));
            Log.i(code, code);

        }
        Toast.makeText(getActivity(), items.toString(), Toast.LENGTH_LONG).show();

        ArrayAdapter<Itemadapter> mArrayAdapter = new ArrayAdapter<Itemadapter>(getActivity(),android.R.layout.simple_expandable_list_item_1,items);
        listView.setAdapter(mArrayAdapter);
    }

我的Itemadapter

public class Itemadapter {

    String ItemCode;
    String ItemName;
    String ItemPhoto;

    public Itemadapter(String ItemCode, String ItemName, String ItemPhoto) {
        this.ItemCode   = ItemCode;
        this.ItemPhoto  = ItemPhoto;
        this.ItemName   = ItemName;
    }

    public String getCode() {
        return ItemCode.toString();
    }

    public String getItemPhoto() {
        return ItemPhoto.toString();
    }

    public  String getItemName(){
        return ItemName.toString();
    }

}

当我运行它时,没有错误。但是,在我listview中,我在multiplerows中看到类似的东西

  

com.example.boby.firstapp.Itemadapter @(somecode)

我该如何解决?我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

您需要覆盖toString()中的Itemadapter

public class Itemadapter {

    String ItemCode;
    String ItemName;
    String ItemPhoto;

    public Itemadapter(String ItemCode, String ItemName, String ItemPhoto) {
        this.ItemCode   = ItemCode;
        this.ItemPhoto  = ItemPhoto;
        this.ItemName   = ItemName;
    }

    public String getCode() {
        return ItemCode.toString();
    }

    public String getItemPhoto() {
        return ItemPhoto.toString();
    }

    public  String getItemName(){
        return ItemName.toString();
    }

    @Override
    public String toString() {
        return ItemCode + " " + ItemName;

}

答案 1 :(得分:1)

ArrayAdapter<Itemadapter> mArrayAdapter = new ArrayAdapter<Itemadapter>(getActivity(),android.R.layout.simple_expandable_list_item_1,items);

此错误是由于此行而产生的。让我解释。如果你想在listview中显示itemCode,那么代码必须是这样的。

ArrayList<String> itemsCode = new ArrayList<>();   
for(int i=0;i<items.size();++i){
    itemsCode.add(item.get(i).getCode());
}
ArrayAdapter<> mArrayAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,itemsCode);

第三个参数应该是您想要显示的内容

答案 2 :(得分:0)

错误发生在ItemAdapter类的getters方法上,从所有getter中移除.toString()

public class Itemadapter {

    String ItemCode;
    String ItemName;
    String ItemPhoto;

    public Itemadapter(String ItemCode, String ItemName, String ItemPhoto) {
        this.ItemCode   = ItemCode;
        this.ItemPhoto  = ItemPhoto;
        this.ItemName   = ItemName;
    }

    public String getCode() {
        return ItemCode;
    }

    public String getItemPhoto() {
        return ItemPhoto;
    }

    public  String getItemName(){
        return ItemName;
    }

}

我希望它会对你有所帮助!