2016-11-28 47 views
0

我有一個edittext和一個按鈕在片段SpeechToText(ID在Viewpager是2)。當我點擊按鈕,它會發送這個edittext片段MyListWord(在Viewpager ID爲0)(我使用接口)。 (這意味着將數據從Tab 3發送到Tablayout中的Tab 1)如何添加新的項目在片段B的listview從片段A上的點擊按鈕與Viewpager

我在Fragment MyListWord中有一個listview。我如何將此文本添加到此列表視圖?

我的代碼:

片段SpeechtoText:

private SendData mSendata; 

public interface SendData { 
    public void SendDataMainActivity(String contentWord); 
} 


@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     this.mSendata = (SendData) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() + e.toString()); 
    } 
} 

onButtonclickListener:

String contentWord = edittext.getText(); 
mSendata.SendDataMainActivity(contentWord); 

MainActivity實現SpeechToText.SendData

public void SendDataMainActivity(String contentWord) 
{ 
    FragmentManager manager = getSupportFragmentManager(); 
    MyListWord myListWord = (MyListWord)manager.findFragmentByTag("android:switcher:" + pager.getId() + ":" + 0); 
    myListWord.Getdata(contentWord); 
} 

而在片段MyListWord:

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

    lv = (ListView) view.findViewById(R.id.lvMylistword); 

    ArrayList<String> Word = new ArrayList<String>(); 

    adapter = new ArrayAdapter(
      getActivity(), 
      android.R.layout.simple_list_item_1, 
      Word 
    ); 
    lv.setAdapter(adapter); 

    return view; 
} 

public void Getdata(ContentWord contentWord) 
{ 
    if(contentWord.getWordContent().length()!=0) 
    { 
     ////I want add this contentWord to listview ..... 
    } 
} 

在GETDATA功能,我如何單詞添加到列表視圖,我已經嘗試過的getData notifyDatasetChanged(),但無法正常工作。

謝謝大家。

回答

0

鍶爲我愚蠢的問題:d

因爲我送由表3的數據片1,所以它的錯誤。

我編輯片段MyListWord爲製表2,和片段SpeechToText到標籤3,它的成功

這裏我的代碼在GETDATA

public void Getdata(ContentWord contentWord) 
{ 
    if(contentWord.getWordContent().length()!=0) 
    { 
     ArrayList<ContentWord> List_contentWords = new ArrayList<ContentWord>(); 
     List_contentWords = adaptor.getListData(); 
     List_contentWords.add(new ContentWord("ABCD","Noud",0)); 
     adaptor.setListData(List_contentWords); 
     adaptor.notifyDataSetChanged(); 
    } 
} 
相關問題