2017-10-09 17 views
0

文字列値、すなわちaccountnameはフラグメントに渡されません。断片androidスタジオのアダプタからフラグメントにデータを渡す方法

lvDashboard = (ListView) view.findViewById(R.id.lvDashboard); 

if (getArguments()!= null) { 
    accountname = getArguments().getString("accountname"); 
} 

tasks = new ArrayList<String>(); 
tasks.add(tasks.size(),accountname); 
lvDashboard.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,tasks)); 

アダプタクラスで

Dashboard fragobj = new Dashboard(); 
bundle = new Bundle(); 
bundle.putString("accountname", accountName); 
// set Fragment class Arguments 
fragobj.setArguments(bundle); 

それは正常に見えるが、文字列値は、フラグメントにaccountname変数に格納されていません。

カスタムアダプタにこのような何かリスナー/コールバックを使用することができます
+2

現在のコードでは何が問題になっていますか? –

+0

それはうまく見えますが、satring値はフラグメント内のacountname変数に格納されません –

+0

そのフラグメントインスタンスを使用していますか? – PedroHawk

答えて

0

:あなたのフラグメントにリスナーを設定すると

public class NameAdapter extends ArrayAdapter<String> { 
    ... 

    private AdapterListener mListener; 

    // define listener 
    public interface AdapterListener { 
    void onClick(String name); 
    } 

    // set the listener. Must be called from the fragment 
    public void setListener(AdapterListener listener) { 
    this.mListener = listener; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

    // view initialization 
    ... 

    // here sample for button 
    btButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // get the name based on the position and tell the fragment via listener 
       mListener.onClick(getItem(position)); 
      } 
     }); 

     return convertView; 
    } 
} 

を:

lvDashboard = (ListView) view.findViewById(R.id.lvDashboard); 
lvDashboard.setAdapter(yourCustomAdapter); 
yourCustomAdapter.setListener(new YourCustomAdapter.AdapterListener() { 
    public void onClick(String name) { 
     // do something with the string here. 

    } 
}); 

それとも、あなたから​​3210を使用することができますリストビュー:

lvDashboard.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String name = parent.getItemAtPosition(position); 
     // do something with the string here. 
    } 
}); 
関連する問題