0

ListActivity我使用and创建了一个列表视图Adapter,我想做的是添加按钮。按钮的个数是动态的,实际上在之前的activity中用户可以设置要添加的按钮个数。所以我不能使用布局。下面给出了创建我的 listView 的代码

public class AppList extends ListActivity {

    private String[] mStrings = (String[]) GridViewAppInfoAdapter.lApplication
            .toArray(new String[GridViewAppInfoAdapter.lApplication.size()]);

    public void setApplication() {
        for (int i = 0; i < GridViewAppInfoAdapter.lApplication.size(); i++) {
            mStrings[i] = GridViewAppInfoAdapter.lApplication.get(i).toString();

        }

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("toto", "la valeur de app" + GridViewAppInfoAdapter.lApplication);
        //setContentView(R.layout.listview);
        // setApplication();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, mStrings);

        setListAdapter(adapter);
        ListView listView = getListView();
          listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

}
4

2 回答 2

0

使用自定义适配器在android中动态地将按钮设置为所有列表值

主文件

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
    android:layout_width="fill_parent"
            android:layout_height="fill_parent">


            <ListView android:layout_height="wrap_content"
                            android:layout_below="@+id/EditText01"
     android:layout_width="wrap_content"
                            android:id="@+id/ListView01">
     </ListView>

    </RelativeLayout>

listview.xml //自定义布局以动态添加按钮和文本到ListView

    <?xml version="1.0" encoding="utf-8"?>

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="wrap_content"
      android:gravity="left|center"
            android:layout_width="fill_parent"
       android:paddingBottom="5px"
            android:background="#fff200"
            android:paddingTop="5px"
         android:paddingLeft="5px">

            <Button android:id="@+id/Button01"
                            android:layout_width="wrap_content"
         android:layout_height="wrap_content">
            </ImageView>

                <TextView android:id="@+id/TextView01"
                            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
                            android:textSize="20px"
                            android:textStyle="bold"
                            android:layout_marginLeft="10px"
            android:textColor="#0099CC">
            </TextView>

             </LinearLayout>

MainActivity.java

       public class CustomListViewSearchOnButtonClick extends Activity
      {
            EditText edittext;
            ListView listview;
            Button search;

            String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
                                                            "Eight", "Nine", "Ten" };


            int textlength = 0;
            ArrayList<String> text_sort = new ArrayList<String>();


            public void onCreate(Bundle savedInstanceState)
        {

                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.main);


                            listview = (ListView) findViewById(R.id.ListView01);
                            listview.setAdapter(new MyCustomAdapter(text, image));




       listview.setAdapter(new MyCustomAdapter(text_sort));

                                            }

                            });

            }

MyCustomAdapter.java

             class MyCustomAdapter extends BaseAdapter
       {

                            String[] data_text;


                            MyCustomAdapter()
                     {

                            }

                            MyCustomAdapter(String[] text)
                             {
                                            data_text = text;

                            }

                                               MyCustomAdapter(ArrayList<String>text,
       ArrayList<Integer>image)
               {
                                            data_text = new String[text.size()];


                                            for (int i = 0; i < text.size(); i++) {
                                                            data_text[i] = text.get(i);

                                            }

                            }

                            public int getCount()
        {
                                            return data_text.length;
                            }

                            public String getItem(int position)
           {
                                            return null;
                            }

                            public long getItemId(int position)
            {
                                            return position;
                            }

                            public View getView(int position, View convertView, ViewGroup parent)
      {

                                            LayoutInflater inflater = getLayoutInflater();
                                            View row;

                                            row = inflater.inflate(R.layout.listview, parent, false);

                                            TextView textview = (TextView) row.findViewById(R.id.TextView01);
                                            Button button= (ImageView) row
                                                                            .findViewById(R.id.Button01);

                                            textview.setText(data_text[position]);
                                            imageview.setImageResource(data_image[position]);

                                            return (row);

                            }
            }
       }
于 2013-03-25T09:03:25.920 回答
0

使用自定义适配器,您可以轻松地在布局中添加按钮。您也可以参考本教程。

教程

于 2013-03-25T08:59:11.807 回答