2012-04-05 4 views
2

표시 할 대화 상자가 점점 어려워 질 것이라고 생각하는 사용자는 누구입니까? C#에서는 너무 쉽습니다. 나는 도움을 줄 모든 줄을 훑어 보았다. 그리고 모든 팁은 나보다 좋은 말썽을 일으키는 것처럼 보인다. 누군가가 아래 코드를보고 내가 잘못했을 수도있는 곳을 말해 줄 수 있습니까?Android 대화 상자가 표시되지 않습니다.

내가 원하는 것은 매우 간단합니다. 사용자가 버튼을 클릭합니다. 그런 다음 목록이 나타납니다. 그런 다음 사용자가 목록에있는 항목 중 하나를 클릭하면 대화 상자가 나타납니다. listview 이벤트의 onItemClick 코드는 다른 사람의 코드입니다. 그것은 분명히 그 사람을 위해 일합니다. 나는 계속해서 아무 것도 보지 않고 보았다. 나는 그것을 수정하는 방법에 대한 제안이나 새로운 코드를 함께 공개한다. 죄송합니다. 로그에 오류 메시지가 없습니다.

안드로이드 놈이므로 코드를 어디에 두어야하는지 구체적으로 설명하십시오. 그리고 미리 감사드립니다!

public class SetPrediction extends Activity 
{ 
    Button btnInsrt, btnFTeam, btnSTeam; 
    ArrayList<NameValuePair> nameValuePairs; 
    TextView txtGameTeams; 
    Bundle recdData; 
    String game; 

    JSONArray jArray; 
    String result; 
    InputStream is; 
    StringBuilder sb; 

    ArrayList<String> fNames; 
    ListView listView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.set_prediction); 

     nameValuePairs = new ArrayList<NameValuePair>(); 
     txtGameTeams = (TextView) findViewById(R.id.txtGameTeams); 

     recdData = getIntent().getExtras(); 
     game = recdData.getString("xxxx.xxxx.xxx"); 
     txtGameTeams.setText("xxxxxxx: " + game + " game."); 

     btnInsrt = (Button) findViewById(R.id.btnInsert); 
     btnFTeam = (Button) findViewById(R.id.btnFirstTeam); 
     btnSTeam = (Button) findViewById(R.id.btnSecondTeam); 

     btnFTeam.setText(removeSpaces(game.split("vs")[0])); 
     btnSTeam.setText(removeSpaces(game.split("vs")[1])); 

     btnSTeam.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       String result = ""; 
       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("players", removeSpaces(game.split("vs")[1]))); 

       fNames = new ArrayList<String>(); 
       listView = (ListView) findViewById(R.id.lstPlayerForPrediction); 

       //http post 
       try 
       { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://xxxxx/getTeamPlayers.php"); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        InputStream is = entity.getContent(); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        is.close(); 

        result=sb.toString(); 
       } 
       catch(Exception e) 
       { 
        Log.e("log_tag", "Error in http connection " + e.toString()); 
       } 

       //parse json data 
       try 
       { 
        JSONArray jArray = new JSONArray(result); 
        for(int i=0;i<jArray.length();i++) 
         fNames.add(jArray.getJSONObject(i).getString("First_Name")); 

       } 
       catch(JSONException e) 
       { 
        Log.e("log_tag", "Error parsing data " + e.toString()); 
       } 

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(SetPrediction.this, android.R.layout.simple_list_item_1, fNames); 
       listView.setAdapter(adapter); 

       listView.setOnItemClickListener(new OnItemClickListener() 
       { 
        public void onItemClick(AdapterView<?> arg0, View predictView, int item, long arg3) 
        { 
         final CharSequence[] items = {"Online", "Away", "Do not distrub","Invisible","Offline"}; 
         AlertDialog.Builder builder = new AlertDialog.Builder(SetPrediction.this); 


         builder.setTitle("Change Status"); 
         builder.setItems(items, new DialogInterface.OnClickListener() 
         { 
          public void onClick(DialogInterface dialog, int item) 
          { 
           Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
          } 
         }); 

         AlertDialog alert = builder.create(); 
         alert.show(); 
        } 
       }); 
      } 
     }); 
    } 


    public String removeSpaces(String s) 
    { 
     StringTokenizer st = new StringTokenizer(s," ",false); 
     String t=""; 
     while (st.hasMoreElements()) t += st.nextElement(); 
     return t; 
    } 

문제는 listView.setOnItemClickListener(new OnItemClickListener() 이벤트에 있습니다. 그 부분은 작동하지 않습니다.

답변

1

확인 :

public void showAlertDialog(String title, String message, Context context) 
{ 
    final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

    alertDialog.setTitle(title); 
    alertDialog.setMessage(message); 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      alertDialog.dismiss(); 
     } 
    }); 

    alertDialog.show(); 
} 
+0

이것을 클래스 정의 외부에 넣으시겠습니까? 예를 들어,'removeSpaces' 메소드 다음에? 그리고'onItemClick'에서'showAlertDialog'를 호출합니까? – Zolt

+0

이 기능을 별도의 클래스에 넣어 재사용하고 싶다고 생각한다면? 그렇다면'MyUtil'이라는 클래스를 만들어 내부에 넣을 수 있습니다. 매개 변수를 전달하여'onItemClick' 내의 모든 액티비티로부터 호출 할 수 있습니다. – Ravi1187342

+0

경고 메시지가 여전히 표시되지 않습니다. 나는 당신의 코드를'public class Util extends Activity {}'와 같이'SetPrediction'의'onItemClick' 내에서 클래스에 넣었습니다.'Util.showAlertDialog ("testTitle", "testMssage", predictView.getContext())를 추가했습니다. 그러나 아무 것도 나타나지 않는다. – Zolt

0

코드 단지 사소한 변화 :::

AlertDialog.Builder builder = new AlertDialog.Builder(SetPrediction.this).create(); 
builder.setTitle("Change Status"); 
builder.setItems(items, new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int item){ 
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
} 
}); 
builder.show(); 

은 또한 다음과 같은 :::

수입 android.app.AlertDialog를 가져올 대신에이 코드를 사용해보십시오; import android.content.DialogInterface; 이 밖에

+0

이 구축되지 않습니다. 3 행에서는 다음과 같은 오류가 발생합니다.'AlertDialog 유형에 대해 setItems (CharSequence [CharSequence], new DialogInterface.OnClickListener() {}) 메서드가 정의되지 않았습니다. ' – Zolt

+0

수입을 추가하십시오. –

관련 문제