自定义ArrayAdapter失败。无论我尝试什么,都要求TextView的资源ID

时间:2016-10-25 15:13:46

标签: android xml android-arrayadapter

我正在研究这个问题,因为6个小时而且无法修复它,任何人都会看到错误?

我的MainActivity Code的重要部分:

private void showAllListEntries() {

    ArrayAdapter<Schulden> adapter = new schuldenArrayAdapter(this, 0, schuldenList);

    ListView schuldenListView = (ListView) findViewById(R.id.schuldenListView);
    schuldenListView.setAdapter(adapter);
}

我的自定义阵列适配器:

public class schuldenArrayAdapter extends ArrayAdapter<Schulden> {

    Context context;
    private ArrayList<Schulden> schuldenList;

    public schuldenArrayAdapter(Context context, int resource, ArrayList<Schulden> schuldens) {

        super(context, resource, schuldens);

        this.context = context;
        this.schuldenList = schuldens;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Schulden schulden = schuldenList.get(position);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(MainActivity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.listlayout, null);

        TextView betragView = (TextView) view.findViewById(R.id.betragtextview);
        TextView nameView = (TextView) view.findViewById(R.id.nametextview);
        TextView datumView = (TextView) view.findViewById(R.id.datumtextview);

        String completeBetrag = schulden.getBetrag() + "€";
        betragView.setText(completeBetrag);

        nameView.setText(schulden.getName());

        SimpleDateFormat datenbankdatumformat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat ausgabedatumformat = new SimpleDateFormat("dd.MM.yyyy");

        Date datumdatenbankformat = datenbankdatumformat.parse(schulden.getDatum(), null);
        String datumausgabedatumformat = ausgabedatumformat.format(datumdatenbankformat);

        datumView.setText(datumausgabedatumformat);

        return view;
    }
}

在我的listlayout.xml中,我只使用了三个TextViews,我正在使用数组适配器。

运行Feed:

D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 84 - 0, 0) vi=Rect(0, 84 - 0,  0) or=1
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@f600c06 time:209490510
E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.benedikt.schuldenliste, PID: 23044
              java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393)
                  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
                  at android.widget.AbsListView.obtainView(AbsListView.java:3073)
                  at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
                  at android.widget.ListView.onMeasure(ListView.java:1212)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                  at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3140)
                  at android.view.View.measure(View.java:20236)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2704)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1656)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1948)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1544)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7601)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                  at android.view.Choreographer.doFrame(Choreographer.java:622)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7331)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
                  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 
                  at android.widget.AbsListView.obtainView(AbsListView.java:3073) 
                  at android.widget.ListView.measureHeightOfChildren(ListView.java:1305) 
                  at android.widget.ListView.onMeasure(ListView.java:1212) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333) 
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333) 
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                  at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333) 
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:629) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333) 
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333) 
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:747) 
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:629) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333) 
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                  at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3140) 
                  at android.view.View.measure(View.java:20236) 
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2704) 
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1656) 
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1948) 
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1544) 
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7601) 
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) 
                  at android.view.Choreographer.doCallbacks(Choreographer.java:686) 
                  at android.view.Choreographer.doFrame(Choreographer.java:622) 
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:7331) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我对解决方案非常满意:D

1 个答案:

答案 0 :(得分:0)

尝试更换适配器以扩展BaseAdapter而不是ArrayAdapter。你需要实现一些更基本的功能,但是你有更多的控制权,不会遇到你遇到的问题。

更新:在额外查看代码和堆栈跟踪之后,我非常确定不是这个适配器抛出异常。你在使用其他适配器吗? 这一行意味着你的适配器没有被使用(因为它不是你正在调用的getView()): android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)