編輯:關於Android編程
問題描述:
我創建了一個自定義的 listview,有 Image,Text 和 checkbox。如何檢測是否選擇或未選擇復選框。我使用了 onItemClick() 但是沒有被調用。我使用的正確嗎?
[java]
class PInfo {
String appname = "";
String pname = "";
Drawable icon;
}
public class InstalledApps extends Activity{
private static LayoutInflater inflater=null;
private ListView listview;
private Activity activity;
List<PInfo> installedApps = new ArrayList<PInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.applist);
activity = this;
listview = (ListView)findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo app : packages) {
PInfo newInfo = new PInfo();
newInfo.appname = (String) app.loadLabel(pm);
newInfo.pname = app.packageName;
//newInfo.icon = app.icon;
//Drawable dd = activity.getResources().getDrawable(app.icon);
newInfo.icon = app.loadIcon(getPackageManager());
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(newInfo);
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
installedApps.add(newInfo);
} else {
installedApps.add(newInfo);
}
}
InstalledAppsAdapter iap = new InstalledAppsAdapter(this, installedApps);
listview.setAdapter(iap);
}
}
class InstalledAppsAdapter extends BaseAdapter{
private Activity activity;
private static LayoutInflater inflater=null;
List<PInfo> installedApps = new ArrayList<PInfo>();
ListView listview;
public InstalledAppsAdapter(Activity a, List<PInfo> b) {
// TODO Auto-generated constructor stub
installedApps = b;
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listview = (ListView)activity.findViewById(R.id.listView1);
//Here is where I have put it currently..
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2);
System.out.println(newInfo.appname);
}
});
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return installedApps.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, arg2,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView2);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);
PInfo pinfo = installedApps.get(arg0);
tv.setText(""+pinfo.appname);
iv.setImageDrawable(pinfo.icon);
return customView;
}
}
class PInfo {
String appname = "";
String pname = "";
Drawable icon;
}
public class InstalledApps extends Activity{
private static LayoutInflater inflater=null;
private ListView listview;
private Activity activity;
List<PInfo> installedApps = new ArrayList<PInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.applist);
activity = this;
listview = (ListView)findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo app : packages) {
PInfo newInfo = new PInfo();
newInfo.appname = (String) app.loadLabel(pm);
newInfo.pname = app.packageName;
//newInfo.icon = app.icon;
//Drawable dd = activity.getResources().getDrawable(app.icon);
newInfo.icon = app.loadIcon(getPackageManager());
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(newInfo);
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
installedApps.add(newInfo);
} else {
installedApps.add(newInfo);
}
}
InstalledAppsAdapter iap = new InstalledAppsAdapter(this, installedApps);
listview.setAdapter(iap);
}
}
class InstalledAppsAdapter extends BaseAdapter{
private Activity activity;
private static LayoutInflater inflater=null;
List<PInfo> installedApps = new ArrayList<PInfo>();
ListView listview;
public InstalledAppsAdapter(Activity a, List<PInfo> b) {
// TODO Auto-generated constructor stub
installedApps = b;
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listview = (ListView)activity.findViewById(R.id.listView1);
//Here is where I have put it currently..
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2);
System.out.println(newInfo.appname);
}
});
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return installedApps.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, arg2,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView2);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);
PInfo pinfo = installedApps.get(arg0);
tv.setText(""+pinfo.appname);
iv.setImageDrawable(pinfo.icon);
return customView;
}
}我要在哪裡添加下面的代碼(如果這是正確的listner)
[java]
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2);
System.out.println(newInfo.appname);
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2);
System.out.println(newInfo.appname);
}
});
解決方案:
在 getView 裡面加入下面的代碼:
[java]
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, arg2,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView2);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
}
PInfo pinfo = installedApps.get(arg0);
tv.setText(""+pinfo.appname);
iv.setImageDrawable(pinfo.icon);
return customView;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, arg2,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView2);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
}
PInfo pinfo = installedApps.get(arg0);
tv.setText(""+pinfo.appname);
iv.setImageDrawable(pinfo.icon);
return customView;
}
由於項目需要,今天學習了一下如何將程序裡的數據添加到本地的Excel表中。下面為學習筆記:先上效果圖:首先,需要導入jxl.jar包到libs文件夾內。然後創建Excel
某些情況下我們可能需要與Mysql或者Oracle數據庫進行數據交互,有些朋友的第一反應就是直接在Android中加載驅動然後進行數據的增刪改查。我個人不推薦這種做法,一
Android系統支持的顏色是由4個值組成的,前3個為RGB,也就是我們常說的三原色(紅、綠、藍),最後一個值是A,也就是Alpha。這4個值都在0~255之間。顏色值越
點九圖片的拉伸區域不難理解,顯示內容區域是怎樣的?.9 ,是andriod平台的應用軟件開發裡的一種特殊的圖片形式,文件擴展名為:.9.png智能手機中有自動橫屏的功能,