【发布时间】:2016-04-20 16:08:20
【问题描述】:
我正在尝试向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>
MAINACTIVITY.JAVA
@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>
我只是想用 布局 B
扩大 布局 A 的视图【问题讨论】:
-
你能提供你想要工作的代码吗?
-
是的@MAOL 这是同一个问题...看看我已经问了几个星期...stackoverflow.com/questions/36184778/…无论如何谢谢!
-
有什么理由不将布局 b 包含在布局 a 中,因为您需要它?
-
因为我必须充气,因为我正在尝试做stackoverflow.com/questions/36184778/… 这就是我想要做的......
标签: android android-layout listview