嗨,我制作了一个应用程序,在列表视图中显示已安装的应用程序,并且每行都有切换按钮。我想在单击相应的切换/切换按钮时显示一个显示应用程序名称的 Toast....
错误:当我单击切换按钮时,它没有显示相应的应用程序名称,但它显示了其他应用程序名称....
源项目:您可以从这里下载完整的源/项目 ------> http://www.ziddu.com/download/23238098/ApplicationViewer1.rar.html
请需要你的帮助
课程如下
package com.tech.ashu;
import java.util.List;
import pete.android.study.R;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;
public class AppInfoAdapter extends BaseAdapter implements OnCheckedChangeListener {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;`enter code here`
ApplicationInfo entry;
TextView tvAppName;
public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
entry = mListAppInfo.get(position);
// reference to convertView
View v = convertView;
// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.layout_appinfo, null);
}
// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
tvAppName = (TextView)v.findViewById(R.id.tvName);
Switch s= (Switch)v.findViewById(R.id.switchForActionBar);
//TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
if (s != null)
{
s.setOnCheckedChangeListener(this);
}
else
{
Log.i("tag", "switch1 is null");
}
//tvPkgName.setText(entry.packageName);
// return view
return v;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Toast.makeText(mContext, ""+entry.loadLabel(mPackManager), Toast.LENGTH_SHORT).show();
}
}