【问题标题】:viewing 2 or more arrays in a listview in array adapter?在数组适配器的列表视图中查看 2 个或更多数组?
【发布时间】:2013-08-26 17:03:12
【问题描述】:

我对这段代码有疑问:

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

}

使用 lv 作为列表视图,如下所示:

private ListView lv;

现在的问题是,我有这个数组 cdatecevent,我希望将它们列在 arrayadapter 上,但 arrayadapter 只接受一个 arraylist。

我还阅读了许多其他相关问题,但我最终得到的只是相同的答案,“你必须制作一个自定义适配器”。但是使用此代码,是否可以使用自定义适配器?代码在:

public class calendarview extends Activity {

编辑: 我还是卡住了,我像 trevor-e 说的那样创建了一个类,这是代码:

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
}

ArrayAdapter 改为:

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

现在代码没有给出任何值。我错过了什么?感觉就像我必须将数组设置到此类中,但我不知道如何。

【问题讨论】:

    标签: android arrays listview android-listview


    【解决方案1】:

    【讨论】:

    • 嗨,我检查了您提供的链接,这对于了解如何使用它的用户似乎很有用,我无法理解代码从 ItemAdapter 部分的“扩展 ArrayAdapter”开始的部分。你能解释一下我应该从我的问题中给出的代码做什么吗?
    【解决方案2】:

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

    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&lt;Event&gt;

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

    【讨论】:

    • 如果你不想继承 ArrayAdapter,你还应该在这个类中重写 toString() 并将日期和事件连接起来。 ArrayAdapter 默认只使用 toString() 来获取它将在 ListView 中使用的值。
    • @TenFour04 谢谢,很好。我现在在帖子中提到了这一点。
    • 感谢您的回复,但我似乎缺乏完全理解您的答案的知识。你能详细说明一下吗?感谢您的理解
    • @AthanSubion 现在你有一个 String 类型的 ArrayAdapter。如果您创建一个新对象来存储要显示的两个字符串条目,那么您可以改用该类型的 ArrayAdapter,这样您就可以同时通过这两个条目。如果你这样做了,那么你可能必须重写 ArrayAdapter 中的一些方法来处理它。
    • @AthanSubion 然后您是否从您拥有的 cevent 和 cdate 数组中创建了一个 Event 对象列表?你应该把它传递给你的适配器。为了显示正确的文本,您可能需要覆盖 Event 中的 toString() 方法。
    【解决方案3】:

    由于我无法理解这个 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多