0

I'm trying to add some data to a ListView which one is not placed in my currently layout, so I cannot use this ListView, I'm inflating my currently layout with the layout where is the ListView placed, but I don't have any idea of how to code with ListView (I cannot set an adapter, I cannot do anything with this ListView, shows NullPointerException...).

How can I use a ListView that's not been placed on my currently Layout? Thanks in advance!

UPDATE

LAYOUT 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"

LAYOUT 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>

JUST WHAT I WANT IS TO INFLATE THE VIEW OF LAYOUT A WITH THE LAYOUT B

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
  • Can you provide codes of what you are trying to get working ? – yardie Apr 11 '16 at 15:49
  • Look what I've done when I try to inflate views. [http://stackoverflow.com/questions/36423427/dynamic-inflating-gives-me-a-nullexception/36424124#36424124](http://stackoverflow.com/questions/36423427/dynamic-inflating-gives-me-a-nullexception/36424124#36424124) – Víctor Martín Apr 11 '16 at 15:51
  • Yes @MAOL it's the same question... look what I've been asking for a few weeks...http://stackoverflow.com/questions/36184778/could-be-possible-to-make-parallax-effect-without-toolbar-on-android thanks anyway! – M. Mariscal Apr 11 '16 at 15:55
  • Is there a reason you don't include layout b inside layout a since you need it there? – codeMagic Apr 11 '16 at 16:29
  • because I have to inflate because I'm trying to do http://stackoverflow.com/questions/36184778/could-be-possible-to-make-parallax-effect-without-toolbar-on-android that's what I'm trying to do this... – M. Mariscal Apr 11 '16 at 16:31

2 Answers2

1

You can try this:

LayoutInflater inflater = LayoutInflater.from(this);
View inflatedLayout= inflater.inflate(R.layout.LAYOUT B, null, false);
parent.addView(inflatedLayout);

EDIT: After inflating your layout in parent, you can use findViewById to get the correct view

ListView list = (ListView) parent.findViewById(R.id.listViewId);
Groco
  • 1,301
  • 15
  • 19
0

Finally, I've been doing this few steps to get my prefered solution:

  1. CardView must be out, cause It's a FrameLayout and I cannot set it in the middle of between ListView and ImageView
  2. Implementing ObservableScrollView from this place
  3. I want to get rid of Toolbar, so I did it, because YOU CAN remove all toolbar code (from XML & Java) and It will works as ever!
  4. Everything works perfectly, just have to look another way to implement a text on my Layout A

SOLVED!

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46