[英]What's the best way to code with list from other layout?
我想一些數據添加到ListView
哪一個是不放在我目前的布局,所以我不能用這個ListView
,我在我的充氣目前與其中的布局布局ListView
放置,但我沒有如何使用ListView
進行編碼的任何想法(我無法設置適配器,對此ListView
不能執行任何操作,顯示NullPointerException
...)。
如何使用當前Layout
未放置的ListView
? 提前致謝!
更新
布局A.XML
<?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>
維護性
@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.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llListview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ChaptersActivity">
<ListView
android:focusableInTouchMode="false"
android:drawSelectorOnTop="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/listViewId"
android:fastScrollEnabled="true"/>
</LinearLayout>
我只想將布局A的視圖與布局B結合起來
您可以嘗試以下方法:
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);
最后 ,我已經執行了以下幾步以獲得我喜歡的解決方案:
解決了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.