想到讓對話框顯示的人會如此困難。在C#中,它非常簡單。我一直在尋找幫助,每一個小費似乎給我帶來了麻煩而不是好處。有人可以看看下面的代碼,並告訴我哪裏可能出錯了嗎?未顯示Android對話框
我想要的是非常簡單的。用戶點擊按鈕。然後列表出現。然後用戶點擊列表中的一個項目,出現對話框。 listview事件中onItemClick中的代碼來自其他人。它顯然適用於那個人。我一直點擊,看不到任何東西。我願意提供關於如何解決這個問題的建議,或者一起提供新的代碼。對不起,日誌沒有錯誤信息。
請具體說明我把代碼放在哪裏,因爲我是Android noob。並提前謝謝你!
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()
事件。那部分不起作用。
我是否將此類置於類定義之外?例如,在'removeSpaces'方法之後?然後從'onItemClick'內調用'showAlertDialog'? – Zolt 2012-04-05 16:22:20
如果我明白你想通過把它放在單獨的類中來重用這個功能?如果那樣的話,你可以創建一個名爲MyUtil的類並將其放入它中。您可以通過傳遞參數,通過'onItemClick'中的任何活動來調用它。 – Ravi1187342 2012-04-05 16:43:59
警報消息仍然不顯示。我把你的代碼放在類如下:'public class Util extends Activity {}',然後從'SetPrediction'中的onItemClick'中添加'Util.showAlertDialog(「testTitle」,「testMssage」,predictView.getContext()) ;'但沒有出現。 – Zolt 2012-04-06 17:51:05