0

Application クラスに項目のリストがあります。

public class MyApp extends Application {
   private static ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
   //GET AND SET
}

ListViewで使用したいと思います。MyApp リストに 1 つの要素を追加するボタンがあります。

public void addBase(View view){
   MyApp.add2List(....); //add to MyApp.list
   adapter.notifyDataSetChanged();
}

同じアクティビティで、ListView を設定します。

list=(ListView)findViewById(R.id.list_view);         
adapter=new MyListAdapter(this, R.layout.list_item, Session.getList());
list.setAdapter(adapter);

そして、これは私のアダプターです:

public class MyListAdapter extends ArrayAdapter<HashMap<String, String>> {

  private Context _context;
  private Activity activity;
  private ArrayList<HashMap<String, String>> data;
  private static LayoutInflater inflater=null;

  public MyListAdapter(Context context, int resourceId, ArrayList<HashMap<String, String>> items) {
    super(context, resourceId, items);
    this.data = items;
    _context = context;
    activity = ((Activity)context);
    inflater = activity.getLayoutInflater();    
  }


   public View getView(final int position, View convertView, ViewGroup parent) {
      View vi=convertView;
      final ArrayList<HashMap<String, String>> list = Session.getList();
      if(convertView==null){
         vi = inflater.inflate(R.layout.list_item, null);

         ImageButton delete = (ImageButton)vi.findViewById(R.id.delete_item);
         delete.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        list.remove(position);
                        notifyDataSetChanged();
                    }
                });

      }
      return vi;
  }

notifyDataSetChanged()メソッドを使用しましたが、機能しません !! リストビューの更新はありません。
アダプターを再度作成しようとすると、追加ボタンが機能します。

adapter=new MyListAdapter(this, R.layout.list_item, Session.getList());
list.setAdapter(adapter);

削除ボタンに対してこれを行うにはどうすればよいですか?
notifyDataSetChanged() メソッドが機能しないのはなぜですか?

4

3 に答える 3

1

アダプターに渡す ArrayList または Array のオブジェクトを再作成しないでください。同じ ArrayList または Array を再度変更するだけです。また、アダプターを変更した後に配列または arrylist のサイズが変更されていない場合、notifydatasetchange は機能しません。

ショットでは、配列または配列リストのサイズが増減した場合にのみ機能します。

于 2012-11-22T14:10:29.937 に答える
0

アダプターを毎回作り直して解決します。
良い解決策ではありませんが、私が見つけたのはこれだけです。

于 2012-09-12T12:55:04.873 に答える
-1

これを試して

Adapter.clear()
for (HashMap <String,String> object : Session.getList())
Adapter.add(object)
于 2012-09-11T23:33:51.873 に答える