【问题标题】:Can I call Multiple adapter classes in android ListView?我可以在 android ListView 中调用多个适配器类吗?
【发布时间】:2013-12-05 07:47:05
【问题描述】:

XML 代码

<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>

我的 .java 文件

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();      
        }
}

在上面的代码中,我使用了显示客户列表的警报对话框 但我也想在同一个中调用 CustomListenerAdapter 显示快速滚动的字母索引器的对话框我可以调用 2 个适配器类吗 关于“客户名单”
如果不是那么请给我其他选项来显示字母索引器 对于对话框中显示的列表视图(弹出)

(在这里你可以看到它的想法https://github.com/andraskindler/quickscroll ... 想为列表视图对话框工作)

【问题讨论】:

    标签: java android listview dialog


    【解决方案1】:

    是的,如果,您可以为 ListView 使用多个适配器,

    • 如果您没有同时使用两个适配器填充 ListView。
    • 如果您使用 Adapter 替代地填充 ListView,那就没问题了。
    • 如果一个适配器不影响另一个适配器,因为一次只有一个适配器有效。

    我认为在您的情况下,您在同一个 Activity 中显示两个 ListView,因此最好使用单独的适配器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多