从预先存在的arraylist创建自定义列表视图

时间:2014-03-28 05:29:35

标签: android listview arraylist

我在制作这个自定义适配器时显然遗漏了一些东西。基本上,我已经获得了从站点获取JSON的代码,然后将其分解为ArrayList。我可以做一个.toString()并将它全部吐出到listview上,但我在网上找到的用于创建自定义适配器的所有内容都会在其中创建列表。有没有办法只提供预先创建的列表?我还没有取得任何进展以使自定义工作正常...但是,正如我所说,我可以让字符串工作得很好,所以我将包含该代码。

这是我的课程:

public class JSONEvents {
String eid;
String bid;
private String bname;
private String valid;
@Override
public String toString() {
    return "Events [eid=" + eid + ", bid=" + bid + ", bname=" + bname
            + ", start=" + start + ", end=" + end + ", points=" + points
            + ", title=" + title + ", description=" + description
            + ", cat=" + cat + ", type=" + type + ", subtype=" + subtype
            + ", valid=" + valid + "]";
}
public String getEventInfo(String field){
    if("eid".equals(field)){return eid;}
    else if("bid".equals(field)){return bid;}
    else if("bname".equals(field)){return bname;}
    else if("valid".equals(field)){return valid;}
    return "none";

}
}

public class JSONAdventures {
private String aid;
private String bid;
private String start;
private String valid;
@Override
public String toString() {
    return "Adventures [aid=" + aid + ", bid=" + bid + ", start=" + start
            + ", end=" + end + ", points=" + points + ", title=" + title
            + ", description=" + description + ", cat=" + cat + ", type="
            + type + ", subtype=" + subtype + ", steps_comp=" + steps_comp
            + ", total_steps=" + total_steps + ", valid=" + valid + "]";
}
public String getEventInfo(String field){
    if("aid".equals(field)){return aid;}
    else if("bid".equals(field)){return bid;}
    else if("start".equals(field)){return start;}
    else if("valid".equals(field)){return valid;}
    return "none";

}

}
public class JSONEandA {
private ArrayList<JSONEvents> events;
private ArrayList<JSONAdventures> adventures;
@Override
public String toString() {
    return "ResponseHolder [events=" + events + ", adventures="
                + adventures + "]";
}
public ArrayList<JSONEvents> getJSONEvents() {
    return events;
}
public ArrayList<JSONAdventures> getJSONAdventures() {
    return adventures;
}

}

对于m活动中的代码:

List<JSONEvents> eventlist = new ArrayList<JSONEvents>();
try {
    Gson googleJson = new Gson();
    JSONEandA rh = googleJson.fromJson(example,
                   JSONEandA.class);
    for (JSONEvents e : rh.getJSONEvents()) {
         eventlist.add(e);
         //eventlist.addAll(e);

         // System.out.println(e.toString());
         System.out.println(e.getEventInfo("eid"));
         System.out.println(e.toString());
    }

    final ListView listview = (ListView) findViewById(R.id.eventlistview);
    String[] values = new String[eventlist.size()];
    eventlist.toArray(values);

    // String[] values = new String[] {e.toString()};
    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
        list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(
    Events.this,
android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent,
    final View view, int position, long id) {
    final String item = (String) parent
        .getItemAtPosition(position);
    list.remove(item);
    adapter.notifyDataSetChanged();
    Toast.makeText(getApplicationContext(),
    "Item Removed", Toast.LENGTH_LONG).show();
 });

我还尝试了各种教程中的适配器等,但无法弄清楚如何将它们组合在一起:

class EventAdapter extends ArrayAdapter<JSONEvents> {

private ArrayList<JSONEvents> items;

public EventAdapter(Context context, int textViewResourceId,
        ArrayList<JSONEvents> items) {
    super(context, textViewResourceId, items);
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_events, null);
    }
    JSONEvents q = items.get(position);
    if (q != null) {
        TextView nameText = (TextView) v.findViewById(R.id.firstline);
        TextView priceText = (TextView) v.findViewById(R.id.secondLine);

        if (nameText != null) {
            nameText.setText(q.getEventInfo("eid"));
        }
        if (priceText != null) {
            priceText.setText(q.getEventInfo("bid"));
        }
    }
    return v;
}
}

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

您是否将数据放入适配器?

adapter.addAll(String[] data);

或者您可以在创建适配器时执行此操作:

EventAdapter adapter = new EventAdapter(this, R.id.some_id, eventlist);