2016-03-15 47 views
0
public class MainActivity extends Activity { 

    ListView list; 
    LinearLayout ll; 
    Button loadBtn; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ll = (LinearLayout) findViewById(R.id.LinearLayout1); 

    list = (ListView) findViewById(R.id.listView1); 

    loadBtn = (Button) findViewById(R.id.button1); 
    loadBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      LoadContacts lc=new LoadContacts(); 
      if(lc.execute()!=null) 
      { 
      Toast toast=Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_LONG); 
      toast.show(); 
      } 

     } 
    }); 

} 

    class LoadContacts extends AsyncTask<Void, Void, ArrayList<String>> 
    { 

     ProgressDialog pd; 
     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

      pd = ProgressDialog.show(MainActivity.this, "Loading Contacts", 
        "Please Wait"); 
     } 
     protected ArrayList<String> doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      ArrayList<String> contacts = new ArrayList<String>(); 

      Cursor c = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
        null, null, null); 
      while (c.moveToNext()) { 

       String contactName = c 
         .getString(c 
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String phNumber = c 
         .getString(c 
           .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

       contacts.add(contactName + ":" + phNumber); 

      } 
      c.close(); 

      return contacts; 
     } 
     @Override 
     protected void onPostExecute(ArrayList<String> contacts) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(contacts); 

      pd.cancel(); 

      ll.removeView(loadBtn); 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getApplicationContext(), R.layout.text, contacts); 

      list.setAdapter(adapter); 

      list.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this); 
        adb.setTitle("call & message"); 
        adb.setPositiveButton("call", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          //write here call coding 

         } 
        }); 

        adb.setNegativeButton("message", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          //write here message coding 
         } 
        });  
        }    

      });    
} 
} 
} 

這是我的MainActivity.java文件。當我刪除對話框代碼其工作正常和加載的聯繫人,但與對話框代碼不工作,請任何人幫助我。如何在單擊由AsyncTask生成的listview項後打開對話框?

回答

0

打電話給你的web服務後

list = (ListView) findViewById(R.id.listView1); 

和調用的onCreate後的web服務(寫列表視圖clicklistener)。

因此步驟將是,

  • 列表=(ListView中)findViewById(R.id.listView1)
  • 新LoadContacts()。執行()
  • list.setOnItemClickListener()
+0

AsyncTask是異步的,所以你可以在執行 –

+0

之前設置項目點擊監聽器是的,這就是對的。 AsyncTask是異步的,你也可以在之前調用它。 –

+0

如果我在AsyncTask之前設置了項目點擊偵聽器,那麼它如何從我的手機加載聯繫人,因爲AsyncTask在ListView中加載聯繫人。不知道它是如何工作的,它仍然不工作 – Jeevan

1

只需使用setOnItemClickListener代替onClickListener:

  list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // TODO Auto-generated method stub 
       AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this); 
       adb.setTitle("call & message"); 
       adb.setPositiveButton("call", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         //write here call coding 

        } 
       }); 

       adb.setNegativeButton("message", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         //write here message coding 
        } 
       }); 
      } 
     }); 
0

您需要使用list.setOnItemClickListener來點擊列表中的特定項目。

list.setOnClickListener將允許您點擊列表視圖,是的,但它把整個列表視爲一個巨大的按鈕。

你可以將整個監聽器代碼塊從AsyncTask移出到onCreate中,因爲AsyncTask只需要獲取聯繫人列表並更新列表視圖適配器,僅此而已。

+0

if(lc.execute()!= null){list.setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView arg0,View arg1,int arg2,long arg3){AlertDialog.Builder adb = newAlertDialog.Builder(MainActivity.this); adb.setTitle(「Select 「); adb.setPositiveButton(」call「,new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int which){}}); adb.setNegativeButton(」message「,new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which){\t \t \t }});}});這是我在onCreate方法中的對話框代碼,它不給我對話框 – Jeevan

+0

'adb.show();'會顯示一個對話框 –

相關問題