1

私のアクティビティでは、2つのリストビューがあるため、2つの異なるアダプターを使用しています。私の要件は、ボタンがあるリスト項目の両方にあります。いずれかのリストビューのボタンをクリックすると、両方のリストビューのデータが変更されます。ここで問題となるのは、あるアダプタのadapter.notifyDataSetChanged()に、別のアダプタのボタンをクリックしてアクセスするにはどうすればよいですか?

4

2 に答える 2

1

単にonClickメソッドで

public void onClick(View v)
{
 adapter1.notifyDataSetChanged();
 adapter2.notifyDataSetChanged();
}

リストビューの行のボタンを使用している場合は、getView()メソッドでこれを行う必要があります

@Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row=convertView;
        if(row==null)
        {
            LayoutInflater inflater=((Activity)context).getLayoutInflater();
            row=inflater.inflate(layoutResourceId, parent,false);

            holder=new YourHodler();
            holder.button=(Button)row.findViewById(R.id.bt);


            row.setTag(holder);
        }
        else
        {
            holder=(YourHolder)row.getTag();
        }

        holder.button.setOnClickListner(new View.onClickListener()
                {
                   //update your Any data set which is beign attached to both the adapters
                   //for example if you are using List<Collection> then you should first
                   //update it or change it
                   //then
                   adapter1.notifyDataSetChanged();
                   adapter2.notifyDataSetChanged();
                }

        return row;
    }
于 2012-12-31T17:54:30.530 に答える
0

アクティビティで次のことを行うメソッドを実装します。

CustomActivity1 extends Activity {
....
//make it public
public void updateLists() {
   CustomAdapter1 adapter1 = (CustomAdapter1) ((ListView) findViewById(R.id.list1)).getListAdapter();
   CustomAdapter2 adapter2 = (CustomAdapter2) ((ListView) findViewById(R.id.list2)).getListAdapter();

   //update the adapters
   adapter1.notifyDatasetChanged();
   adapter2.notifyDatasetChanged();
}
....
}

Activityカスタムアダプタが機能するには、コンテキストを渡す必要があります。したがって、両方のアダプタのonclickリスナーは、次のようなものを追加できます。

CustomAdapter1 { //and in CustomAdapter2 
....
private OnClickListener ButtonClick = new OnClickListener() {
     public void onClick(View v) {
         //...your code             
         //having the context of the CustomActivity1 stored in a variable from the constructor you can simply do:
         customActivity1Context.updateLists();
     }
}
....
}
于 2012-12-31T18:32:49.157 に答える