2016-04-11 10 views
0

Ich versuche, einige Daten zu einem ListView hinzuzufügen, die ein nicht in meiner Zeit Layout platziert wird, so kann ich diese ListView nicht verwenden, ich bin mein derzeit Layout mit dem Layout aufbläst, wo die ListView platziert ist, aber ich habe keine Ahnung, wie man mit ListView programmiert (ich kann einen Adapter nicht einstellen, ich kann nichts damit tun ListView, zeigt NullPointerException ...).Was ist der beste Weg, mit der Liste aus anderem Layout zu codieren?

Wie kann ich eine ListView verwenden, die nicht auf meiner derzeit Layout platziert wurde? Danke im Voraus!

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.XM L

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

GERADE WAS ICH WILL ist die Ansicht von LAYOUT aufzublasen A MIT DEM PLAN B

+0

Können Sie Codes von dem, was Sie versuchen zu arbeiten? – andre3wap

+0

Schauen Sie, was ich getan habe, wenn ich versuche, Ansichten aufzublasen. [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) – MAOL

+0

Ja @MAOL es ist die gleiche Frage ... schau, was ich seit ein paar Wochen gefragt habe ...http://stackoverflow.com/questions/36184778/could-be-possible-make-parallax-effect-without-toolbar-on-android danke trotzdem! –

Antwort

0

Schließlich, ich habe das schon einige Schritte getan meine bevorzugte Lösung zu erhalten:

  1. CardView muss aus sein, denn es ist ein FrameLayout ist und ich kann es nicht zwischen Listview in der Mitte gesetzt und Image
  2. Implementierung ObservableScrollView von this place
  3. ich möchte der Toolbar, um loszuwerden, also tat ich es, weil KÖNNEN sIE alle Code-Symbolleiste entfernen (von XML & Java) und I t funktioniert wie immer!
  4. Alles funktioniert perfekt, nur einen anderen Weg zu suchen, um einen Text auf meinem Plan A

GELÖST zu implementieren!

1

du versuchen:

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

EDIT: Nach Aufblasen Ihr Layout in Eltern, können Sie findViewById verwenden, um die richtige Ansicht

zu erhalten
ListView list = (ListView) parent.findViewById(R.id.listViewId); 
+0

Tut mir leid, es funktioniert nicht, und ich kann die dritte Zeile des Codes entfernen hinzufügen Eltern anstelle von null –

+0

Ok ich werde versuchen, danke trotzdem! –

+0

Es funktioniert immer noch nicht ... danke trotzdem! Ich kann es nicht wie Google Android Entwickler, weil ich nicht appbar, Symbolleiste ... –

Verwandte Themen