台灣最大程式設計社群網站
線上人數
531
 
會員總數:245973
討論主題:189551
歡迎您免費加入會員
討論區列表 >> Java >> 從AZURE上撈到的資料用alertdialog來顯示
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
從AZURE上撈到的資料用alertdialog來顯示
價值 : 20 QP  點閱數:1309 回應數:1
樓主

宗翰先生
門外漢
0 1
34 0
發送站內信

是這樣的,
我寫了一隻程式,是將放在azure上的資料透過JSON格式撈回來到手機端然後用listview方式呈現,
點選其中某一筆資料時用alertdialog的方式呈現出來。

但是我現在困在沒辦法讓被選取的資料在alertdialog上顯示出來,不清楚哪裡設定有誤,希望能發問,看能不能有好的想法。
謝謝。
(因為程式碼很亂,請多忍耐...)


import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.view.View.OnClickListener;
import com.example.MyContact.assistant.AlertDialogManager;
import com.example.MyContact.assistant.ConnectionDetector;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SearchActivity extends ListActivity implements DialogInterface.OnClickListener {
ConnectionDetector cd;

// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParsing jsonParsing = new JSONParsing();

ArrayList<HashMap<String, String>> searchList;

// albums JSONArray
JSONArray search = null;

// albums JSON url
private static final String URL_SEARCH = "http://netiq.azure-mobile.net/api/getsearcheverything";

String message;

// ALL JSON node names
private static final String TAG_ID = "id";
private static final String TAG_DISPLAYNAME = "displayname";
private static final String TAG_DEPT = "Dept";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
addListenerOnButtion();

}

public void addListenerOnButtion () {
ImageButton iButton = (ImageButton) findViewById(R.id.iButton_search);
iButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
cd = new ConnectionDetector(getApplicationContext());
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(SearchActivity.this, "Internet Connection Error", "Please connect to working Internet connection", false);
return;
}
// Hashmap for ListView
searchList = new ArrayList<HashMap<String, String>>();

EditText term = (EditText) findViewById(R.id.text_name);
message = term.getText().toString();

/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
new LoadResults().execute();
}
} );
ListView lv = getListView();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
new AlertDialog.Builder(SearchActivity.this)
.setIcon(android.R.drawable.btn_star_big_on)
.setTitle("搜尋的結果如下: ")
.setMessage("姓名: " + getText(R.id.member_name).toString() + "\n" +
"部門: " + getText(R.id.member_dept).toString() + "\n" +
"分機: " + getText(R.id.member_ext).toString() + "\n" +
"信箱: " + getText(R.id.member_mail).toString())
.setNegativeButton("電話",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent_call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getText(R.id.member_ext).toString()));
startActivity(intent_call);
}
}
)

.setNeutralButton ("郵件",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent_mail = new Intent (Intent.ACTION_SENDTO, Uri.parse("mailto:"+ getText(R.id.member_mail).toString()));
startActivity(intent_mail);
}
})

.setPositiveButton ("返回",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})

.show();
}
});
}

@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
//dialog.dismiss();
}
else if (which == DialogInterface.BUTTON_NEUTRAL) {
//Intent intent_call = new Intent (Intent.ACTION_CALL, Uri.parse("tel:"+ TAG_EXT));
// startActivity(intent_call);
}
else if (which == DialogInterface.BUTTON_NEGATIVE) {
// Intent intent_mail = new Intent (Intent.ACTION_SENDTO, Uri.parse("mailto:"+ TAG_MAIL));
// startActivity(intent_mail);
}
}

/**
* Background Async Task to Load all Albums by making http request
* */
class LoadResults extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SearchActivity.this);
pDialog.setMessage("搜尋中...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

/**
* getting Albums JSON
* */
protected String doInBackground(String... args) {



// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();

// post album id as GET parameter
params.add(new BasicNameValuePair(TAG_DISPLAYNAME, message));

// getting JSON string from URL
String json = jsonParsing.makeHttpRequest(URL_SEARCH, "GET",
params);

// Check your log cat for JSON reponse
Log.d("Track List JSON: ", json);
try {
search = new JSONArray(json);

if (search != null) {
// looping through All albums
for (int i = 0; i < search.length(); i++) {
JSONObject c = search.getJSONObject(i);

// Storing each json item values in variable
String id = c.getString(TAG_ID);
String displayname = c.getString(TAG_DISPLAYNAME);
String Dept = c.getString(TAG_DEPT);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_DISPLAYNAME, displayname);
map.put(TAG_DEPT, Dept);


// adding HashList to ArrayList
searchList.add(map);
}
}else{
Log.d("Albums: ", "null");
}

} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {

// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SearchActivity.this, searchList,
R.layout.result_page_item, new String[] { TAG_ID,
TAG_DISPLAYNAME, TAG_DEPT}, new int[] {
R.id.member_id, R.id.member_name, R.id.member_dept});

// updating listview
setListAdapter(adapter);

}
});
}

}
}

</pre>

搜尋相關Tags的文章: [ alert dialog ] , [ java ] ,
本篇文章發表於2014-04-09 14:57
1樓
回應

CCY
檢舉此回應
SearchActivity.LoadResults.onPostExecute 方法裡需要把資料(searchList?) 放進新產生的 adapter 裡

onItemClick 那個 method 的第三個參數 arg2 是選取的項目 position
可以用 getListAdapter() 拿回 adapter 參考然後 adapter.getItem(arg2) 拿到選項的物件

試看看
本篇文章回覆於2014-08-16 02:12
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.