2017-09-28 67 views
0

我希望通過使用AdapterRecyclerView從天氣報告中創建一組可滾動的項目。我的問題是,即使ArrayList<>我用來提供數據確實有數據,該視圖只是不顯示任何內容 - 沒有錯誤,沒有崩潰。該應用程序啓動並沒有顯示。RecyclerView不顯示任何項目

ActivityMain.onCreate()

private JSONParser_basic parser; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    parser = new JSONParser_basic(getResources() 
      .getString(R.string.weather_json)); 

    parser.divideToListElements(); 

    NewsRecycleAdapter adapter = new NewsRecycleAdapter(parser.getListElements()); 
    LinearLayoutManager lm = new LinearLayoutManager(this); 
    lm.setOrientation(LinearLayoutManager.VERTICAL); 

    RecyclerView rv = (RecyclerView) findViewById(R.id.recycleView); 
    rv.setLayoutManager(lm); 
    rv.setAdapter(adapter); 

} 

注意,JSONParser_basic是自制的簡單類(練出來的,從一個特定的字符串得到的數據)。我用它從JSON字符串獲取數據並將其加載到我的集合listElements。它的工作,我得到的數據,這不是問題。

News類:

public class News { 

    private String max; 
    private String min; 
    private String weather; 
    private String windForce; 

    public News(String max, String min, String weather, String windForce) { 
     this.max = max; 
     this.min = min; 
     this.weather = weather; 
     this.windForce = windForce; 
    } 

    public String formReport(){ 
     return max + "/" + min + ", " 
       + weather + ", " + 
       "Wind=" + windForce; 
    } 
} 

我上傳這個類來顯示formReport方法。

我的適配器:

public class NewsRecycleAdapter extends RecyclerView.Adapter<NewsRecycleAdapter.NewsViewHolder> { 

    List<News> items; 

    public NewsRecycleAdapter(List<News> items) { 
     this.items = items; 
    } 

    @Override 
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType)  
    { 
     return new NewsViewHolder(View.inflate(parent.getContext(), 
       R.layout.list_element, 
       null)); 
    } 

    @Override 
    public void onBindViewHolder(NewsViewHolder holder, int position) { 
     News myNews = items.get(position); 
     holder.titleTV.setText(myNews.formReport()); 
    } 

    @Override 
    public int getItemCount() { 
     return items == null ? 0 : items.size(); 
    } 

    public class NewsViewHolder extends RecyclerView.ViewHolder { 
     public TextView titleTV, leadTV; 
     public NewsViewHolder(View itemView) { 
      super(itemView); 
      titleTV = (TextView) itemView.findViewById(R.id.title); 
     } 
    } 


}  

list_element.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="7dp"> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:maxLines="1" 

    android:text="some gibberish for now" 
    android:textStyle="bold" /> 

最後我main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.smth.myname.jeeesoooon.MainActivity"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recycleView" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

確實有這條線在我的應用程序的build.gradle

compile 'com.android.support:recyclerview-v7:26.0.+' 

回答

1

爲什麼RecyclerView高度和寬度0dp

將其更改爲以下:

android:layout_width="match_parent" 
android:layout_height="wrap_content" 

此外,在list_element.xml,佈局改變高度wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
+0

嘿,爲什麼我把0dp那裏,這是一個謎。現在它工作得很好,謝謝你的建議。 好吧,如果我自己這麼說,那我就犯了一個不好的錯誤。 – agiro