Android 列表项无法';单击时不会弹出对话框
我已经用Acitvity修改了这个类的extends,之前它是用ListActivity扩展的,这是因为我喜欢用setEmptyView来处理这个类。但是在我改变了这个类之后,当它被点击时,它的列表项不能弹出对话框。有人能给我一些建议吗 ThirdActivity.javaAndroid 列表项无法';单击时不会弹出对话框,android,listview,android-activity,dialog,Android,Listview,Android Activity,Dialog,我已经用Acitvity修改了这个类的extends,之前它是用ListActivity扩展的,这是因为我喜欢用setEmptyView来处理这个类。但是在我改变了这个类之后,当它被点击时,它的列表项不能弹出对话框。有人能给我一些建议吗 ThirdActivity.java public class ThirdActivity extends Activity{ Bundle b; String user,line,result,datapassed,list; DefaultHttpClien
public class ThirdActivity extends Activity{
Bundle b;
String user,line,result,datapassed,list;
DefaultHttpClient httpclient;
HttpPost httppost;
HttpResponse response;
InputStream is = null;
BufferedReader reader;
StringBuilder sb;
ArrayList<NameValuePair> nameValuePairs;
ListView lv;
IntentFilter intentFilter;
int notification = 1;
String str = "";
byte[] data;
int dlength;
String[] item = {"Track Location","Remove Friend"};
ArrayAdapter<String> adapter;
ArrayList<String> friend;
TextView emptyText;
public void onCreate(Bundle savedInstancesState)
{
super.onCreate(savedInstancesState);
setContentView(R.layout.list_screen);
user = getIntent().getExtras().getString("user");
Log.d("dg",user);
lv = (ListView) findViewById(R.id.list);
emptyText = (TextView)findViewById(R.id.empty);
getList();
}
}
public void getList(){
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/tracker/friendlist.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Username", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.d("test",sb.toString());
is.close();
result = sb.toString();
result = result.substring(0, result.length()-1);
// Log.d("result",result);
friend = new ArrayList<String>(Arrays.asList(result.split("[*]")));
Log.d("size",String.valueOf(friend.size()));
runOnUiThread(new Runnable()
{
public void run(){
adapter = new ArrayAdapter<String>(ThirdActivity.this,android.R.layout.simple_list_item_1,friend);
lv.setAdapter(adapter);
lv.setEmptyView(emptyText);
}
});
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}.start();
}
public void onItemClick( View v, final int position, long id){
AlertDialog.Builder adb = new AlertDialog.Builder(ThirdActivity.this);
adb.setItems(item, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ThirdActivity.this, item[which], Toast.LENGTH_LONG).show();
if(item[which].equals("Track Location")){
AlertDialog.Builder adb = new AlertDialog.Builder(
ThirdActivity.this);
adb.setIcon(R.drawable.location);
adb.setTitle("Friend Location");
adb.setMessage("Do you want to track "
+ lv.getItemAtPosition(position) + "'s location?");
adb.setPositiveButton("Yes",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/tracker/track.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
Log.d("12345678",user);
nameValuePairs.add(new BasicNameValuePair("Targetname", (String) lv.getItemAtPosition(position)));
Log.d("Targetname",(String) lv.getItemAtPosition(position));
nameValuePairs.add(new BasicNameValuePair("Username", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.d("test",sb.toString());
is.close();
result = sb.toString();
Intent i = new Intent(ThirdActivity.this,Map.class);
i.putExtra("location", result);
startActivity(i);
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}.start();
}
});
adb.setNegativeButton("No", null);
adb.show();
}else{
AlertDialog.Builder adb=new AlertDialog.Builder(ThirdActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + (String) lv.getItemAtPosition(position));
Log.d("index", String.valueOf(position));
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
String delete = (String) ((lv.getAdapter()).getItem(position));
public void onClick(DialogInterface dialog, final int which) {
// lv.invalidateViews();
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/tracker/remove.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
Log.d("12345678",user);
nameValuePairs.add(new BasicNameValuePair("Targetname", (String) lv.getItemAtPosition(position)));
nameValuePairs.add(new BasicNameValuePair("Username", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
data = new byte[256];
StringBuffer buffer = new StringBuffer();
int len = 0; //100
while (-1 != (len = is.read(data)) )
{
buffer.append(new String(data, 0, len));
}
Log.d("MainActivity", buffer.toString());
if(buffer.charAt(0)=='Y')
{
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(ThirdActivity.this, (String)lv.getItemAtPosition(position)+" has removed from friendlist", Toast.LENGTH_LONG).show();
friend.remove(delete);
adapter.notifyDataSetChanged();
}
});
}
else
{
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(ThirdActivity.this, "Friend remove failed", Toast.LENGTH_LONG).show();
}
});
}
is.close();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
}
}.start();
}});
adb.show();
}
}
});
AlertDialog ad = adb.create();
ad.show();
}
公共类第三活动扩展活动{
束b;
字符串用户、行、结果、传递的数据、列表;
默认httpclient httpclient;
HttpPost-HttpPost;
HttpResponse响应;
InputStream=null;
缓冲读取器;
为某人做准备;
ArrayList名称值对;
ListView lv;
IntentFilter IntentFilter;
int通知=1;
字符串str=“”;
字节[]数据;
长度;
字符串[]项={“跟踪位置”,“删除好友”};
阵列适配器;
阿雷利斯特的朋友;
文本视图清空文本;
创建时的公共void(Bundle savedinstancestate)
{
super.onCreate(savedinstancestate);
setContentView(右布局列表屏幕);
user=getIntent().getExtras().getString(“用户”);
日志d(“dg”,用户);
lv=(ListView)findViewById(R.id.list);
emptyText=(TextView)findViewById(R.id.empty);
getList();
}
}
public void getList(){
新线程(){
公开募捐{
试一试{
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://www.kryptoquest.com/tracker/friendlist.php");
ArrayList nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“用户名”,用户));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
is=response.getEntity().getContent();
}捕获(例外e){
Log.e(“Log_标记”,“错误:+e.toString());
}
//将响应转换为字符串
试一试{
reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8);
sb=新的StringBuilder();
行=空;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
Log.d(“test”,sb.toString());
is.close();
结果=sb.toString();
result=result.substring(0,result.length()-1);
//Log.d(“结果”,结果);
friend=newArrayList(Arrays.asList(result.split(“[*]”));
Log.d(“size”,String.valueOf(friend.size());
runOnUiThread(新的Runnable()
{
公开募捐{
adapter=newarrayadapter(third活动。this,android.R.layout。简单列表\项目\ 1,friend);
低压设置适配器(适配器);
lv.setEmptyView(emptyText);
}
});
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
}
}.start();
}
public void onItemClick(视图v,最终整数位置,长id){
AlertDialog.Builder adb=新建AlertDialog.Builder(ThirdActivity.this);
adb.setItems(项,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int其中){
Toast.makeText(ThirdActivity.this,item[which],Toast.LENGTH_LONG.show();
if(项[which].equals(“轨道位置”)){
AlertDialog.Builder adb=新建AlertDialog.Builder(
第三十届;
adb.设置图标(R.可绘制位置);
adb.setTitle(“朋友地址”);
adb.setMessage(“您想跟踪吗”
+lv.getItemAtPosition(位置)+“的位置?”);
adb.setPositiveButton(“是”,
新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton)
{
新线程(){
公开募捐{
试一试{
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://www.kryptoquest.com/tracker/track.php");
nameValuePairs=新的ArrayList(2);
Log.d(“12345678”,用户);
添加(新的BasicNameValuePair(“Targetname”,(String)lv.getItemAtPosition(position));
Log.d(“Targetname”,(字符串)lv.getItemAtPosition(位置));
添加(新的BasicNameValuePair(“用户名”,用户));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
is=response.getEntity().getContent();
}捕获(例外e){
Log.e(“Log_标记”,“错误:+e.toString());
}
//将响应转换为字符串
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/no_friend"
android:gravity="center_vertical|center_horizontal"
/>
</LinearLayout>