【问题标题】:how to insert a checkbox in a list如何在列表中插入复选框
【发布时间】:2012-01-08 05:39:51
【问题描述】:

我有这个列表...我应该将复选框作为最后一个元素(“通知”一词)...我不知道该怎么做...因为每个元素都有自己的图标...但最后我必须放一个复选框....

enter code here

公共类 Listview 扩展 Activity {

protected static final Intent CopyOfListview = null;
static ListView listView;


static public class BackgroundWorker extends AsyncTask<Void, Person, Void> {

    @SuppressWarnings("unchecked")
    @Override
    protected void onPreExecute () {
        // Prima di iniziare a inserire gli elementi svuotiamo l'adapter
        ( ( ArrayAdapter<Person> ) listView.getAdapter() ).clear();
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground ( Void... params ) {

        // Qui dentro si possono mettere le operazioni che potrebbero
        // rallentare il caricamento della listview, come ad sempio il
        // caricamento da db degli oggetti

        Person[] people = { new Person( " Privacy",null, R.drawable.freccetta ) };
        Person[] people1 = {new Person( " Visualizzazione", null, R.drawable.freccetta )};
        Person[] people2=  { new Person( " Notifiche", null, R.drawable.freccetta)};

        // riempimento casuale della lista delle persone
        Random r = new Random();

        for ( int i = 0; i < 1; i++ ) {
            // Pubblichiamo il progresso
            publishProgress( people);
            publishProgress( people1);
            publishProgress( people2);

        }

        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onProgressUpdate ( Person... values ) {
        // Aggiungiamo il progresso pubblicato all'adapter
        ( ( ArrayAdapter<Person> ) listView.getAdapter() ).add( values[0] );
        super.onProgressUpdate( values );
    }

}

@Override
public void onCreate ( final Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView( R.layout.main );


    listView = ( ListView ) findViewById( R.id.personListView );

    listView.setAdapter( new PersonAdapter( this, R.layout.row_item, new ArrayList<Person>() ) );

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Person p = (Person) parent.getItemAtPosition(position);

            switch(position) {
            case 0:
                Activity_privacy();
            //  startActivity(CopyOfListview);
                //onCreate1(savedInstanceState);
//  setContentView(R.layout.main1);
            break;
            case 1:
            setContentView(R.layout.main2);
            break;
            case 2:
            setContentView(R.layout.main3);
            }
            }
    });

    new BackgroundWorker().execute();
}


protected void Activity_privacy()
{
    Intent i = new Intent(this, CopyOfListview.class);
    startActivity(i);

}

}

【问题讨论】:

    标签: android android-layout checkbox listadapter


    【解决方案1】:

    添加footer view 是否有某些原因不起作用?

    如果由于某种原因这不是一个可接受的解决方案,我唯一能想到的另一件事是创建一个实现Adapter 接口的自定义类。它必须返回两种不同的视图类型(即getViewTypeCount() 将返回 2)并根据您请求视图的位置从 getView() 返回不同的视图。

    【讨论】:

      【解决方案2】:

      当为列表的最后一个元素调用 getView 时,让您的 PersonAdapter 类返回按钮视图。确保执行此操作时 getCount 返回正确的大小。

      您还可以从让适配器获取视图工厂集合而不是 List 中受益。

      【讨论】:

        猜你喜欢
        • 2011-06-21
        • 1970-01-01
        • 2011-09-05
        • 2014-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        相关资源
        最近更新 更多