2011-07-15 17 views
0

新しいFragmentクラスの使い方を学び、答えが見つからない状況で実行しました。アクティビティからフラグメント内のアダプタを更新する

リスナーが設定されているため、リスト内のアイテムがクリックされたときにそのアイテムが別のフラグメントのGridViewに反映されるようになっています。ここ

は、リスナーである:ここ

public void onBulkScanAttributesFragmentListItemClicked(View v) 
{ 
    TextView nameID = (TextView) v.findViewById(R.id.list_item_with_checkbox_dialog_item_name); 
    String name = (String) nameID.getText(); 
    String value = "Set value"; 

    if (!names.contains(name)) 
    { 
     names.add(name); 
     items.add(new Row(name, value)); 
    } 

    for (Row row : items) 
    { 
     Log.d(TAG, row.getRowName()); 
     Log.d(TAG, row.getRowValue()); 
    } 
    BulkScanChosenAttributes chosen = new BulkScanChosenAttributes(items); 

    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.chosen_att_fragment, chosen); 
    transaction.commit(); 
} 

はコンストラクタとのGridViewを有する断片からonActivityCreateとonCreateView方法は次のとおり パブリッククラスBulkScanChosenAttributesフラグメント { プライベート静的最終的な文字列タグ=「BulkScanChosenAttributes」を延在し; プライベートArrayListアイテム。 プライベートGridAttributeAdapterアダプタ。ここ

public BulkScanChosenAttributes() 
{ 
    items = null; 
} 

public BulkScanChosenAttributes(ArrayList<Row> items) 
{ 
    this.items = items; 
} 

@Override 
public void onActivityCreated(Bundle savedInstance) 
{ 
    super.onActivityCreated(savedInstance); 

    adapter = new GridAttributeAdapter(getActivity(), items); 

    if (items != null) 
    { 
     LayoutInflater gridInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = gridInflater.inflate(R.layout.bulk_scan_chosen, null); 
     GridView gridView = (GridView) v.findViewById(R.id.gridview); 

     gridView.setAdapter(adapter); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    if (adapter != null) 
     adapter.notifyDataSetChanged(); 
    return inflater.inflate(R.layout.bulk_scan_chosen, container, false); 
} 

はGridViewの(R.layout.bulk_scan_chosen)のXMLレイアウトである:

:GridViewの(R.layout.grid_item_layout)におけるビューの

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" /> 

XMLレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
    android:id="@+id/grid_att_name" 
    android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content" 
    android:textColor = "@color/black" 
    android:textSize = "@dimen/font_large" 
    android:textStyle = "bold" 
    android:gravity = "center_horizontal" /> 
    <TextView 
    android:id="@+id/grid_att_value" 
    android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content" 
    android:textColor = "@color/black" 
    android:textSize = "@dimen/font_large" 
    android:textStyle = "bold" 
    android:gravity = "center_horizontal" /> 
</LinearLayout> 
(!これが長くなっている)そして最後に、私のアダプタ:

public class GridAttributeAdapter extends BaseAdapter 
{ 
    private int count; 
    private LayoutInflater inflater; 
    private Context context; 
    private String name; 
    private String value; 
    private ArrayList<Row> items; 

    public GridAttributeAdapter(Context context, ArrayList<Row> items) 
    { 
     int count = 0; 
     this.context = context; 
     this.items = items; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() 
    { 
     return count; 
    } 

    @Override 
    public Object getItem(int position) 
    { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) 
    { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View v = convertView; 
     //try to inflate the view 
     try 
     { 
      v = inflater.inflate(R.layout.grid_item_layout, null); 
     } 
     catch (NullPointerException npe) 
     { 
      npe.printStackTrace(); 
      Log.e(TAG, "view is null"); 
     } 

     Row row = items.get(position); 

     try 
     { 
      TextView name = (TextView) v.findViewById(R.id.grid_att_name); 
      TextView value = (TextView) v.findViewById(R.id.grid_att_value); 

      if (ListMetaData.withDates.contains(ListMetaData.reverseScreenNames.get(row.getRowName()))) 
      { 
       try 
       { 
        Date date = new Date(new Long(row.getRowValue())); 
        value.setText(date.getMonth() + "/" + date.getDate() + "/" + date.getYear()); 
       } 
       catch (NumberFormatException nfe) 
       { 
        value.setText(row.getRowValue()); 
       } 
      } 
      else 
      { 
       value.setText(row.getRowValue()); 
      } 

      name.setText(row.getRowName()); 
     } 
     catch (NullPointerException npe) 
     { 
      npe.printStackTrace(); 
      Log.e(TAG, "something went wrong in TextView assignment"); 
     } 

     return v; 
    } 

} 

アダプタはBulkScanChosenAttributes内で宣言されています。

更新したいBulkScanChosenAttributesフラグメントは空白です。私は、コードが実行されていることを確認するためにチェックしたが、私はまだFragments上で少し不安定です。どんな助けでも大歓迎です。

答えて

0

問題は私のBaseAdapter実装にありました。 getCountメソッドは0を返していました。それを修正した後、期待どおりに機能しました。

+0

こんにちは。弁当箱!アクティビティコードを表示できますか?今私は私のgridViewといくつかの問題がある..または多分電子メール/スカイプ?出来ますか?私のgoogleProfile gplus.to/vsvydenkoありがとう – vsvydenko

関連する問題