我正在使用this教程并成功实现了一个代码,用于在Activity中显示数据(以Dialog的形式),扩展了ListActivity
尽管如此,我仍然使用Dialog主题
以下面的格式获取数据现在,我想知道,如何在底部添加两个按钮?
就像我们通常在AlertDialog中看到的那样:OK和CANCEL
CountrycodeActivity.java:
public class CountrycodeActivity extends ListActivity {
public static String RESULT_CONTRYCODE = "countrycode";
public String[] countrynames, countrycodes;
private TypedArray imgs;
private List<Country> countryList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
populateCountryList();
ArrayAdapter<Country> adapter = new CountryListArrayAdapter(this, countryList);
setListAdapter(adapter);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Country c = countryList.get(position);
Intent returnIntent = new Intent();
returnIntent.putExtra(RESULT_CONTRYCODE, c.getCode());
setResult(RESULT_OK, returnIntent);
imgs.recycle(); //recycle images
finish();
}
});
}
private void populateCountryList() {
countryList = new ArrayList<Country>();
countrynames = getResources().getStringArray(R.array.country_names);
countrycodes = getResources().getStringArray(R.array.country_codes);
imgs = getResources().obtainTypedArray(R.array.country_flags);
for(int i = 0; i < countrycodes.length; i++){
countryList.add(new Country(countrynames[i], countrycodes[i], imgs.getDrawable(i)));
}
}
public class Country {
private String name;
private String code;
private Drawable flag;
public Country(String name, String code, Drawable flag){
this.name = name;
this.code = code;
this.flag = flag;
}
public String getName() {
return name;
}
public Drawable getFlag() {
return flag;
}
public String getCode() {
return code;
}
}
}
CountryListArrayAdapter.java:
public class CountryListArrayAdapter extends ArrayAdapter<CountrycodeActivity.Country> {
private final List<CountrycodeActivity.Country> list;
private final Activity context;
static class ViewHolder {
protected TextView name;
protected ImageView flag;
}
public CountryListArrayAdapter(Activity context, List<CountrycodeActivity.Country> list) {
super(context, R.layout.activity_countrycode_row, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.activity_countrycode_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.flag = (ImageView) view.findViewById(R.id.flag);
view.setTag(viewHolder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.name.setText(list.get(position).getName());
holder.flag.setImageDrawable(list.get(position).getFlag());
return view;
}
}
答案 0 :(得分:0)
在列表视图中添加这两行
android:layout_height="0sp"
android:layout_weight="1"
在列表视图下方添加此线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
答案 1 :(得分:0)
由于您似乎正在使用Dialog
界面,因此您可以setPositiveButton
和setNegativeButton
并相应地设置点击侦听器。