【问题标题】:Android use and modify a different layoutAndroid 使用和修改不同的布局
【发布时间】:2016-01-10 16:54:34
【问题描述】:

我需要使用一个列表视图来创建和活动,该列表视图显示所有已安装的应用程序、其名称、图标和一个开关,以便当您单击该项目时,开关状态会发生变化,并且它必须与 sharedpreferences 一起存储。我使用了this example,然后在snippet_list_row.xml 上添加了一个开关。为了更改和存储开关的状态,我已将AllAppsActivity.java 更改为:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    ApplicationInfo app = applist.get(position);
    Switch appSwitch = (Switch) findViewById(R.id.app_switch);
    if (appSwitch.isChecked()){
        appSwitch.setChecked(false);
    }else{
        appSwitch.setChecked(true);
    }
    PreferenceManager.getDefaultSharedPreferences(AppsNotificationsActivity.this).edit().putString(app.packageName , String.valueOf(appSwitch.isChecked())).commit();
}

但它不会存储或更改开关状态,我认为这可能是因为它不在活动布局内。那么,我该如何解决呢? 谢谢!

【问题讨论】:

    标签: android android-layout listview sharedpreferences


    【解决方案1】:

    试试这样的:

    在 AllAppsActivity.java 的 onCreate 方法中,添加以下代码行:

    setListAdapter(new CustomArrayAdapter(this, arrayOfApps ,arrayOfAppIcons));
    

    其中 arrayOfApps 是字符串(应用名称)数组,arrayOfAppIcons 是对应应用图标的数组。

    然后为 CustomArrayAdapter.java 创建代码:

    public class CustomArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String [] appnames;
    private final IconType [] appicons; //Replace IconType by your icon images' datatype
    
    
    public CustomArrayAdapter(Context context, String[] appnames,IconType[] appicons ) {
        super(context, R.layout.app_list_item_layout, appnames);
        this.context = context;
        this.appnames = appnames;
        this.appicons = appicons;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View rowView = inflater.inflate(R.layout.app_list_item_layout, parent, false);
    
    //The following variables are references to the elements within each list item displayed (in app_list_item_layout xml file)
        TextView appNameView = (TextView) rowView.findViewById(R.id.appName);
        ImageView appIconView = (ImageView)  rowView.findViewById(R.id.appIcon);      
    //If you are using something else to dislay app icons, replace ImageView by corresponding datatype
        Switch appSwitch = (Switch) rowView.findViewById(R.id.app_switch);
    
    
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                // Do whatever u want here on any List item click
                // the variable "position" indicates which particular list item was clicked
            }
        });
    
    
    
        return rowView;
    }
    

    }

    注意,app_list_item_layout.xml 是一个布局 xml 文件,您可以在其中定义 列表中每个项目的布局样式

    在 AllAppsActivity.java 的 onCreate() 方法中,您应该将布局设置为您的主布局 xml 文件。

    使用这个之后就不需要再使用你的 onListItemClick 方法了。

    编辑: 在您的主布局 xml 文件中,您应该有一个 ListView 元素。让它的 id 为 appList。然后把这段代码 sn-p 放在你的 AllAppsActivity.java 的 onCreate() 方法中:

    appListView = (ListView)findViewById(R.id.appList); //appList element exists in your main layout xml file for your activity
    appListView.setItemsCanFocus(true);
    appListView.setAdapter(new CustomArrayAdapter(this, arrayOfApps ,arrayOfAppIcons));
    

    我之前提到的代码是一个更通用的版本,但是你应该在你的 AllAppsActivity.java 中使用这个编辑过的代码。

    【讨论】:

    • 但这并不能解决我的错误,实际上它给了我更多错误,我只需要能够从 AllAppsActivity.java 管理 sn-p_list_row.xml 上的开关
    • Switch appSwitch = (Switch) rowView.findViewById(R.id.app_switch);
    • rowView 显示为红色,好像它不存在一样,我应该放什么?我试过 sn-p_list_row 但它不起作用
    • 在您的活动 - 布局文件中,您应该包含一个列表视图。现在编辑我的答案。
    • @Javierd98 :检查这个编辑过的答案。自定义数组适配器中定义的元素是对 app_list_item_layout xml 文件中元素的引用,而在 onCreate() 中定义的 appListView 是对主活动 xml 文件的引用,该文件应包含一个列表视图元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多