我正在尝试将一些数据添加到ListView
当前布局中未放置 ,因此我无法使用此ListView
,我是< strong>使用放置ListView
的布局来扩展我当前的布局,但我不知道如何使用ListView
进行编码(我无法设置适配器,我不能对此ListView
执行任何操作,显示NullPointerException
...)。
如何使用尚未放置在我当前ListView
的{{1}}?提前谢谢!
更新
布局A.XML
Layout
MAINACTIVITY.JAVA
<?xml version="1.0" encoding="utf-8"?>
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:layout_height="match_parent"
android:id="@+id/observable_scrollview"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
card_view:cardBackgroundColor="@color/cardview_shadow_start_color"
android:layout_width="match_parent"
android:layout_height="100dp"
android:fitsSystemWindows="true"
android:isScrollContainer="true"
card_view:cardCornerRadius="@dimen/cardview_default_radius"
card_view:cardElevation="@dimen/cardview_default_elevation"
card_view:cardUseCompatPadding="true">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="12dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="12dp"
android:gravity="center"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<com.bluejamesbond.text.DocumentView
card_view:documentView_textColor="@color/circle_color"
android:id="@+id/dv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v7.widget.CardView>
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="600dp"
android:background="@android:color/black" />
</LinearLayout>
布局B.XML
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.LAYOUT A); //It's not the real name of layout, it's for this question...
ListView list = (ListView) findViewById(R.id.listViewId);
cv = (CardView) findViewById(R.id.cv);
//TextView dv = (TextView) findViewById(R.id.dv);
DocumentView dv = (DocumentView) findViewById(R.id.dv);
image = (ImageView) findViewById(R.id.image);
ObservableScrollView osv = (ObservableScrollView) findViewById(R.id.observable_scrollview);
osv.setScrollViewCallbacks(this);
View v = findViewById(R.id.view);
ViewGroup parent = (ViewGroup) v.getParent();
v = getLayoutInflater().inflate(R.layout.listview, parent, false);
int index = parent.indexOfChild(v);
parent.addView(v, index);
/*ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
View C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);*/
b = getIntent().getExtras();
ArrayList<String> arrayList = new ArrayList<>();
for (int x = 0; x < Util.getData().size(); x++) {
if (b.get(Util.getData().get(x).getNombre()) != null) {
for (int z = 0; z < Util.GETDATA().get(x).getsomething().size(); z++) {
arrayList.add(Util.getData().get(x).getsomething().get(z).getname());
}
}
}
ArrayAdapter adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(adapter); //IT CRASHES HERE!
....
//I CANNOT USE THIS LIST BECAUSE IT'S ON LAYOUT "B"
我想要通过布局B 充实布局A 的观点
答案 0 :(得分:1)
你可以试试这个:
LayoutInflater inflater = LayoutInflater.from(this);
View inflatedLayout= inflater.inflate(R.layout.LAYOUT B, null, false);
parent.addView(inflatedLayout);
编辑:
在父级中展开布局后,您可以使用findViewById
来获取正确的视图
ListView list = (ListView) parent.findViewById(R.id.listViewId);
答案 1 :(得分:0)
最后,我一直在做这几个步骤来获得我的首选解决方案:
<强>解决!强>