0

我已经用 Acitvity 更改了这个类扩展,而在此之前它是用 ListActivity 扩展的,这是因为我喜欢对这个类使用 setEmptyView。但是在我用Activity更改了这个类扩展后,它的列表项在单击时无法弹出对话框。有人能给点建议吗?

第三活动.java

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();       
    }

list_screen.xml

 <?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>
4

1 回答 1

2

这是因为您尚未设置OnItemClickListenerListView. 所以,你onItemClick()的永远不会被调用。OnItemClickListener当您扩展ActivityasActivity和 not时,您必须明确设置为 ListView ListActivity

于 2012-12-17T07:05:01.667 回答