我是android的初学者。我想在谷歌翻译的帮助下通过我的应用程序打开已安装的应用程序。 就像用户说" whatsapp"然后我的应用程序将其转换为文本。通过文本我将打开已安装的应用程序。 我已经完成了文本编码和安装应用程序列表编码的演讲。
Google演讲文字
我已将谷歌翻译文本的值存储到String s1
列出ApplicationInfo
btnopen = (Button) findViewById(R.id.btnopen);
btnopen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s1=txtText.getText().toString();
String s2=applist.get(position).toString();
if(s1 == s2){
packageManager = context.getPackageManager();
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(AllAppsActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(AllAppsActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(getApplicationContext(), txtText.getText().toString()+" is not match", Toast.LENGTH_LONG).show();
}
}
private PackageManager getPackageManager() {
// TODO Auto-generated method stub
return null;
}
});
ApplicationAdapter
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
public ApplicationAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> appsList) {
super(context, textViewResourceId, appsList);
this.context = context;
this.appsList = appsList;
packageManager = context.getPackageManager();
}
@Override
public int getCount() {
return ((null != appsList) ? appsList.size() : 0);
}
@Override
public ApplicationInfo getItem(int position) {
return ((null != appsList) ? appsList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.snippet_list_row, null);
}
ApplicationInfo data = appsList.get(position);
if (null != data) {
TextView appName = (TextView) view.findViewById(R.id.app_name);
TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);
appName.setText(data.loadLabel(packageManager));
packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
};
ApplicationAdapter 并列出 Applicationinfo ,为我提供已安装应用程序的列表。
如何比较ListView中的 s1 ?
答案 0 :(得分:0)
if(s1 == s2)
你是comparingen字符串的引用,而不是实际值。下面的代码将比较实际文本。
if(s1.equals(s2))
我不知道这是否是你的实际问题,很难从你提出问题的方式解读它。