我想选择列表中显示的应用,然后在选择其中任何一个时出现一个对话框,其中提示用户输入与该特定应用相关联的密码,然后将密码保存到数据库“mysqlite”
以下是我的代码以及我想知道的是我需要更改/添加到我的代码中才能实现此目的?
的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listapps"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
的java:
package com.example.androidproject;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class PasscodeLock extends Activity {
private ListView lView;
private ArrayList results;
List<ResolveInfo> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_passcode_lock);
results = new ArrayList();
lView = (ListView) findViewById(R.id.listapps);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
}
lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_passcode_lock, menu);
return true;
}
}
答案 0 :(得分:0)
首先,在我看来,您已使用 lView.setAdapter 填充了列表,但实际上并未添加允许选择用户选择的功能。
另一件事是,通常首选活动是 ListActivity 而不是Activity,但当然这取决于实现,因为您可能没有全屏ListView。
尽管如此,您可以像这样添加 OnItemClickListener :
public class PasscodeLock extends Activity implements OnItemClickListener(){
....
}
将触发您添加另一个名为onItemClick的函数,在该函数中您将添加代码以调用提示用户输入密码的对话框
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//call dialog
}
关于如何创建该对话框,我不确定,因为我创建的对话框只是向用户显示消息。