誰能幫助解決這個問題?如何獲得recyclerview中物品的位置並獲取它用於比較
這種情況是,我有一個來自sqlite的數據來填充它的recyclerview。 當我點擊一個項目時,一個新的活動將開始,這是每個項目後唯一的。
但問題是,當我點擊來自recyclerview的項目B中的項目時,它始終會啓動項目A活動,我不想要這些活動,並且對於回收站視圖中的每個項目都是相同的。
是他們正確的方式來得到這個權利?
我的繼承人爲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.quearyData("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);
}
};
}
您應該發佈您的代碼以獲取一些幫助,至少你的點擊事件處理程序。 –