0

im having a problem with this code:

public void calendarview()
{
    lv = (ListView) findViewById(R.id.calendarlist);

    SharedPreferences pref = getApplicationContext().getSharedPreferences(
            "animobile", MODE_PRIVATE);

    int size = pref.getInt("clength", 0);
    String cdate[] = new String[size];
    String cevent[] = new String[size];
    for(int i=0;i<size;i++){
        cdate[i] = pref.getString("cdate" + i, null);
        cevent[i] = pref.getString("cdate" + i, null);
    }

    Log.w("athan","kalendaryo"+size);

    ArrayAdapter<String> arrayAdapter =      
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, cdate);
    lv.setAdapter(arrayAdapter); 

}

with lv as listview like this:

private ListView lv;

now the problem is, i have this array cdate and cevent and i want them to be listed on an arrayadapter, but arrayadapter accepts only one arraylist.

i have also read lots of other related questions, but i only end up with same answer, "you have to make a custom adapter". but with this code, is it possible with custom adapter? the code is inside the:

public class calendarview extends Activity {

EDIT: im still stuck, i created a class just like trevor-e said, this is the code:

class Event {
    private String cdate;
    private String cevent;
    public Event(String date, String event) {
        this.cdate = date; this.cevent = event;
    }

    public void setcdate(String cdate) {
        this.cdate = cdate;
    }
    public void setcevent(String cevent) {
        this.cevent = cevent;
    }
    public String cevent() {
        return cevent;
    }
    public String cdate() {
        return cdate;
    }
    //getters and setters
}

and the ArrayAdapter is changed to:

ArrayAdapter<Event> arrayAdapter =      
    new ArrayAdapter<Event>(this,android.R.layout.simple_list_item_1);

now the code does not give any values. im a missing something? it feels like i have to set the array into this class but i dont know how.

4

3 回答 3

0

因为我无法理解这个 arrayadapter,所以我使用了 table:

    TableLayout tl = (TableLayout) findViewById(R.id.calendarlist);
    SharedPreferences pref = getApplicationContext().getSharedPreferences(
            "animobile", MODE_PRIVATE);
    int size = pref.getInt("clength", 0);
    for(int i=0;i<size;i++){
        TableRow tr = new TableRow(this);
        TextView date = new TextView(this);
        date.setText(pref.getString("cdate" + i, null) + "    ");
        tr.addView(date);

        TextView event = new TextView(this);
        event.setText(pref.getString("cevent" + i, null));
        tr.addView(event);
        tl.addView(tr);
    }
于 2013-09-04T06:33:07.623 回答
0

您应该创建一个对象来封装每种类型的一个条目。像这样的东西:

class Event {
    private String date;
    private String event;
    public Event(String date, String event) {
        this.date = date; this.event = event;
    }
    //getters and setters
}

然后构造这些对象的 ArrayList 以用于您的适配器。所以在这种情况下它会是ArrayAdapter<Event>

编辑:正如 TenFour04 在评论中所说,覆盖事件的 toString() 方法也是明智的,因为默认情况下,适配器将在每个条目上调用 toString() 以获取要显示的表示。或者您可以选择对适配器进行子类化以获得更好的控制。

于 2013-08-26T17:18:55.150 回答
0

阅读这篇文章会对你有所帮助 http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

于 2013-08-26T17:14:25.597 回答