Ich versuche, eine App mit einer Liste von Lebensmitteln auf der Haupttätigkeit und einem Kontrollkästchen für jedes Essen zu erstellen.Warum funktioniert dieses RecyclerView + CardView-Beispiel nicht?
folgte ich dieses Tutorial, um den gesamten Text und Kontrollkästchen, was zu tun: http://android-pratap.blogspot.co.il/2015/01/recyclerview-with-checkbox-example.html
Aber immer, wenn ich die app laufen sie einfach nicht die RecyclerView auf dem Hauptbildschirm zeigen ... (Die App läuft , aber der RecyclerView ist einfach nicht da)
Wenn jemand erklären kann, wie ich meinen Code ändern kann, so würde es nicht die Karten erfordern, die fantastisch wären. Hier ist mein Code:
MainAcitivity.java: (Just kombiniert die RecyclerView mit dem FoodAdapter)
package com.gregskl.foodreminder;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recycler;
private RecyclerView.Adapter adapter;
private List<Food> foods;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
foods = new ArrayList<>();
foods.add(new Food("Grapes", true));
foods.add(new Food("Oranges", true));
recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));
adapter = new FoodAdapater(foods);
recycler.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
FoodAdapter.java:
package com.gregskl.foodreminder;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
/**
* Created by Gregory on 11-Sep-16.
*/
public class FoodAdapater extends RecyclerView.Adapter<FoodAdapater.ViewHolder> {
private List<Food> foods;
public FoodAdapater(List<Food> foods) {
this.foods = foods;
}
@Override
public FoodAdapater.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, null);
return new ViewHolder(itemLayoutView);
}
@Override
public void onBindViewHolder(FoodAdapater.ViewHolder holder, int position) {
ViewHolder h = (ViewHolder) holder;
h.text.setText(foods.get(position).getText());
h.checkbox.setChecked(foods.get(position).isAvailable());
h.checkbox.setTag(foods.get(position));
h.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(), "Hey", Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return foods.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public CheckBox checkbox;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
}
}
}
Food.java: (Das Datenmodell für das Essen)
package com.gregskl.foodreminder;
/**
* Created by Gregory on 11-Sep-16.
*/
public class Food {
private String text;
private boolean available;
public Food(String text, boolean available) {
this.text = text;
this.available = available;
}
public String getText() {
return text;
}
public boolean isAvailable() {
return available;
}
public void setText(String text) {
this.text = text;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.gregskl.foodreminder.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:scrollbars="vertical" />
</RelativeLayout>
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Wow ... Ich kann nicht glauben, dass ich das nicht bemerkt habe ... Wie wäre es aber ohne CardView? Kann ich CardView nur aus list.xml entfernen und RelativeLayout in ein horizontales LinearLayout ändern? –
Ja, Sie können 'CardView' aus' list.xml' entfernen. Ist nur ein Widget. Du kannst tun was du willst, so lernst du. Prost! –