2016-06-30 14 views
0

Ich bin nur einige Daten analysieren und auf meiner ListProperty Klasse anzeigen. Ich verwende RecyclerView und CardView als Zeilenlayout. Irgendwie funktioniert alles gut, ich sehe keinen Fehler im worse. Aber das Daten- und Zeilenlayout wird in meiner Hauptaktivität angezeigt. Aktivität wird leer angezeigt. Ich versuche von Stunden aber nett den Fehler zu finden. Bitte jemand helfenZeilenlayout nicht auf Hauptaktivität Recycler Ansicht eingestellt

Mein MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    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"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/cardList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</RelativeLayout> 

Dies ist row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <ImageView 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:id="@+id/imageViewFront" /> 
      </LinearLayout> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="100dp" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:text="New Text" 
      android:textColor="#0055CC" 
      android:id="@+id/textViewPropertyname" /> 

     <TextView 
      android:layout_marginTop="10dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:textColor="#0055CC" 
      android:layout_columnWeight="1" 
      android:id="@+id/textViewPropertyDescription"/> 
    </LinearLayout> 

    </android.support.v7.widget.CardView> 


</LinearLayout> 

Dies ist myAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { 
List<Bean> DataList; 
public MyAdapter(List<Bean> List){ 
    super(); 
    DataList = List; 
} 
public class MyViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public TextView propertyName; 
    public TextView propertyDesc; 
    public ImageView ImageFront; 
    public MyViewHolder(View v) { 
     super(v); 
     propertyName = (TextView) v.findViewById(R.id.textViewPropertyname); 
     propertyDesc = (TextView) v.findViewById(R.id.textViewPropertyDescription); 
     ImageFront = (ImageView) v.findViewById(R.id.imageViewFront); 
    } 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View itemView = LayoutInflater.from(parent.getContext()). 
      inflate(R.layout.list_card_view,parent,false); 
    return new MyViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) { 
    Bean bean = DataList.get(position); 
    holder.propertyName.setText(bean.getPropertyName()); 
    holder.propertyDesc.setText(bean.getPropertyDescription()); 
    holder.ImageFront.setImageResource(R.drawable.agriculture); 

} 

@Override 
public int getItemCount() { 
    return DataList.size(); 
} 

} 

Und das ist mein MainActivity.java

public class ListProperty extends AppCompatActivity { 
    private RecyclerView mRecyclerView; 
    private MyAdapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 
    Bean bean; 
    AQuery aQuery; 
    JSONObject jsonObject; 
    List<Bean> list; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_property); 
     aQuery= new AQuery(getApplicationContext()); 
     String URL = "http://192.168.0.107/propertyapp/service/simpleservice.php"; 
     list = new ArrayList<Bean>(); 
     mRecyclerView = (RecyclerView) findViewById(R.id.cardList); 
     mRecyclerView.setHasFixedSize(true); 
     mLayoutManager = new LinearLayoutManager(this); 
     mRecyclerView.setLayoutManager(mLayoutManager); 
     mAdapter = new MyAdapter(list); 
     mRecyclerView.setAdapter(mAdapter); 
     aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){ 
      @Override 
      public void callback(String url, JSONObject object, AjaxStatus status) { 

       try { 
        bean = new Bean(); 
        list = new ArrayList<Bean>(); 
        String string = object.getString("data"); 
        JSONArray jsonArray = new JSONArray(string); 
        for (int i=0;i<jsonArray.length();i++) { 
         jsonObject = jsonArray.getJSONObject(i); 
         bean.setPropertyName("property_name"); 
         bean.setPropertyDescription("property_desc"); 
         list.add(bean); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 


      } 
     }); 
    } 
} 
+0

haben Sie versucht, einen Debugger setzen und prüfen, ob die 'dataList' nicht null ist? – Sanoop

+0

Call ** mAdapter.notifyDataSetChanged(); ** kurz vor der catch-Klausel .. Dies wird behoben – Sanoop

Antwort

1

Versuchen Sie, das übergeordnete LinearLayout zu entfernen. Weil Sie CardView haben, das als Listenelement fungiert.

dieses row.xml Versuchen -

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <ImageView 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:id="@+id/imageViewFront" /> 
       </LinearLayout> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_marginLeft="100dp" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="New Text" 
       android:textColor="#0055CC" 
       android:id="@+id/textViewPropertyname" /> 

      <TextView 
       android:layout_marginTop="10dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Text" 
       android:textColor="#0055CC" 
       android:layout_columnWeight="1" 
       android:id="@+id/textViewPropertyDescription"/> 
     </LinearLayout> 

</android.support.v7.widget.CardView> 

Hoffe, dass es wie unten :)

1

Fügen Sie einfach notifyDataSetChanged() zu Ihrem Activity helfen:

aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){ 
     @Override 
     public void callback(String url, JSONObject object, AjaxStatus status) { 

      try { 
       bean = new Bean(); 
       list = new ArrayList<Bean>(); 
       String string = object.getString("data"); 
       JSONArray jsonArray = new JSONArray(string); 
       for (int i=0;i<jsonArray.length();i++) { 
        jsonObject = jsonArray.getJSONObject(i); 
        bean.setPropertyName("property_name"); 
        bean.setPropertyDescription("property_desc"); 
        list.add(bean); 
       } 
       //here notify your adapter 
       //is to Notify any registered observers that the data set has changed. 
       mAdapter.notifyDataSetChanged(); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 


      } 
     }); 
1

Da auf Ihrem Rückruf Sie haben neue Instanz der Liste

erstellt
list = new ArrayList<Bean>(); 

In der Tat, die neu erstellte Liste und DataList in Ihrem Adapter zeigen auf 2 Unterschied Objekte => NotifyDataSetChanged hier aufrufen wird nicht funktionieren. Versuchen Sie Methode update auf MyAdapter Schreiben und rufen Sie es von Ihrem Callback-Methode

public void updateData(List<Bean> List){ 
    DataList = List; 
    notifyDataSetChanged(); 
    } 

Auf dem Rückruf:

aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){ 
    @Override 
    public void callback(String url, JSONObject object, AjaxStatus status) { 

     try { 
      bean = new Bean(); 
      list = new ArrayList<Bean>(); 
      String string = object.getString("data"); 
      JSONArray jsonArray = new JSONArray(string); 
      for (int i=0;i<jsonArray.length();i++) { 
       jsonObject = jsonArray.getJSONObject(i); 
       bean.setPropertyName("property_name"); 
       bean.setPropertyDescription("property_desc"); 
       list.add(bean); 
      } 
      // update data 
      mAdapter.updateData(list); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 


     } 
    }); 
Verwandte Themen