call notifyondatasetchanged() from another adapter [duplicate]
Possible Duplicate:
Is there a way to call notifyDataSetChanged() from the main activity for a custom adapter of an item inside another custom adapter?
i have two listarray adapters A and B. 'A' contains few buttons that when clicked should process the data present in 'B' and then notify adapter 'B' that some change has occurred so that 'B' refreshes itself and displays new data. I see that this issue was asked earlier here in SO. But the solution does not provide adequate information.
Thank you for the solution... I tried implementing this but it does not recognize which button was pressed (it considers the entire list as one entity). Also, it does not update the list content as well even though I tried keeping a default sort
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;
}
}};
Also, just in case you wanted to see the Adapter. Please note that the TextView are set to setClickable="true"
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 = in开发者_如何学JAVAflater.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;
//}
*/ //} }
Use Handler concept. If you click on any item of list1 then call the handler to update list2.
Sample Example to update any list
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;
}
;
};
};
}
精彩评论