我想爲我的學校製作Android應用程序。它應顯示教師和菜單中的缺席,但該菜單不顯示任何內容。它沒有給出例外或我可以使用的任何東西。我使用JSON數據從網址獲取數據。不幸的是,由於個人原因,我無法給你網址。Android RecyclerView不顯示任何東西
不幸的是,它看起來是這樣的。
我已經研究很多網頁試圖解決自己的問題,但它不是非常有益。我使用了兩個RecyclerViews,兩個適配器類和兩個片段。在我實現了http請求和JSON解析器之前,它工作正常。有人可以幫助我嗎?
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragAbsenz fragAbsenz = new FragAbsenz();
android.app.FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.contentLayout, fragAbsenz,
fragAbsenz.getTag()).commit();
setTitle("Lehrerabsenzen");
BottomNavigationView navigation = (BottomNavigationView)
findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
MainActivity.context = getApplicationContext();
}
public static Context getAppContext() {
return MainActivity.context;
}
FragAbsenz.java: (I haven't implemented the http request and the JSON
parser on this one yet, but it only displays the first Item from 20 and
the others are gone.)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_frag_absenz,
container, false);
mRecycler = (RecyclerView) view.findViewById(R.id.rv_absenz);
mContacts = new ArrayList<>();
mContacts.add("Michi \n Zi");
mContacts.add("Michi Zi 2");
mContacts.add("Michi \n Zi 3");
for (int i = 4; i < 20; i++) {
mContacts.add("Michi Zi" + i);
}
mRecycler.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new Abse_MainAdapter(mContacts);
mRecycler.setLayoutManager(mLayoutManager);
mRecycler.setAdapter(mAdapter);
return view;
}
Abse_MainAdapter.java:
class Abse_MainAdapter extends
RecyclerView.Adapter<Abse_MainAdapter.ViewHolder> {
ArrayList<String> mContacts;
public Abse_MainAdapter(ArrayList<String> Contacts) {
mContacts = Contacts;
}
@Override
public Abse_MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.abse_row,
parent, false);
return new Abse_MainAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(Abse_MainAdapter.ViewHolder holder, int
position) {
holder.mFullName.setText(mContacts.get(position));
}
@Override
public int getItemCount() {
return mContacts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView mFullName;
public ViewHolder(View itemView) {
super(itemView);
mFullName = (TextView) itemView.findViewById(R.id.full_name);
}
}
FragMenu.java:
public class FragMenu extends Fragment {
ArrayList<String> mMenu;
RecyclerView mRv;
RecyclerView.LayoutManager mLm;
RecyclerView.Adapter mAd;
StringRequest stringRequest = null;
String result;
public FragMenu() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_frag_menu,
container, false);
mRv = (RecyclerView) view.findViewById(R.id.rv_menu);
RequestQueue queue =
Volley.newRequestQueue(MainActivity.getAppContext());
String url = "Censored because of personal reasons";
stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
result = response.substring(0);
try {
JSONObject JSONobj = new JSONObject(result);
String lastUpdate =
JSONobj.getString("lastUpdate");
result = lastUpdate;
JSONArray WMenu = JSONobj.getJSONArray("data");
mMenu = new ArrayList<>();
for (int i = 0; i < WMenu.length(); i++) {
String Str = "";
JSONObject TMenu = WMenu.getJSONObject(i);
Str = Str + TMenu.getString("time") + "|";
JSONArray Menu =
TMenu.getJSONArray("main");
String str = "";
for (int k = 0; k < Menu.length(); k++) {
str = str + Menu.getString(k) + "\n";
}
Str = Str + str + "|";
str = "";
JSONArray Hit = TMenu.getJSONArray("hit");
for (int k = 0; k < Menu.length(); k++) {
str = str + Hit.getString(k) + " \n";
}
Str = Str + str;
mMenu.add(Str);
}
mRv.setHasFixedSize(true);
mLm = new LinearLayoutManager(getActivity());
mAd = new Menu_MainAdapter(mMenu);
mRv.setLayoutManager(mLm);
mRv.setAdapter(mAd);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//mText.setText("That didn't work!");
}
});
queue.add(stringRequest);
return view;
}
Menu_MainAdapter.java:
class Menu_MainAdapter extends
RecyclerView.Adapter<Menu_MainAdapter.ViewHolder> {
ArrayList<String> mMenu;
public Menu_MainAdapter(ArrayList<String> Menu) {
mMenu = Menu;
}
@Override
public Menu_MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_row,
parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(Menu_MainAdapter.ViewHolder holder, int
position) {
String date = ((mMenu.get(position)).split("|"))[0];
String Menu = ((mMenu.get(position)).split("|"))[1];
String Hit = ((mMenu.get(position)).split("|"))[2];
holder.tDate.setText(date);
holder.tDate.setText(Menu);
holder.tDate.setText(Hit);
}
@Override
public int getItemCount() {
return mMenu.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tDate;
public TextView tMenu;
public TextView tHit;
public ViewHolder(View itemView) {
super(itemView);
tDate = (TextView) itemView.findViewById(R.id.tDate);
tMenu = (TextView) itemView.findViewById(R.id.menu);
tHit = (TextView) itemView.findViewById(R.id.hit);
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="systems.exygen.examp.MainActivity">
<FrameLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>
fragment_frag_absenz.xml:
<LinearLayout
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"
tools:context="systems.exygen.examp.FragAbsenz">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_absenz">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
abse_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"
android:layout_margin="12dp">
<TextView
android:id="@+id/full_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:padding="12dp"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
fragment_frag_menu.xml:
<FrameLayout 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"
tools:context="systems.exygen.examp.FragMenu">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_menu">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
menu_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time"
android:layout_margin="12dp"
android:textSize="24dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tages-Menü"
android:layout_margin="12dp"
android:textSize="24dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tages-Menü"
android:layout_margin="12dp"
android:textSize="16dp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tages-Hit"
android:layout_margin="12dp"
android:textSize="24dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/hit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tages-Hit"
android:layout_margin="12dp"
android:textSize="16dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
請閱讀[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/mcve) –