4

すべて!私はアンドロイドアプリで新しいです。今私はいくつかの問題があります。 List(ListViewをListFragment内で更新した後)を直ちに変更した結果を見ることができません。 たとえば、メソッドをaddNewItemと呼びましたが、画面に何も変更がありませんでした。しかし、私がListFragmentに触れると、すべての変更が見えます。ListFragmentは現時点で変更を表示していません

ListFragment:

public class PointsListFragment extends ListFragment { 

    PlaceItemsAdapter adapter; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     adapter = new PlaceItemsAdapter(
       getActivity(), R.layout.place_list_item, 
       new ArrayList<PlaceItem>()); 
     setListAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
    } 

    public void addNewItem(int id, String address) { 
    adapter.items.add(new PlaceItem(id, address)); 
    adapter.notifyDataSetChanged(); 
    } 


    private class PlaceItemsAdapter extends ArrayAdapter<PlaceItem> { 

     private final int viewResourceId; 
     final public ArrayList<PlaceItem> items; 

     @Override 
     public int getCount() { 
      return items.size(); 
     } 

     @Nullable 
     @Override 
     public PlaceItem getItem(int position) { 
      return items.get(position); 
     } 

     public PlaceItemsAdapter(Context context, int viewResourceId, ArrayList<PlaceItem> items) { 
      super(context, viewResourceId, items); 
      this.items = items; 
      this.viewResourceId = viewResourceId; 

     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(viewResourceId, null); 
      } 
      PlaceItem placeItem = getItem(position); 
      TextView placeIdTextView = (TextView) v.findViewById(R.id.placeId); 
      placeIdTextView.setText(String.valueOf(placeItem.getId())); 
      TextView placeAddressView = (TextView) v.findViewById(R.id.placeAddress); 
      placeAddressView.setText(placeItem.getAddress()); 
      if (selectedValue == position) { 
       v.setBackgroundResource(R.drawable.active_item); 
      } 
      return v; 
     } 

    } 
} 

私の活動のXML

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"[enter image description here][1] 
     android:padding="2dp"> 
     > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:background="@color/forBorder" 
      android:orientation="horizontal"> 

      <Spinner 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/spinner" 
       android:layout_weight="1" /> 

      <TextView 
       android:text="проложить маршрут" 
       android:textColor="@android:color/white" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@android:drawable/ic_media_ff" /> 

     </LinearLayout> 

     <fragment 
      class="com.restfulrobot.cdcapplication.PointsListFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/list_frag" /> 
    </LinearLayout> 

画像例 ListView Problem

+0

これをグローバルに定義し、なぜネストされたクラス(それぞれ異なる親を持つ)を持っていますか? 'final public ArrayList items;' –

+0

あなたは[同じ問題がある]と思います(http://stackoverflow.com/questions/17689357/getting-setlistadapter-to-work) –

+0

私は何をする必要がありますか? –

答えて

0

ALL、ありがとうございます!私は答えを見つけた!別のクラス(アクティビティまたはフラグメントなし)からメソッドを呼び出しました。そして今私は別のクラスのメソッドも呼びますが、ハンドラを通してそれを行います。そして今は本当にうまくいく! 次のようなものがあります。

Message msg = mainActivityHandler.obtainMessage(MainActivity.NEW_MESSAGE, someObjectToAdd); 
mainActivityHandler.sendMessage(msg); 
関連する問題