Raphael Thomas Liewl
Raphael Thomas Liewl

Reputation: 49

List item couldn't pop out dialog when clicked

I have changed this class extends with Acitvity and before that it was extends with ListActivity, this is because I like to use setEmptyView for this class. But after I changed this class extends with Activity, its list items couldn't pop out the dialog when it's clicked.Could anyone give some advices?

ThirdActivity.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>

Upvotes: 0

Views: 221

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Its because you are haven't set OnItemClickListener to ListView. So, your onItemClick() is never called. You have to set OnItemClickListener to ListView explicitly when you are extending your Activity as Activity and not ListActivity.

Upvotes: 2

Related Questions