私のXMLファイルに既に入っているListViewに新しいTextViewを追加したいのですが、 それらを追加する直前に、静的なString []でsetText()を使用します。 私はソリューションの多くを試してみたが、私は時々のTextView IDに関する例外、およびその他の時間を持って、この1:新しいTextViewをListViewに追加する
04-20 14:51:57.139 8034-8034/com.example.irit.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.irit.myapp, PID: 8034
java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:700)
at com.example.irit.myapp.Model.ListObjet.creatView(ListObjet.java:65)
at com.example.irit.myapp.Model.ListObjet.access$100(ListObjet.java:31)
at com.example.irit.myapp.Model.ListObjet$1.onClick(ListObjet.java:50)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
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)
私のクラス:
:私は自分のコードを変更し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)
ではなく、リストビュー、それを自己の、リストビューのアダプタに項目を追加します。 – LearnPainLess
アイテムは文字列ではなく、文字列を追加するとjava.lang.UnsupportedOperationExceptionが返されます。 –
String型ArrayAdapterを使用しています。カスタムアイテムを追加できません。カスタムアダプタを作成します。 – LearnPainLess