博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓setText导入什么Java包,java – 如何在recyclerview上添加SetOnClicklistener
阅读量:5109 次
发布时间:2019-06-13

本文共 6511 字,大约阅读时间需要 21 分钟。

这是我的适配器..

package com.lim.fiture.fiture.adapters;

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.EditText;

import com.lim.fiture.fiture.R;

import com.lim.fiture.fiture.models.Exercise;

import java.util.ArrayList;

public class AdminActivityAdapter extends RecyclerView.Adapter {

Context context;

public ArrayList exercises;

private OnItemClicked onClick;

//make interface like this

public interface OnItemClicked {

void onItemClick(int position);

}

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_admin_adapter,parent,false);

AdminActivityAdapter.ViewHolder viewHolder = new AdminActivityAdapter.ViewHolder(view);

return viewHolder;

}

@Override

public void onBindViewHolder(ViewHolder holder, int position) {

holder.exerciseName.setText(exercises.get(position).getExerciseName());

holder.mainMuscleGroup.setText(exercises.get(position).getMainMuscleGroup());

holder.otherMuscleGroup.setText(exercises.get(position).getOtherMuscleGroup());

holder.type.setText(exercises.get(position).getType());

holder.equipment.setText(exercises.get(position).getEquipment());

holder.difficulty.setText(exercises.get(position).getDifficulty());

holder.deleteExercise.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

onClick.onItemClick(position);

}

});

}

@Override

public int getItemCount() {

return exercises.size();

}

public class ViewHolder extends RecyclerView.ViewHolder {

private EditText exerciseName, mainMuscleGroup, otherMuscleGroup, type, equipment, difficulty;

private ImageButton deleteExercise;

public ViewHolder(View itemView) {

super(itemView);

exerciseName = itemView.findViewById(R.id.exerciseName);

mainMuscleGroup = itemView.findViewById(R.id.mainMuscleGroup);

otherMuscleGroup = itemView.findViewById(R.id.otherMuscleGroup);

type = itemView.findViewById(R.id.type);

equipment = itemView.findViewById(R.id.equipment);

difficulty = itemView.findViewById(R.id.difficulty);

deleteExercise = itemView.findViewById(R.id.deleteBtn);

}

}

}

这是我的活动

package com.lim.fiture.fiture.activities;

import android.content.ClipData;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.ActionBar;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.helper.ItemTouchHelper;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.support.v7.app.AppCompatActivity;

import android.widget.ImageButton;

import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;

import com.google.android.gms.tasks.OnSuccessListener;

import com.google.firebase.auth.AuthResult;

import com.google.firebase.auth.FirebaseAuth;

import com.google.firebase.auth.FirebaseUser;

import com.google.firebase.database.ChildEventListener;

import com.google.firebase.database.DataSnapshot;

import com.google.firebase.database.DatabaseError;

import com.google.firebase.database.DatabaseReference;

import com.google.firebase.database.FirebaseDatabase;

import com.lim.fiture.fiture.R;

import com.lim.fiture.fiture.adapters.AdminActivityAdapter;

import com.lim.fiture.fiture.adapters.ExercisesAdapter;

import com.lim.fiture.fiture.models.Exercise;

import java.util.ArrayList;

public class AdminActivity extends AppCompatActivity implements AdminActivityAdapter.OnItemClicked {

private RecyclerView exercisesList;

private AdminActivityAdapter adapter;

private RecyclerView.LayoutManager layoutManager;

private FloatingActionButton addExerciseBtn;

private ExercisesAdapter exercisesAdapter;

private ArrayList exercises = new ArrayList<>();

//TODO: instance variables for firebase database

private DatabaseReference exerciseReference;

private static final String TAG = "AdminActivity";

private FirebaseAuth mAuth;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_admin);

mAuth = FirebaseAuth.getInstance();

exerciseReference = FirebaseDatabase.getInstance().getReference("Exercises");

findViews();

loadExercises();

}

@Override

protected void onStart() {

super.onStart();

FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {

// do your stuff

} else {

signInAnonymously();

}

}

private void signInAnonymously() {

mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener() {

@Override

public void onSuccess(AuthResult authResult) {

// do your stuff

}

})

.addOnFailureListener(this, new OnFailureListener() {

@Override

public void onFailure(@NonNull Exception exception) {

Log.e(TAG, "signInAnonymously:FAILURE", exception);

}

});

}

@Override

public void onClick(View view) {

switch (view.getId()) {

case R.id.addExerciseBtn:

startActivity(new Intent(this, AddExerciseStepOne.class));

break;

}

}

public void findViews() {

addExerciseBtn = findViewById(R.id.addExerciseBtn);

addExerciseBtn.setOnClickListener(this);

exercisesList = findViewById(R.id.exercisesList);

layoutManager = new LinearLayoutManager(this);

exercisesList.setLayoutManager(layoutManager);

ActionBar bar = getSupportActionBar();

bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2980b9")));

bar.setTitle("Fiture Admin");

}

public void loadExercises() {

exerciseReference.addChildEventListener(new ChildEventListener() {

@Override

public void onChildAdded(DataSnapshot dataSnapshot, String s) {

Exercise exercise = dataSnapshot.getValue(Exercise.class);

Log.d(TAG, exercise.toString());

exercises.add(exercise);

exercisesAdapter = new ExercisesAdapter(AdminActivity.this, exercises);

exercisesList.setAdapter(exercisesAdapter);

}

@Override

public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override

public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override

public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});

}

@Override

public void onItemClick(int position) {

}

}

所以我想要做的是当点击删除按钮时它会在回收器视图中删除所选的(包含有关练习的详细信息的cardview)并在firebase中删除它,但我很难在互联网上寻找教程.谁知道怎么做?非常需要你的帮助.谢谢

转载地址:http://whjdv.baihongyu.com/

你可能感兴趣的文章
.26-浅析webpack源码之事件流make(1)
查看>>
IO流
查看>>
mybatis调用存储过程,获取返回的游标
查看>>
设计模式之装饰模式(结构型)
查看>>
面向对象的设计原则
查看>>
Swift3.0服务端开发(三) Mustache页面模板与日志记录
查看>>
【转】 FPGA设计的四种常用思想与技巧
查看>>
EntityFrameWork 实现实体类和DBContext分离在不同类库
查看>>
新手算法学习之路----二叉树(在一个二叉查找树中插入一个节点)
查看>>
autopep8
查看>>
GIT在Linux上的安装和使用简介
查看>>
基于C#编程语言的Mysql常用操作
查看>>
s3c2440实验---定时器
查看>>
MyEclipse10安装SVN插件
查看>>
[转]: 视图和表的区别和联系
查看>>
Regular Experssion
查看>>
图论例题1——NOIP2015信息传递
查看>>
uCOS-II中的任务切换-图解多种任务调度时机与问题
查看>>
CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
查看>>
Android 官方新手指导教程
查看>>