2012-12-04 53 views
3

在我的android應用程序中,當我點擊文本視圖時,我想顯示一個包含項目列表的警告對話框。這怎麼可能。請親切指導。具有項目列表的提醒對話框

我的代碼它作爲:

cus_name_txt = (TextView)findViewById(R.id.cus_name_txta); 
    cus_name_txt.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Onclick_click1(cus_name_txt); 
      // TODO Auto-generated method stub 

     } 
    }); 

    contact_no_txt = (TextView)findViewById(R.id.contact_no_txta); 


    attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txt); 
    attend_by_txtbx.setText(My_Task.attend_by_txt); 


    ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txta); 


    task_detail_txt = (TextView)findViewById(R.id.task_detail_txt); 

我如何可以通過單擊的TextView獲取項目列表的警告框。請指導。我會感謝到u

回答

1

如果你想顯示進度的提示對話框中列出的裝載前,再使用的AsyncTask了點。

例如爲:

private class LoadingTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected void onPreExecute(){ 
       super.onPreExecute(); 
       progressDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... str) { 
      String response = ""; 
        // Call Web Service here and return response 

      response = API.getDealsByCategory(str[0], str[1]); 
        // e.g.: above is my WebService Function which returns response in string 
      return response; 
     } 
     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      System.out.println("result is: "+result); 


      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        progressDialog.dismiss(); 
       } 
      }).start(); 

     // SHOW THE ALERT DIALOG HERE..... 
     } 
    } 

呼叫的AsyncTask如象下面這樣:

LoadingTask任務=新LoadingTask(); task.execute(「YOUR_PARAMETER」,「YOUR_PARAMETER」);

// ==============================

只要把下面的代碼中的後Excution AsyncTask,你會得到你想要的。

final CharSequence[] items = {"","50","100","150","200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","1000"}; 
     AlertDialog.Builder builder = new AlertDialog.Builder(getParent()); 
     builder.setTitle("Select Country"); 
     //builder.setI 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       //Toast.makeText(getApplicationContext(), con.get(item).getCountrName(), Toast.LENGTH_SHORT).show(); 
       selectDistanceTV.setText(items[item]); 
       System.out.println("Item is: "+items[item]); 
       /*CONTRY_ID = con.get(item).getCountryId(); 
       stateET.requestFocus();*/ 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 

希望它能幫助你。

如果您需要關於如何使用的AsyncTask更多的幫助,然後看到這裏:Vogella

評論我要任何查詢。

享受編碼... :)

+0

非常感謝:)它工作:)我想添加一個更新按鈕,可以更新列表。這怎麼可能?? – Mona

+0

表示你想要列表中的按鈕? –

+0

評論我的任何查詢和感謝接受答案。 –

0

認沽下面的代碼在textViewonClick

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Select Color Mode"); 

ListView modeList = new ListView(this); 
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" }; 
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray); 
modeList.setAdapter(modeAdapter); 

builder.setView(modeList); 
final Dialog dialog = builder.create(); 

dialog.show(); 

或者

Dialog dialog = new Dialog(**Your Context**); 
    dialog.setContentView(R.layout.**Your Layout File**); 
    dialog.show(); 

在這個佈局文件,你可以讓你的佈局按照您的要求。當你想使用的ListView從你的對話框佈局文件,那麼你必須寫

ListView listView = (ListView)**dialog**.findViewById(R.id.**Your ListView Id**) 
+0

謝謝Shirkant – Mona

+0

歡迎您:) – Shrikant

0

您可以通過產品清單中的字符串數組和AlertBox顯示它..

例如:

private void SingleChoice() { 

字符串[] selectFruit =新的String [] { 「蘋果」, 「橙」, 「芒果」};

Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Single your Choice"); 
builder.setItems(selectFruit, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    Toast.makeText(MainActivity.this, 
     selectFruit[which] + " Selected", Toast.LENGTH_LONG) 
     .show(); 
    dialog.dismiss(); 
    } 
}); 
builder.setNegativeButton("cancel", 
    new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     dialog.dismiss(); 
     } 
    }); 
AlertDialog alert = builder.create(); 
alert.show(); 

}

+0

謝謝您的幫助:) – Mona

0
button.setOnClickListener(new OnClickListener() 
    { 

     public void onClick(View arg0) 
     { 
      new AlertDialog.Builder(MainActivity.this) 

      .setSingleChoiceItems(arrClientName,0, new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int which) 
       { 
        name.setText(arrClientName[which]); 


       } 
      }) 
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
      { 
         public void onClick(DialogInterface dialog, int id) 
         { 

         } 
      }) 
      .show(); 

     } 
    }); 
+0

哪裏是顯示ListView控件的代碼? – Shrikant

+0

此對話框包含項目列表..arrClientName是一個字符串數組顯示點擊textview .. –