Android的使用权和修改不同的布局 [英] Android use and modify a different layout
问题描述
我需要和活动有一个ListView,显示安装的所有应用程序,它的名称,图标和一个开关,这样当您单击该项目的开关状态的改变,它必须存储与共享preferences。我已经使用这个例子,然后加上 snippet_list_row.xml
的开关。为了改变和存储交换机的地位,从来就改变了 AllAppsActivity.java
来:
I need to make and activity with a listview which shows all the apps installed, its name, icon and a switch so that when you click the item the switch state change and it has to be stored with sharedpreferences. I have used this example and then add a switch on snippet_list_row.xml
. For changing and storing the status of the switch, i´ve changedAllAppsActivity.java
to:
@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();
}
但doesn't商店或改变开关状态,一个我认为这可能是因为它是不活动的布局中。所以,我怎么能解决这个问题?
谢谢!
But it doesn´t store or change the switch status, an I think it could be because it is not inside the activity layout. So, how could I fix it?? Thanks!
推荐答案
尝试是这样的:
在您AllAppsActivity.java的onCreate方法,增加code这行:
In the onCreate method of your AllAppsActivity.java, add this lines of code:
setListAdapter(new CustomArrayAdapter(this, arrayOfApps ,arrayOfAppIcons));
其中 arrayOfApps 为String(应用程序名称)和 arrayOfAppIcons阵列是相应的应用程序图标的排列。
where arrayOfApps is an array of String (app names) and arrayOfAppIcons is array of corresponding app icons.
然后创建code为CustomArrayAdapter.java:
Then create the code for 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文件的列表中的每个项目的。
Note, app_list_item_layout.xml is a layout xml file where you define the layout style for each item in your list.
在您AllAppsActivity.java的onCreate()方法,你应该布局设置为您的主布局的xml文件。
In the onCreate() method of your AllAppsActivity.java, you should set the layout to your main layout xml file.
您不需要使用此之后,使用您的onListItemClick方法。
You don't need to use your onListItemClick method after using this.
编辑:在主布局的xml文件,你应该有一个ListView元素。让其ID为 APPLIST 。于是就把这个code片段在的onCreate()的你AllAppsActivity.java的方法:
In your main layout xml file, you should have a ListView element. Let its id be appList. So then put this code snippet in the onCreate() method of your AllAppsActivity.java:
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));
较早code我提到的是一个更广义的版本,但你应该在你的AllAppsActivity.java使用这个编辑code。
The earlier code I mentioned is a more generalized version, but you should use this edited code in your AllAppsActivity.java.
这篇关于Android的使用权和修改不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!