任何人都可以帮忙解决这个问题吗?
情况是我有一个来自sqlite的数据的recyclerview填充它。 当我点击一个项目时,将启动一个新活动,该活动对于之后的每个项目都是唯一的。
但问题是,当我点击来自recyclerview的项目B时,它将始终启动我不想要的项目A活动,并且对于recyclerview中的每个项目都是相同的。
是他们正确的方法吗?
继承我的Recyclerview项目代码点击
public void onBindViewHolder(reg_ViewHolder holder, final int position) {
holder.pet_name.setText(items.get(position).getPet_name());
holder.pet_type.setText(items.get(position).getPet_type());
holder.pet_breed.setText(items.get(position).getPet_breed());
holder.btndel.setTag(items.get(position).getPet_name());
holder.btnpet_guide.setTag(items.get(position));
holder.btnpet_guide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
Cursor compare=register_pet_database.getBreed();
String name = compare.getString(compare.getColumnIndex("Pet_breed"));
try {
if (compare.getCount()>0 && compare.moveToFirst()) {
do {
if (name.equals("Chow Chow")) {
Intent intent = new Intent(data_context, dog_chow_chow.class);
data_context.startActivity(intent);
break;
}
else if (name.equals("Beagle")) {
Intent intent = new Intent(data_context, dog_beagle.class);
data_context.startActivity(intent);
break;
}
} while (compare.moveToNext());
}
}catch (Exception e){
e.printStackTrace();
}
compare.close();
}
});
我在数据库中获取数据的代码
public Cursor getBreed()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur=db.rawQuery("select Pet_breed from "+TABLE_NAME,null);
return cur;
}
被修改
继承我的新适配器
public class reg_Adapter extends RecyclerView.Adapter<reg_ViewHolder>{
Context data_context;
Register_pet_database register_pet_database;
ArrayList<db_getItem> items;
ItemClicklistiener listener;
db_getItem adapter;
DBAdapter dbAdapter;
public reg_Adapter(Context data_context, ArrayList<db_getItem> items) {
this.data_context= data_context;
this.items = items;
}
public reg_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reg_pet_recycler, parent, false);
return new reg_ViewHolder(view, data_context, items);
}
public void add(db_getItem pet) {
items.add(0, pet);
notifyItemInserted(0);
}
@Override
public void onBindViewHolder(reg_ViewHolder holder, final int position) {
holder.pet_name.setText(items.get(position).getPet_name());
holder.pet_type.setText(items.get(position).getPet_type());
holder.pet_breed.setText(items.get(position).getPet_breed());
holder.btndel.setTag(items.get(position).getPet_name());
holder.btnpet_guide.setTag(items.get(position));
holder.btnpet_guide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
items=new ArrayList<>();
Cursor compare=register_pet_database.getBreed();
try {
if (compare.getCount() > 0 && compare.moveToFirst()) {
do {
String name = compare.getString(compare.getColumnIndex("Pet_breed"));
String chow="Chow Chow";
String beagle="Beagle";
if (name.equals(chow)) {
listener.onItemClicked(position, dog_chow_chow.class);
break;
}
else if (name.equals(beagle)) {
listener.onItemClicked(position, dog_beagle.class);
break;
}
} while (compare.moveToNext());
}
}catch (Exception e){
e.printStackTrace();
}
compare.close();
}
});
holder.btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
Integer deleted =register_pet_database.deleteData(v.getTag().toString());
if (deleted > 0) {
items.remove(position);
notifyItemRemoved(position);
Toast.makeText(v.getContext(), "Pet Has been Remove", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(v.getContext(), "Pet not Removed", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return items.size();
}
}
以下是我的MyFragment代码
public class pet_tab extends Fragment{
RecyclerView recyclerView;
Register_pet_database register_pet_database;
ArrayList<db_getItem> arrayList;
reg_Adapter reg_adapter;
db_getItem item;
Button btndel, btnpet_guide;
TextView pet_type, pet_breed;
Cursor c;
public pet_tab() {
// 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_pet_tab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.reg_pet_recycler);
loadDb();
return view;
}
public void loadDb() {
register_pet_database = new Register_pet_database(getActivity());
DBAdapter db = new DBAdapter(getActivity());
db.openDB();
arrayList = new ArrayList<>();
c = register_pet_database.queryData("select * from Pet_Registered");
final int position = 0;
try {
if (c != null) {
if (c.moveToFirst()) {
do {
db_getItem item = new db_getItem();
item.setPet_name(c.getString(1));
item.setPet_type(c.getString(2));
item.setPet_breed(c.getString(3));
arrayList.add(item);
} while (c.moveToNext());
}
}
} catch (SQLException e) {
e.printStackTrace();
}
reg_Adapter adapter = new reg_Adapter(getActivity(), arrayList);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
}
ItemClicklistiener listener=new ItemClicklistiener() {
@Override
public void onItemClicked(int adapterPosition, Class<?> cls) {
Intent intent = new Intent(getContext(), cls);
getContext().startActivity(intent);
}
};
}
答案 0 :(得分:0)
创建一个名为ItemClickListener
的界面,其结构如下:
interface ItemClickListener {
void onItemClicked(int adapterPosition, Class<?> cls);
}
现在在activity
中实现此功能,如下所示:
ItemClickListener listener = new ItemClickListener {
@Override
public void onItemClicked(int position, Class<?> cls) {
Intent intent = new Intent(ActivityName.this, cls);
ActivityName.startActivity(intent);
}
};
将listener
传递给recyclerview
的适配器,并修改适配器的onBindViewHolder
,如下所示:
public void onBindViewHolder(reg_ViewHolder holder, final int position) {
holder.pet_name.setText(items.get(position).getPet_name());
holder.pet_type.setText(items.get(position).getPet_type());
holder.pet_breed.setText(items.get(position).getPet_breed());
holder.btndel.setTag(items.get(position).getPet_name());
holder.btnpet_guide.setTag(items.get(position));
holder.btnpet_guide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
Cursor compare=register_pet_database.getBreed();
String name = compare.getString(compare.getColumnIndex("Pet_breed"));
try {
if (compare.getCount()>0 && compare.moveToFirst()) {
do {
if (name.equals("Chow Chow")) {
listener.onItemClicked(position, dog_chow_chow.class);
break;
}
else if (name.equals("Beagle")) {
listener.onItemClicked(position, dog_beagle.class);
break;
}
} while (compare.moveToNext());
}
}catch (Exception e){
e.printStackTrace();
}
compare.close();
}
});
请更改适配器,如下所示,唯一的区别是提供了一个可以从pet_tab
片段设置的setter方法:
public class reg_Adapter extends RecyclerView.Adapter<reg_ViewHolder>
{
Context data_context;
Register_pet_database register_pet_database;
ArrayList<db_getItem> items;
ItemClicklistiener listener;
db_getItem adapter;
DBAdapter dbAdapter;
public reg_Adapter(Context data_context, ArrayList<db_getItem> items) {
this.data_context= data_context;
this.items = items;
}
public void setItemClickListener(ItemClickListener listener) {
this.listener = listener;
}
public reg_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reg_pet_recycler, parent, false);
return new reg_ViewHolder(view, data_context, items);
}
public void add(db_getItem pet) {
items.add(0, pet);
notifyItemInserted(0);
}
@Override
public void onBindViewHolder(reg_ViewHolder holder, final int position) {
holder.pet_name.setText(items.get(position).getPet_name());
holder.pet_type.setText(items.get(position).getPet_type());
holder.pet_breed.setText(items.get(position).getPet_breed());
holder.btndel.setTag(items.get(position).getPet_name());
holder.btnpet_guide.setTag(items.get(position));
holder.btnpet_guide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
items=new ArrayList<>();
Cursor compare=register_pet_database.getBreed();
try {
if (compare.getCount() > 0 && compare.moveToFirst()) {
do {
String name = compare.getString(compare.getColumnIndex("Pet_breed"));
String chow="Chow Chow";
String beagle="Beagle";
if (name.equals(chow)) {
listener.onItemClicked(position, dog_chow_chow.class);
break;
}
else if (name.equals(beagle)) {
listener.onItemClicked(position, dog_beagle.class);
break;
}
} while (compare.moveToNext());
}
}catch (Exception e){
e.printStackTrace();
}
compare.close();
}
});
holder.btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register_pet_database=new Register_pet_database(v.getContext());
Integer deleted =register_pet_database.deleteData(v.getTag().toString());
if (deleted > 0) {
items.remove(position);
notifyItemRemoved(position);
Toast.makeText(v.getContext(), "Pet Has been Remove", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(v.getContext(), "Pet not Removed", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return items.size();
}
}