将新TextView添加到ListView

时间:2017-04-20 12:56:21

标签: android listview android-adapterview

我想将新的TextView添加到我的ListView中,该ListView已经存在于我的XML文件中。 在添加它们之前,我使用静态String [] setText()。 我已经尝试了很多解决方案,但有时候我有一个关于TextView id的例外,有时候这个也是:

public class ListObjet{
    private ImageButton mButton;
    private EnvoiMailActivity mEnvoiMailActivity;
    private LayoutInflater inflater;
    private ListView mList;
    private int ressource;

    public ListObjet(EnvoiMailActivity pEnvoiMailActivity, int resourceId) {
        this.ressource = resourceId;
        this.mEnvoiMailActivity = pEnvoiMailActivity;
        this.inflater = LayoutInflater.from(mEnvoiMailActivity);

        this.mList = (ListView) inflater.inflate(this.ressource, null);
        this.mButton = (ImageButton) mEnvoiMailActivity.findViewById(R.id.listobjet);
        this.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder myList = new AlertDialog.Builder(mEnvoiMailActivity);

                myList.setView(creatView());
                AlertDialog alert = myList.create();
                alert.show();
            }
        });
    }


    private View creatView(){
        ScrollView mScrollView = new ScrollView(mEnvoiMailActivity);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mEnvoiMailActivity, this.ressource, Conf.OBJET_PROPOSE);
        this.mList.setAdapter(adapter);
        for (int i = 0; i < Conf.OBJET_PROPOSE.length; i++ ) {
            TextView item = new TextView(mEnvoiMailActivity);
            item.setText(Conf.OBJET_PROPOSE[i]);
            mList.addView(item);
        }
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //todo
            }
        });
        mScrollView.addView(mList);


        return mScrollView;
    }
}

我的课程:

private View creatView(){
    ScrollView mScrollView = new ScrollView(mEnvoiMailActivity);
    ArrayAdapter<TextView> adapter = new ArrayAdapter<TextView>(mEnvoiMailActivity, this.ressource);
    this.mList.setAdapter(adapter);
    for (int i = 0; i < Conf.OBJET_PROPOSE.length; i++ ) {
        TextView item = new TextView(mEnvoiMailActivity);
        item.setText(Conf.OBJET_PROPOSE[i]);
        adapter.add(item);
    }

我改变了我的代码:

04-20 15:07:51.819 32350-32350/com.example.irit.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.irit.myapp, PID: 32350
    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:2929)
        at android.widget.ListView.onMeasure(ListView.java:1180)
        at android.view.View.measure(View.java:20151)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2104)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.widget.ScrollView.onMeasure(ScrollView.java:510)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        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:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
        at android.view.View.measure(View.java:20151)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1524)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
        at android.view.Choreographer.doCallbacks(Choreographer.java:695)
        at android.view.Choreographer.doFrame(Choreographer.java:631)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7224)
        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.ListView 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:2929) 
        at android.widget.ListView.onMeasure(ListView.java:1180) 
        at android.view.View.measure(View.java:20151) 
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2104) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
        at android.widget.ScrollView.onMeasure(ScrollView.java:510) 
        at android.view.View.measure(View.java:20151) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
        at android.view.View.measure(View.java:20151) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
        at android.view.View.measure(View.java:20151) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 
        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:20151) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
        at android.view.View.measure(View.java:20151) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
        at android.view.View.measure(View.java:20151) 
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328) 
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158) 
        at android.view.View.measure(View.java:20151) 
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594) 
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1524) 
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841) 
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437) 
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397) 
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) 
        at android.view.Choreographer.doCallbacks(Choreographer.java:695) 
        at android.view.Choreographer.doFrame(Choreographer.java:631) 
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        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) 

发生此异常:

getGeneratedKeys

1 个答案:

答案 0 :(得分:0)

不是动态分配TextView,而是传递包含TextView的布局ID。您可以创建仅具有TextView的自定义布局,也可以使用android sdk本身提供的默认模板布局。 例如:

android.R.layout.simple_list_item_1

完整示例:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,list);