问题
XML code
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000">
</ListView>
My .java file
public class TestListView extends Activity{
private DatabaseConection db;
Button btn;
private List<Customer> custList = new ArrayList<Customer>();
private CustomListenerAdapter custListAdaptor;
private ListView list;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_listview);
// Connection object for Login
db= new DatabaseConection(this);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
createListViewDialog(btn);
}
});
try{
list = (ListView) findViewById(R.id.listView1);
// populate the store list
custList = db.getAllCustomers();
// create an adaptor with the store list
custListAdaptor = new CustomListenerAdapter(this,custList);
// assign the listview an adaptor
list.setAdapter(custListAdaptor);
}catch(Exception e){
e.getStackTrace();
}
}
// on button click i called dialog wich contains List (sorted)
public void createListViewDialog(final Button btnId) {
AlertDialog levelDialog = null;
// Creating and Building the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final ArrayAdapter<Customer> ad = new ArrayAdapter<Customer> (this,android.R.layout.simple_list_item_1,custList.toArray(new Customer[custList.size()]));
builder.setSingleChoiceItems(ad, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
btnId.setText(ad.getItem(item).toString());
}
});
levelDialog = builder.create();
levelDialog.show();
}
}
In the above code i have used Alert Dialog which shows List of customers
but i also want to call CustomListenerAdapter in same
dialogbox to show alphabetical indexer for fast scroll can i call 2 adapter classes
on "CustList"
if not then please give me other options to show alphabetical indexer
for list view shown in dialog box (popup)
(here you can see idea for it https://github.com/andraskindler/quickscroll ... want to work for list view dialog box)
回答1:
Yes you can use Multiple Adapter for ListView if,
- If you are not populating your ListView with both Adapters at the same time.
- If you are populating your ListView alternatively with the Adapter then its fine.
- If one Adapter is not affecting the other as only one Adapter will be in effect at a time.
I think in your case you are showing both ListView at the same Activity so its better to use a seperate Adapter
来源:https://stackoverflow.com/questions/20394267/can-i-call-multiple-adapter-classes-in-android-listview