2011-02-09 28 views
2

私のAndroidアプリケーションのナビゲーションでは、ListViewを使用していて、アクティビティのonCreateメソッドでBaseAdapterを作成して設定しています。BaseAdapter.notifyDataSetChangedは要素の順序を逆順にします

BaseAdapterは、要素(cache.getNavigationを())を取得するためのArrayListにアクセスします。

public class NavigationAdapter extends BaseAdapter { 
    Context mContext; 

    public NavigationAdapter(Context c) { 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return cache.getNavigation() != null ? cache.getNavigation().size() 
       : 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     return cache.getNavigation() != null ? cache.getNavigation().get(
       position) : 0; 
    } 

    @Override 
    public long getItemId(int position) { 
     return cache.getNavigation() != null ? cache.getNavigation().get(
       position).getId() : 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v; 
     if (convertView == null) { 
      LayoutInflater li = getLayoutInflater(); 

       v = li.inflate(R.layout.list_nav_icon, null); 
       TextView tv = (TextView) v.findViewById(R.id.list_nav_text); 
       tv.setText(((TemplateInstanceDto) getItem(position)) 
         .getName()); 

       ImageView icon = (ImageView) v 
         .findViewById(R.id.list_nav_icon); 
       byte[] binary = ((TemplateInstanceDto) getItem(position)) 
         .getIcon(); 
       Bitmap bm = BitmapFactory.decodeByteArray(binary, 0, 
         binary.length); 
       icon.setImageBitmap(bm); 

       ImageView arrow = (ImageView) v 
         .findViewById(R.id.list_nav_arrow); 
       arrow.setImageResource(R.drawable.arrow); 


     } else { 
      v = convertView; 
     } 
     return v; 
    } 
} 

ので、ナビゲーションはキャッシュからの起動時に構築されています。私はpopulateData()でより多くの何もしないときは、

private class RemoteTask extends 
     AsyncTask<Long, Integer, List<TemplateInstanceDto>> { 
    protected List<TemplateInstanceDto> doInBackground(Long... ids) { 
     try { 
      RemoteTemplateInstanceService service = (RemoteTemplateInstanceService) ServiceFactory 
        .getService(RemoteTemplateInstanceService.class, 
          getClassLoader()); 

      List<TemplateInstanceDto> templates = service 
        .findByAccountId(ids[0]); 

      return templates; 
     } catch (Exception e) { 
      return null; 
     } 
    } 

    protected void onPostExecute(List<TemplateInstanceDto> result) { 
     if (result != null && result.size() > 0) { 
      cache.saveNavigation(result); 
      populateData(); 
     } else { 
      Toast text = Toast.makeText(ListNavigationActivity.this, 
        "Server communication failed.", 3); 
      text.show(); 
     } 
    } 
} 

リストビューdoesn'tアップデートを: はその間私は、サーバーからのナビゲーションのArrayListを取得し、それがキャッシュに新しいナビゲーションを節約変更されたときにAsyncTaskを開始します。 ((BaseAdapter) ListView.getAdapter()).notifyDataSetChanged()と呼ぶと、ビューは更新されますが、順序は逆になります。最初の項目は最後であり、最後は最初の項目です。

開催必要!前もって感謝します。

答えて

2

問題がgetViewメソッド()関数です。 メソッドの別の呼び出しによって、Viewオブジェクトはキャッシュされますが、位置には関連付けられません。適切なログを

、あなたはまた、この動作を見ることができます:リストを反転なっている理由のthats

pos1: View1 
pos2: View2 
pos3: View3 
pos3: View1 
pos2: View2 
pos1: View3 

。 解決策:オブジェクトが存在する場合でも、getViewの各呼び出しで値をValueに設定します。

public View getView(int position, View convertView, ViewGroup parent) { 
    View v; 
    if (convertView == null) { 
     LayoutInflater li = getLayoutInflater(); 
     v = li.inflate(R.layout.list_nav_icon, null); 
    } else { 
     v = convertView; 
    } 
    TextView tv = (TextView) v.findViewById(R.id.list_nav_text); 
    tv.setText(((TemplateInstanceDto) getItem(position)) 
        .getName()); 

    ImageView icon = (ImageView) v 
        .findViewById(R.id.list_nav_icon); 
    byte[] binary = ((TemplateInstanceDto) getItem(position)) 
        .getIcon(); 
    Bitmap bm = BitmapFactory.decodeByteArray(binary, 0, 
        binary.length); 
    icon.setImageBitmap(bm); 

    ImageView arrow = (ImageView) v 
        .findViewById(R.id.list_nav_arrow); 
    arrow.setImageResource(R.drawable.arrow); 

    return v; 
} 
+0

Perfect answer。私はconvertViewに慣れていませんでしたが、その間に私はそれが何であるかを理解しています。ありがとうございました。 – Konsumierer

0

リモートサービスから新しいデータが正しく返されますか?私はその部分でいくつかのデバッグを行い、投稿されたコードは私のために大丈夫だと思われます。

また、回避策があります:ユーザーのリストビューのStackFromBottom属性:)

+0

リモートサービスが正しく注文されたデータを配信することは間違いありません。 AsyncTaskを手動で起動すると、その順序は毎回反転されます(上端、下端、上端、下端など)。今のところ、 'populateData()'の新しいアダプタを設定する際の回避策が見つかりました: 'ListView.setAdapter(new NavigationAdapter(this))' – Konsumierer

関連する問題