2015-12-06 53 views
2

在db更改後,我在適配器上調用notifyDataSetChanged方法,但此方法不更新視圖。上cardview上抽頭RecyclerView中的notifyDatasetChanged不能從片段中工作

MyClass的片段打開我使用的界面處的外部活動接收事件onActivityResult(此火災正確地)

public interface StartActivityForResultInterface { 
    public void myStartActivityForResult(Intent intent, int requestCode); 
} 

MyClass的片段

public class ContentFragment extends Fragment implements StartActivityForResultInterface { 

    private PaffVocaliAdapter m_adapter; 

    @Override 
    public void myStartActivityForResult(Intent intent, int requestCode) { 
     // do pre-processing here if you need to 
     startActivityForResult(intent, requestCode); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.content_fragment,container,false); 


     dbmanPaffVocali loc_dbmanPafflocali = new dbmanPaffVocali((getActivity())); 
     List<PaffVocali> items = loc_dbmanPafflocali.lista(); 

     if (items.size() == 0) 
     { 
      for (int i = 0; i < 10; i++) { 
       PaffVocali p = new PaffVocali(); 
       p.setTitolo("Tab #" + " item #" + i); 
       items.add(p); 
      } 
     } 

     m_adapter = new PaffVocaliAdapter(items); 
     m_adapter.setCallback(this); 

     RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.recyclerview); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     recyclerView.setAdapter(m_adapter); 

     return v; 
    } 


    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     m_adapter.notifyDataSetChanged(); 
    } 

} 

myadapter類

public class PaffVocaliAdapter extends RecyclerView.Adapter<PaffVocaliAdapter.ViewHolder> { 

private List<PaffVocali> mItems; 
private StartActivityForResultInterface myInterface; 

PaffVocaliAdapter(List<PaffVocali> items) { 
    mItems = items; 
} 

public void setCallback (StartActivityForResultInterface myInterface) { 
    this.myInterface = myInterface; 
} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_listapaff, viewGroup, false); 

    return new ViewHolder(v); 
} 

@Override 
public void onBindViewHolder(ViewHolder viewHolder, int i) { 
    final PaffVocali item = mItems.get(i); 
    viewHolder.mTextViewTitolo.setText(item.getTitolo()); 

    viewHolder.mTextViewEdit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Context context = view.getContext(); 
      //context.startActivityForResult(new Intent(context, crud_activity_paff.class)); 
      Intent intent = new Intent(context, crud_activity_paff.class); 
      intent.putExtra("id",item.getId()); 
      myInterface.myStartActivityForResult(intent, 1); 
     } 
    }); 
} 



@Override 
public int getItemCount() { 
    return mItems.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 

    private final TextView mTextViewTitolo; 
    private final TextView mTextViewEdit; 

    ViewHolder(View v) { 
     super(v); 
     mTextViewTitolo = (TextView) v.findViewById(R.id.cardpaff_titolo); 
     mTextViewEdit = (TextView) v.findViewById(R.id.cardpaff_edit); 

    } 
} 
} 

我確定數據庫已更改,新數據顯示爲我重新啓動應用程序。

+0

是您的應用程序在onActivityResult進入()方法? –

回答

1
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    dbmanPaffVocali loc_dbmanPafflocali = new dbmanPaffVocali((getActivity())); 
    List<PaffVocali> items = loc_dbmanPafflocali.lista(); 

    if (items.size() == 0) 
    { 
     for (int i = 0; i < 10; i++) { 
      PaffVocali p = new PaffVocali(); 
      p.setTitolo("Tab #" + " item #" + i); 
      items.add(p); 
     } 
    } 
    m_adapter.setMItems(items);//add the method 
    m_adapter.notifyDataSetChanged(); 
} 

儘量

+0

你介意添加一些關於你的代碼如何解決這個問題的解釋嗎? – andrewsi

1

您呼叫的notifyDataSetChanged,但你沒有更新的適配器類的數據,這就是爲什麼你沒有看到在RecyclerView任何變化的原因。在調用notifyDataSetChanged之前,您需要更新數據(將新數據傳遞給適配器)。

更改onActivityResult,如:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    dbmanPaffVocali loc_dbmanPafflocali = new dbmanPaffVocali((getActivity())); 
    List<PaffVocali> items    = loc_dbmanPafflocali.lista(); 

    if (items.size() == 0) 
    { 
     for (int i = 0; i < 10; i++) 
     { 
      PaffVocali p = new PaffVocali(); 
      p.setTitolo("Tab #" + " item #" + i); 
      items.add(p); 
     } 
    } 

    m_adapter.setData(items); 
    m_adapter.notifyDataSetChanged(); 
} 

而在你的適配器類實現對新數據傳遞的公共方法:

public void setData(List<PaffVocali> items) 
{ 
    mItems = items; 
} 
相關問題