Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ListActivity的底部显示两个按钮_Android_Listactivity - Fatal编程技术网

Android 在ListActivity的底部显示两个按钮

Android 在ListActivity的底部显示两个按钮,android,listactivity,Android,Listactivity,我正在使用教程并成功地实现了一个代码来在扩展ListActivity的活动(以对话框的形式)中显示数据 尽管如此,我还是通过使用对话框主题以下面的格式获取数据 现在,我想知道,如何在底部添加两个按钮 就像我们通常在AlertDialog中看到的那样:确定并取消 CountrycodeActivity.java: public class CountrycodeActivity extends ListActivity { public static String RESULT_CON

我正在使用教程并成功地实现了一个代码来在扩展ListActivity的活动(以对话框的形式)中显示数据

尽管如此,我还是通过使用对话框主题以下面的格式获取数据

现在,我想知道,如何在底部添加两个按钮

就像我们通常在AlertDialog中看到的那样:确定并取消

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;
        }
    }
}
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;
    }
}
公共类CountrycodeActivity扩展了ListActivity{
公共静态字符串结果\u CONTRYCODE=“countrycode”;
公共字符串[]国家名称、国家代码;
专用型Darray imgs;
私人名单国家名单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
populateCountryList();
ArrayAdapter=新的CountryListArrayAdapter(此,countryList);
setListAdapter(适配器);
getListView().setOnItemClickListener(新的AdapterView.OnItemClickListener()){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
国家c=国家列表。获取(位置);
Intent returnIntent=新Intent();
returnIntent.putExtra(RESULT_CONTRYCODE,c.getCode());
setResult(结果正常,返回意图);
imgs.recycle();//回收图像
完成();
}
});
}
私有void populateCountryList(){
countryList=新的ArrayList();
countrynames=getResources().getStringArray(R.array.country_name);
countrycodes=getResources().getStringArray(R.array.country\u代码);
imgs=getResources().obtainTypedArray(R.array.country_flags);
对于(int i=0;i
countrylistarayadapter.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;
        }
    }
}
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;
    }
}
公共类CountryListArrayAdapter扩展了ArrayAdapter{
私人最终名单;
私人最终活动背景;
静态类视窗夹{
受保护的文本视图名称;
受保护的图像视图标志;
}
public CountryListArrayAdapter(活动上下文,列表){
超级(上下文、右布局、活动、国家代码行、列表);
this.context=上下文;
this.list=列表;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=空;
if(convertView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
视图=充气机。充气(R.layout.activity\u countrycode\u行,空);
最终ViewHolder ViewHolder=新的ViewHolder();
viewHolder.name=(TextView)view.findViewById(R.id.name);
viewHolder.flag=(ImageView)view.findViewById(R.id.flag);
view.setTag(viewHolder);
}否则{
视图=转换视图;
}
ViewHolder=(ViewHolder)view.getTag();
holder.name.setText(list.get(position.getName());
setImageDrawable(list.get(position.getFlag());
返回视图;
}
}

在listview中添加这两行

  android:layout_height="0sp"
  android:layout_weight="1"
在listview下方添加此线性布局

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


由于您似乎正在使用
对话框
界面,您可以
设置正按钮
设置负按钮
并相应地设置单击侦听器。

不,如果将其放在列表视图的“下方”,则不会。将其添加到列表视图下方而不是列表项XML我没有在XML中单独使用任何列表视图,检查此代码,您将更清楚地了解我的代码的当前结构: