如何通过setonclicklistener传递变量

时间:2013-12-02 20:02:54

标签: java android arrays json button

概述:map_midvalley_g是主要布局,有8个图像按钮被点击。通过在服务器上获取数据后使用forloop设置图像按钮颜色(绿色或红色)后,单击任何图像按钮将通过调用initiatePopupWindow()启动弹出窗口。 pTitle将出现在弹出窗口中。

问题:如何在单击图像按钮时显示正确的 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();
    }

}
}

1 个答案:

答案 0 :(得分:0)

然后在onClick()方法中使用这个

public void onClick(View v) {
   switch (v.getId()) {
        case R.id.image1:
           // here retrieve array value if you want 0th value or another value
        case R.id.image2:               

        case R.id.image3:               

        }

}

相关问题