我想为我的学校制作一个Android应用程序。它应显示教师和菜单的缺席,但菜单不显示任何内容。它没有给出例外或我可以使用的任何东西。我从带有JSON数据的URL获取数据。不幸的是,由于个人原因,我不能给你这个网址。
不幸的是它看起来像这样。
我已经研究了许多试图自己解决问题的网页,但这并不是很有帮助。我正在使用两个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>
答案 0 :(得分:0)
在abse_row.xml
中,您的顶级代码是<LinearLayout>
,指定android:layout_height="match_parent"
。这会导致RecyclerView
中的每个项目都高出一个屏幕;如果向下滚动,你应该可以看到更多的卡片。
将attr更改为android:layout_height="wrap_content"
,而且一切都应该是好的。