2017-02-21 50 views
1

已經有2天了,我仍然不知道爲什麼當我在sqlite數據庫中插入數據時,我的RecyclerView不能立即更新。如何在Sqlite數據庫中添加新數據時更新回收站視圖

我一直在嘗試了許多方法,例如nofityDataSetChangedswapAdapter但是我的RecyclerView將不會更新,更新它,我需要去到另一個活動之前的數據已經被添加到RecyclerView

所以誰能告訴我我在哪裏做錯了?

繼承人我查詢

public void AddData(){ 
    reg_pet.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      items=new ArrayList<>(); 
      final int position=0; 
      adapter=new reg_Adapter(getActivity(),items); 
      boolean insterdata = myDB.insertDataToPetRegister(pet_name.getText().toString(), 
        spin_type.getSelectedItem().toString(), spin_breed.getSelectedItem().toString()); 
        if (insterdata = true) { 
         adapter.notifyItemInserted(position); 
         adapter.notifyDataSetChanged(); 
         pet_name.setText(""); 
        Toast.makeText(getActivity(), "Your Pet has been Registered", Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getActivity(), "Your Pet has not been Registerd", Toast.LENGTH_LONG).show(); 
       } 
     } 
    }); 
} 

我的適配器

public class reg_Adapter extends RecyclerView.Adapter<reg_ViewHolder> { 
Activity activity; 
Register_pet_database register_pet_database; 
List<db_getItem> items; 
db_getItem adapter; 
DBAdapter dbAdapter; 
public reg_Adapter(Activity activity, List<db_getItem> items) { 
    this.activity = activity; 
    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); 
} 



@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.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(); 
} 

}

我在我的片段代碼

代碼
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; 
ImageButton btndel; 
TextView pet_name; 
    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); 
     btndel=(ImageButton)view.findViewById(R.id.btndel); 
     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.swapAdapter(adapter,false); 
    } 


} 

回答

0

有沒有必要做notifyItemInsertednotifyDataSetChanged,只有前者,只要你有正確的位置。但所有通知方法都是刷新視圖。您還必須確保實際的數據集已更新。

您的代碼卡閱讀並遵照因爲PHP代碼的風格,所以我還沒有更新你的原始樣本,但基本上:

在上下文:

Pet myPet = new Pet(...); 
boolean isSuccessful = db.insert(myPet.getName(), ..., ...); 
if (isSuccessful) { 
    mAdapter.add(myPet); 
} 

在適配器:

public void add(Pet pet) { 
    mData.insert(0, pet); 
    notifyItemInserted(0); 
} 

一般來說,restrict responsibility 。如果您發現自己不得不手動跟蹤位置和呼叫等事件以通知其所有者(本例中爲適配器)外部的方法,則會出現問題。

另一種選擇是依靠Loader自動監測變化