您好我已经制作了一个应用程序,它在列表视图中显示已安装的应用程序,并且每行都有切换按钮。 我想显示一个Toast,当点击相应的Switch / toggle按钮时显示应用名称....
错误:当我点击切换按钮时,它没有显示相应的应用程序名称,但它显示其他应用程序名称....
源项目:您可以从这里下载完整的源/项目------> 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();
}
}
答案 0 :(得分:0)
让我们看看你的方法:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
Toast.makeText(mContext, ""+entry.loadLabel(mPackManager), Toast.LENGTH_SHORT).show();
}
在这里,您只使用两个变量来获取文本(entry
和mPackManager
)。您在entry
方法中将mListAppInfo.get(i)
设置为getView()
。这不会起作用,因为这会使entry
ApplicationInfo
与上次加载的View
相关联,这很可能是底部(或顶部)可见视图。这意味着,当您检查列表中的项目时,entry
可能不是您选中的视图的ApplicationInfo
。
那我们该怎么办?我们想要的是ApplicationContext
到已检查的项目,并且该项目在onCheckedChanged
方法中提供给我们buttonView
。那么这个怎么样:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
// This is the TextView you found, and set the text for in getView()
TextView tv = (TextView)v.findViewById(R.id.tvName);
String appName = tv.getText();
Toast.makeText(mContext, appName, Toast.LENGTH_SHORT).show();
}
但是,这并不完美。您可能想要的不仅仅是点击的应用程序的名称,但ApplicationContext
未存储在ListView
项中,因此如果不对代码进行任何更改,我们不能做更多的事情。这个。
如果您希望entry
变量指向所点击的item
,您可以做的是扩展ArrayAdapter
而不是BaseAdapter
和{{1} } Override
方法,提供onItemClicked()
。要获得int index
,您只需撰写ApplicationContext
注意:
我认为最好不要在mListAppInfo.get(index);
中发布整个代码,而是在.rar
或Gist
或类似内容中发布相关类。