【发布时间】:2013-12-02 20:02:53
【问题描述】:
概述:map_midvalley_g 是主要布局,有 8 个可单击的图像按钮。在从服务器获取的数据上使用forloop设置图像按钮颜色(绿色或红色)后,单击任何图像按钮将通过调用initialPopupWindow()来启动弹出窗口。 pTitle 将显示在弹出窗口中。
问题:当点击 imagebutton 时,我怎样才能得到正确的 LotID 显示。比如imagebutton1应该显示LOT1。
所有变量都已全局声明,因此它们中的每一个都可以在所有类中使用。 提前致谢。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_midvalley_g);//this is the layout where imagebuttons will be showing, there are 8 imagebuttons.
new loadAllIndicators().execute();
}
/*this is the asynctask class*/
class loadAllIndicators extends AsyncTask<String, String, String> {
protected void onPreExecute() {
...
}
/**
* doInBackground() below is just get data from server nad
* */
protected String doInBackground(String... args) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost(url_all_indicotors);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (ConnectException e) {
...
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
System.out.println(result);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Cannot retrieve results",
Toast.LENGTH_SHORT).show();
}
return null;
}
/**
* onPostExecute() setting all data from server into layout to show
* */
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
try {
jArray = new JSONArray(result);
if (jArray == null) {
Toast.makeText(getBaseContext(), "jArray IS EMTPY",
Toast.LENGTH_SHORT).show();
}
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
LotID = json_data.getString("LotID");//this is the value i gonna display into pTitle accordingly later when imagebuttons is clicked
pStatus = json_data.getInt("pStatus");
indicator_pending = (ImageButton) findViewById(buttonIDs[i]);
if (pStatus == 1)//set the 8 imagebuttons to either red or green according to pStatus
{
indicator_pending
.setImageResource(R.drawable.indicator_red);
indicator_pending
.setOnClickListener(getOnClickDoSomething(indicator_pending));
} else {
indicator_pending
.setImageResource(R.drawable.indicator_green);
indicator_pending
.setOnClickListener(getOnClickDoSomething(indicator_pending));
}
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "FAILED",
Toast.LENGTH_SHORT).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
});
}
View.OnClickListener getOnClickDoSomething(
final ImageButton indicator_pending) {
return new View.OnClickListener() {
public void onClick(View v) {initiatePopupWindow();}
/**
when any of the imagebuttons get clicked will call initiatePopupWindow() and show pTitle
**/
public void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) MapActivityMidValley.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popwindow,
(ViewGroup) findViewById(R.id.popup_element));
lock_status = (TextView) layout.findViewById(R.id.lock_status);
pTitle = (TextView) layout.findViewById(R.id.popupTitle);
pTitle.setText(LotID);//set this variable accordingly to the button clicked.
} catch (Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
请清楚地问这个问题..您在哪里加载图像...
-
@Bhr 我已经编辑了代码区域,添加了一些详细说明。如果这令人困惑,请原谅糟糕的结构。
-
JSON 数组中有多少个值..
-
@Bhr 有 8 个,将循环到 8 个图像按钮。
标签: java android arrays json button