2012-03-10 7 views
0

私はカスタムアレイリストを持っています。これは、私のアレイリストの各行のアダプタとカスタムXMLに接続されています。アイテムをクリックすると、作成した文字列ビルダの警告が表示され、確認したらカスタムのArray Listに表示されているイメージを変更したいと思います。これまでのところ、選択した項目で新しい画像でリストを更新することはできませんでした。それがどうやってできるか知っていますか?どのように追跡する方法がありますか?リストのどの部分がクリックされましたか?オンレジュームメソッドと、ある種の「再描画」が最適なソリューションですか?Androidカスタムアレイリスト画像の更新

おかげ

編集:私は使用していますアダプタのコードを追加しました:

public class datamanagerAdapter extends ArrayAdapter<mapObject>{ 
private ArrayList<mapObject> items; 
private Context ctx; 
String rememberName=""; 

public datamanagerAdapter(Context context, int textViewResourceId, ArrayList<mapObject> items) { 
    super(context, textViewResourceId, items); 
    this.items = items; 
    this.ctx = context; 
} 

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


     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.row, null); 
     } 
     final mapObject mo = items.get(position); 
     if (mo != null) { 
       ImageView imv = (ImageView) v.findViewById(R.id.selected); 
       TextView one = (TextView) v.findViewById(R.id.mapname); 
       TextView two = (TextView) v.findViewById(R.id.map_contains); 
       TextView three = (TextView) v.findViewById(R.id.map_size); 

       if (one != null) { 
         one.setText(""+mo.getMapName());        
       } 
       if(two != null){ 
         two.setText(""+ mo.getMapContains()); 
       } 
       if(three != null){ 
        three.setText("Size: "+ mo.getMapSize()+"MB  POI: "+mo.getMapContainsPoi()+""); 
       } 
       if (imv != null) { 
        imv.setImageDrawable(mo.getImage()); 
       }else{ 
        imv.setImageDrawable(v.getResources().getDrawable(R.drawable.cross)); 
       } 

       v.setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 

          showDetails(mo, ctx); 
        } 

        private void showDetails(final mapObject mo, final Context ctx) { 
         final Context mContext = ctx.getApplicationContext(); 
         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE); 
         final View layout = inflater.inflate(R.layout.custom_dialog,null); 
         AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
         builder.setView(layout); 

         final AlertDialog alertDialog = builder.create(); 

         TextView l1=(TextView) layout.findViewById(R.id.lineone); 
         TextView l2=(TextView) layout.findViewById(R.id.linetwo); 
         TextView l3=(TextView) layout.findViewById(R.id.linethree); 
         TextView l4=(TextView) layout.findViewById(R.id.linefour); 
         TextView l5=(TextView) layout.findViewById(R.id.linefive); 
         TextView l6=(TextView) layout.findViewById(R.id.linesix); 
         TextView l7=(TextView) layout.findViewById(R.id.lineseven); 

         Button select=(Button) layout.findViewById(R.id.selectActiveMap); 
         Button cancel=(Button) layout.findViewById(R.id.closeWindowButton); 

         rememberName=mo.getMapName(); 

         l1.setText("..."); 
         l2.setText("Map: "+mo.getMapName()); 
         l3.setText("Covered Area: "+mo.getMapContains()); 
         l4.setText("Size: "+mo.getMapSize()+" MB"); 
         l5.setText("POI: "+mo.getMapContainsPoi()); 
         l6.setText("Source: "+mo.getMapSource()); 
         l7.setText("Version: "+mo.getMapVersion()); 

         select.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(View v) { 
          //here I want to change image which is displayed in the list. If I select this map, I ant to have displayed "tick" better than a "cross" which is used by default. 


           } 
           alertDialog.dismiss(); 



          } 
         }); 

         cancel.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(View v) { 
           alertDialog.dismiss(); 

          } 
         }); 





         alertDialog.show(); 
        } 
       }); 
     } 
     return v;  
} 

}

+0

アダプタのコードを投稿してください。 – Luksprog

+0

アダプタコード – Waypoint

答えて

1

あなたgetView()方法のコードが混乱で、それはこの方法では、このような単純なする必要があります:

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

      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) ctx 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.stacktest3_row, null); 
      } 
      final mapObject mo = items.get(position); 
      if (mo != null) { 
       ImageView imv = (ImageView) v.findViewById(R.id.selected); 
       TextView one = (TextView) v.findViewById(R.id.mapname); 
       TextView two = (TextView) v.findViewById(R.id.map_contains); 
       TextView three = (TextView) v.findViewById(R.id.map_size); 

       if (one != null) { 
        one.setText("" + mo.getMapName()); 
       } 
       if (two != null) { 
        two.setText("" + mo.getMapContains()); 
       } 
       if (three != null) { 
        three.setText("Size: " + mo.getMapSize() + "MB  POI: " 
          + mo.getMapContainsPoi() + ""); 
       } 
       if (imv != null) { 
        imv.setImageDrawable(mo.getImage()); 
       } else { 
        imv.setImageDrawable(v.getResources().getDrawable(
          R.drawable.cross)); 
       } 

      } 
      return v; 
     } 

リスト行をクリックしたい場合は、リスナーをクリックしますo ListActivityを拡張する場合はonListItemClick(ListView l, View v, int position, long id)メソッドを実装するか、Activityを拡張する場合はListView要素にsetOnItemClickListener()を設定します。その後onItemClick()方法で:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    mapObject item = l.getItemAtPosition(position); 
      showDetails(item, context); //see what context you can use   
} 

その後、選択ボタンで:

select.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     //If you want to change the image then put your new image(or drawable or what ever) or id in the ITEM mapObject that you pass to the method showDetails() then call notifyDataSetChanged() on your adapter object 
     alertDialog.dismiss(); 
     } 
    }); 
+1

@Waypointダイアログの選択ボタンを押すと、アイテムメソッドmapObjectのイメージが変更メソッドで変更され、 'adapterObject.notifyDataSetChanged()'が呼び出されます。これにより、アダプタにデータが変更されたことが通知され、新しい変更を反映するためにリストがリフレッシュされます。 – Luksprog

+0

ありがとうたくさんのslukian! – Waypoint

関連する問題