Android 从另一个适配器调用notifyondatasetchanged()

Android 从另一个适配器调用notifyondatasetchanged(),android,android-layout,listadapter,Android,Android Layout,Listadapter,可能重复: 我有两个listarray适配器A和B。“A”包含几个按钮,单击这些按钮时应处理“B”中存在的数据,然后通知适配器“B”发生了某些更改,以便“B”刷新自身并显示新数据。 我看到这个问题是在SO早些时候提出的。但解决方案并没有提供足够的信息 谢谢你的解决方案。。。我尝试实现这个功能,但它无法识别按下了哪个按钮(它将整个列表视为一个实体)。而且,即使我尝试保持默认排序,它也不会更新列表内容 headerListView.setOnItemClickListener(new On

可能重复:

我有两个listarray适配器A和B。“A”包含几个按钮,单击这些按钮时应处理“B”中存在的数据,然后通知适配器“B”发生了某些更改,以便“B”刷新自身并显示新数据。 我看到这个问题是在SO早些时候提出的。但解决方案并没有提供足够的信息

谢谢你的解决方案。。。我尝试实现这个功能,但它无法识别按下了哪个按钮(它将整个列表视为一个实体)。而且,即使我尝试保持默认排序,它也不会更新列表内容

    headerListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                    long arg3) {
                sorter = new TwoWaySorter();
                Message msg = new Message();

                Log.i("Click","arg0.getId():"+arg0.getId());
                Log.i("Click", v.getId() + " id is " + v.toString());
                Log.i("Click","arg2:"+arg2+" arg3:"+arg3);
                switch (v.getId()) {

                case R.id.headerList_quantity: // perform sort over the quantity
                    Log.i("Click", "qty clicked");
                    System.out.println("..Item is clicked..");
                    msg.what = UPDATE_QTY;
                    updateListHandler.sendMessage(msg);
                    //sorter.sortBasedOn(contentList, "symQty");
                    //contentAdapter.notifyDataSetChanged();
                    break;
                case R.id.headerList_price: // perform sort over price
                    Log.i("Click", "list price clicked");
                    msg.what = UPDATE_PRICE;
                    updateListHandler.sendMessage(msg);

                    //sorter.sortBasedOn(contentList, "price");
                    //contentAdapter.notifyDataSetChanged();

                    break;
}
}});


 private Handler updateListHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case UPDATE_LIST:
                    sorter = new TwoWaySorter();
                    Log.i("Click", "sort based on priceYesterday");
                    sorter.sortBasedOn(contentList, "priceYesterday");
                    contentAdapter.notifyDataSetChanged();
                    break;

                }

            }};
headerListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0,视图v,内部arg2,
长arg3){
分拣机=新的双向分拣机();
Message msg=新消息();
Log.i(“单击”,“arg0.getId():”+arg0.getId());
Log.i(“单击”,v.getId()+“id是”+v.toString());
Log.i(“单击”,“arg2:+arg2+”arg3:+arg3);
开关(v.getId()){
case R.id.headerList_quantity://对数量进行排序
Log.i(“点击”、“点击数量”);
System.out.println(“…项已单击…”);
msg.what=更新数量;
updateListHandler.sendMessage(消息);
//分拣机sortBasedOn(内容列表,“符号数量”);
//contentAdapter.notifyDataSetChanged();
打破
案例R.id.headerList_price://执行价格排序
Log.i(“点击”、“点击标价”);
msg.what=更新价格;
updateListHandler.sendMessage(消息);
//sortBasedOn(内容列表,“价格”);
//contentAdapter.notifyDataSetChanged();
打破
}
}});
私有处理程序updateListHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
开关(msg.what){
案例更新列表:
分拣机=新的双向分拣机();
Log.i(“单击”,“根据价格排序”);
sorter.sortBasedOn(contentList,“priceforday”);
contentAdapter.notifyDataSetChanged();
打破
}
}};
另外,以防您想看到适配器。请注意,TextView设置为setClickable=“true”

公共类HeaderAdapter扩展了ArrayAdapter{
最终标题[]列表符号;
私人双向分拣机;
私有向量内容列表;
//私有ArrayAdapter内容适配器;
public HeaderAdapter(上下文上下文、int-symResourceID、头[]对象、,
向量内容列表,ArrayAdapter内容适配器){
超级(上下文、符号资源ID、对象);
列表符号=对象;
分拣机=新的双向分拣机();
this.contentList=contentList;
//this.contentAdapter=contentAdapter;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null){
LayoutInflater充气器=(LayoutInflater)getContext()
.getSystemService(上下文布局\充气机\服务);
行=充气机。充气(右布局。双向标题右,父,假);
}
收割台headerRec=列表符号[位置];
TextView symQty=(TextView)行findViewById(R.id.headerList\u数量);
symQty.SETEXT(“+headerRec.symQty”);
Log.i(“单击”,“symQty id”+symQty.getId());
//symQty.setOnClickListener(此);
TextView symPx=(TextView)row.findViewById(R.id.headerList\u price);
symPx.setText(“+headerRec.price”);
Log.i(“单击”,“symPx id”+symPx.getId());
//symPx.setOnClickListener(此);
TextView symPxC=(TextView)行
.findviewbyd(R.id.headerList_priceChange);
symPxC.setText(“+headerRec.priceforday”);
//symPxC.setOnClickListener(此);
TextView symVal=(TextView)row.findViewById(R.id.headerList_值);
符号setText(“+headerRec.daysGain”);
//symVal.setOnClickListener(此);
TextView符号=(TextView)行
.findviewbyd(R.id.校长名单价值变更);
符号setText(“+headerRec.daysGainPercent”);
//symQty.setOnClickListener(此);
TextView symValCP=(TextView)行
.findViewById(R.id.校长的价值变化百分比);
symValCP.SETEXT(“+headerRec.change”);
//symQty.setOnClickListener(此);
TextView符号quote=(TextView)row.findViewById(R.id.headerList_quote);
符号quote.setText(“+headerRec.week52高);
//symQty.setOnClickListener(此);
行.背景颜色(R.颜色.浅灰色);
返回行;
}
/*@凌驾
公共void onClick(视图v){
Log.i(“单击”,v.getId()+“”+v.toString());
开关(v.getId()){
case R.id.headerList_quantity://对数量进行排序
Log.i(“点击”、“点击数量”);
分拣机sortBasedOn(内容列表,“符号数量”);
MainActivity.contentAdapter.notifyDataSetChanged();
打破
案例R.id.headerList_price://执行价格排序
Log.i(“点击”、“点击标价”);
sortBasedOn(内容列表,“价格”);
MainActivity.contentAdapter.notifyDataSetChanged();
打破
案例R.id.headerList\u priceChange://对价格变化进行排序
sorter.sortBasedOn(contentList,“priceforday”);
MainActivity.contentAdapter.notifyDataSetChanged();
Log.i(“点击”,“点击价格变化”);
打破
案例R.id.headerList_值:
Log.i(“单击”,“单击列表值”);
sortBasedOn(内容列表,“变更”);
MainActivity.contentAdapter.notifyDataSetChanged();
打破
案例R.id.headerList\u价值变更:
public class HeaderAdapter extends ArrayAdapter<Header>  {
final Header[] listSymbols;
private TwoWaySorter sorter;
private Vector<Content> contentList;
//private ArrayAdapter<Content> contentAdapter;

public HeaderAdapter(Context context, int symResourceID, Header[] objects,
        Vector<Content> contentList, ArrayAdapter<Content> contentAdapter) {
    super(context, symResourceID, objects);
    listSymbols = objects;
    sorter = new TwoWaySorter();
    this.contentList = contentList;
    //this.contentAdapter = contentAdapter;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.twoway_header_right, parent, false);
    }
    Header headerRec = listSymbols[position];

    TextView symQty = (TextView) row.findViewById(R.id.headerList_quantity);
    symQty.setText("" + headerRec.symQty);
    Log.i("Click","symQty id"+symQty.getId());
    //symQty.setOnClickListener(this);

    TextView symPx = (TextView) row.findViewById(R.id.headerList_price);
    symPx.setText("" + headerRec.price);
    Log.i("Click","symPx id"+symPx.getId());
    //symPx.setOnClickListener(this);

    TextView symPxC = (TextView) row
            .findViewById(R.id.headerList_priceChange);
    symPxC.setText("" + headerRec.priceYesterday);
//  symPxC.setOnClickListener(this);

    TextView symVal = (TextView) row.findViewById(R.id.headerList_value);
    symVal.setText("" + headerRec.daysGain);
//  symVal.setOnClickListener(this);

    TextView symValC = (TextView) row
            .findViewById(R.id.headerList_valueChange);
    symValC.setText("" + headerRec.daysGainPercent);
//  symQty.setOnClickListener(this);

    TextView symValCP = (TextView) row
            .findViewById(R.id.headerList_valueChangePercent);
    symValCP.setText("" + headerRec.change);
//  symQty.setOnClickListener(this);

    TextView symQuote = (TextView) row.findViewById(R.id.headerList_quote);
    symQuote.setText("" + headerRec.week52High);
//  symQty.setOnClickListener(this);

    row.setBackgroundColor(R.color.light_gray);
    return row;
}

/*@Override
public void onClick(View v) {
    Log.i("Click", v.getId() + " " + v.toString());
    switch (v.getId()) {

    case R.id.headerList_quantity: // perform sort over the quantity
        Log.i("Click", "qty clicked");
        sorter.sortBasedOn(contentList, "symQty");
        MainActivity.contentAdapter.notifyDataSetChanged();
        break;
    case R.id.headerList_price: // perform sort over price
        Log.i("Click", "list price clicked");

        sorter.sortBasedOn(contentList, "price");
        MainActivity.contentAdapter.notifyDataSetChanged();

        break;
    case R.id.headerList_priceChange: // perform sort over price change
        sorter.sortBasedOn(contentList, "priceYesterday");
        MainActivity.contentAdapter.notifyDataSetChanged();

        Log.i("Click", "price change clicked");

        break;
    case R.id.headerList_value:
        Log.i("Click", "list value clicked");

        sorter.sortBasedOn(contentList, "change");
        MainActivity.contentAdapter.notifyDataSetChanged();

        break;
    case R.id.headerList_valueChange:
        Log.i("Click", "value change clicked");

        sorter.sortBasedOn(contentList, "daysGain");
        MainActivity.contentAdapter.notifyDataSetChanged();

        break;
    case R.id.headerList_valueChangePercent:
        Log.i("Click", "change percent clicked");

        sorter.sortBasedOn(contentList, "daysGainPercent");
        MainActivity.contentAdapter.notifyDataSetChanged();

        break;
    case R.id.headerList_quote:
        Log.i("Click", "return clicked");

        sorter.sortBasedOn(contentList, "returns");
        MainActivity.contentAdapter.notifyDataSetChanged();

        break;
    //}
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListView extends ListActivity {
    ArrayList<String> pens = new ArrayList<String>();
    ArrayAdapter arrayAdapter = null;
    private static final byte UPDATE_LIST = 100;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pens.add("MONT Blanc");
        pens.add("Gucci");
        pens.add("Parker");

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, pens);
        setListAdapter(arrayAdapter);

        getListView().setTextFilterEnabled(true);
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                System.out.println("..Item is clicked..");
                Message msg = new Message();
                msg.what = UPDATE_LIST;
                updateListHandler.sendMessage(msg);
            }
        });



        // System.out.println("....g1..."+PhoneNumberUtils.isGlobalPhoneNumber("+912012185234"));
        // System.out.println("....g2..."+PhoneNumberUtils.isGlobalPhoneNumber("120121852f4"));

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub

        super.onConfigurationChanged(newConfig);
        System.out.println("...11configuration is changed...");
    }

    void addMoreDataToList() {
        pens.add("item1");
        pens.add("item2");
        pens.add("item3");

    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Object o = this.getListAdapter().getItem(position);
        String pen = o.toString();
        Toast.makeText(this, id + "You have chosen the pen: " + " " + pen,
                Toast.LENGTH_LONG).show();
    }

    private Handler updateListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_LIST:
                addMoreDataToList();
                arrayAdapter.notifyDataSetChanged();
                break;

            }
            ;
        };
    };




}