Answer the question
In order to leave comments, you need to log in
How to replace recyclerview with button?
I am new to programming, please help to replace recyclerview with Button!
There is a list of Levels, I need to make a button that would launch 1 level (Level 1), instead of recyclerview!
CategoryActivity
public class CategoryActivity extends ActionBarActivity {
private Toolbar toolbar;
private RecyclerView recyclerView;
private List<CategoryModel> categoryList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Choose Category");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
}
});
initCategory();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
CategoryAdapter adapter = new CategoryAdapter(categoryList);
recyclerView.setAdapter(adapter);
}
public void initCategory() {
categoryList.add(new CategoryModel("Level 1"));
categoryList.add(new CategoryModel("Level 2"));
categoryList.add(new CategoryModel("Level 3"));
categoryList.add(new CategoryModel("Level 4"));
categoryList.add(new CategoryModel("Level 5"));
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
}
@Override
protected void onPause() {
super.onPause();
}
}
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private List<CategoryModel> items;
private DatabaseHandler databaseHandler;
public CategoryAdapter(List<CategoryModel> items) {
this.items = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_category, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.cat_name.setText(items.get(i).getName());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class ViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView cat_name;
ViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
cat_name = (TextView) itemView.findViewById(R.id.cat_name);
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(itemView.getContext(), BeginTestActivity.class);
Bundle b = new Bundle();
databaseHandler = new DatabaseHandler(itemView.getContext());
b.putString("category", cat_name.getText().toString());
if (databaseHandler.getScoreForCategory(cat_name.getText().toString()) != null) {
b.putString("best_score", databaseHandler.getScoreForCategory(cat_name.getText().toString()).getScore());
} else {
b.putString("best_score", "0");
}
intent.putExtras(b);
itemView.getContext().startActivity(intent);
}
});
}
}
}
Answer the question
In order to leave comments, you need to log in
Here is the code that "starts the level":
Intent intent = new Intent(itemView.getContext(), BeginTestActivity.class);
Bundle b = new Bundle();
databaseHandler = new DatabaseHandler(itemView.getContext());
b.putString("category", cat_name.getText().toString());
if (databaseHandler.getScoreForCategory(cat_name.getText().toString()) != null) {
b.putString("best_score", databaseHandler.getScoreForCategory(cat_name.getText().toString()).getScore());
} else {
b.putString("best_score", "0");
}
intent.putExtras(b);
itemView.getContext().startActivity(intent);"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question