我希望用户能够从列表中选择多个应用。我使用Drawables和CheckedTextViews将自定义布局用于已安装应用的列表及其图标。我创建了一个从ArrayAdapter扩展的类来获取图标。
主要的问题是,当我点击列表中的某个应用程序时,CheckedTextView不会改变它的drawable,但它确实将CheckedTextView设置为已选中,因此功能仍然有效,但如果你看不到哪个功能它不是很有用检查。我还是Android新手,但我认为问题出在我的数组适配器上。
ListActivity:
public class FilterListActivity extends ListActivity {
private AppArrayAdapter adapt;
private ArrayList<String> oldChecked =new ArrayList<String>();
private ArrayMap<String, ApplicationInfo> availableApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filter_list);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
availableApps=getAvailableApps();
ArrayList<String> appArray=new ArrayList<String>(availableApps.keySet());
Collections.sort(appArray);
adapt = new AppArrayAdapter( this, android.R.layout.simple_list_item_checked, appArray);
setListAdapter(adapt);
try{
//check for previous settings
BufferedInputStream buf = new BufferedInputStream(openFileInput("priorityList.txt"));
Scanner scan = new Scanner(buf).useDelimiter(",|\\n");
while (scan.hasNext()){
String appLabel = scan.next();
String packageName = scan.next();
int pos = Integer.parseInt(scan.next());
oldChecked.add(appLabel);
//set item as checked
int i = adapt.getPosition(appLabel);
if (i>=0){
CheckedTextView textView = (CheckedTextView)adapt.getView(i, getCurrentFocus(), getListView()).findViewById(R.id.app_names);
if (textView.getText().toString().equals(appLabel)){
this.getListView().setItemChecked(i, true);
textView.setChecked(true);
}
}
}
}catch(FileNotFoundException e){
//do nothing, list must be created
}
adapt.notifyDataSetChanged();
}
@Override
protected void onListItemClick( ListView l, View v, int position, long id)
{
CheckedTextView textView = (CheckedTextView)v.findViewById(R.id.app_names);
textView.toggle();
adapt.notifyDataSetChanged();
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AppView" >
<ImageView
android:id="@+id/icon"
android:contentDescription="icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_launcher" />
<CheckedTextView
android:id="@+id/app_names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/icon"
android:layout_alignParentRight="true"
android:layout_marginRight="11dp"
android:layout_toRightOf="@+id/icon"
android:checkMark="?android:attr/textCheckMark"
android:text="@string/app_name" />
</RelativeLayout>
阵列适配器:
package edit.rit.ce.whud;
import java.util.List;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v4.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.ImageView;
public class AppArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> objects;
private ArrayMap<String, ApplicationInfo> availableApps;
public AppArrayAdapter(Context context, int resource, List<String> objects)
{
super(context, resource, objects);
this.context = context;
//this.values = values;
this.objects=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final PackageManager pm = this.getContext().getPackageManager();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.appview, parent, false);
CheckedTextView textView = (CheckedTextView) rowView.findViewById(R.id.app_names);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(objects.get(position));
// Change icon based on name
String applabel = objects.get(position);
availableApps=getAvailableApps();
Drawable draw= pm.getApplicationIcon(availableApps.get(applabel));
imageView.setImageDrawable(draw);
return rowView;
}
private ArrayMap<String, ApplicationInfo> getAvailableApps(){
ArrayMap<String, ApplicationInfo> availableApps= new ArrayMap<String, ApplicationInfo>();
final PackageManager pm = this.getContext().getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo app: apps){
if (app.enabled && (app.flags & ApplicationInfo.FLAG_TEST_ONLY)==0){
availableApps.put((String)pm.getApplicationLabel(app), app);
}
}
return availableApps;
}
}