我使用ListActivity
和Adapter
创建了一个列表视图,我想要做的是添加按钮。按钮的数量是动态的,实际上在之前的活动中用户可以设置要添加的按钮的数量。所以我不能使用布局。创建我的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);
}
}
答案 0 :(得分:0)
使用自定义适配器,您可以轻松地在布局中添加按钮。您也可以参考本教程。
答案 1 :(得分:0)
使用自定义适配器在android
中动态设置按钮到所有列表值main.xml中
<?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);
}
}
}