获取变量形式onItemListener并在另一个函数中使用它

时间:2016-03-15 00:05:13

标签: java android-studio

private class BackTask extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd.setTitle("Retrieving data");
        pd.setMessage("Please wait.");
        pd.setCancelable(true);
        pd.setIndeterminate(true);
        pd.show();
    }

    protected Void doInBackground(Void... params) {
        InputStream is = null;
        String result = "";
        try {
            httpclient = new DefaultHttpClient();
            // i want to use httppost in this ligne
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            // Get our response as a String.
            is = entity.getContent();
        } catch (Exception e) {
            if (pd != null)
                pd.dismiss(); // close the dialog if error occurs
            Log.e("ERROR", e.getMessage());
        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 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("ERROR", "Error converting result " + e.toString());
        }
        // parse json data
        try {
            result = result.substring(result.indexOf("["));
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Prog2 p = new Prog2();
                p.setTitre_emission(json_data.getString("titre_emission"));
                p.setDesc_emission(json_data.getString("desc_emission"));
                p.setHeure_emission(json_data.getString("heure_emission"));
                p.setChaine_emission(json_data.getString("titre_chaine"));
                records.add(p);
            }
        }
        catch (Exception e) {
            Log.e("ERROR", "Error pasting data " + e.toString());
        }
        return null;
    }

    protected void onPostExecute(Void result) {
        if (pd != null)
            pd.dismiss(); // close dialog
        Log.e("size", records.size() + "");
        adapter.notifyDataSetChanged(); // notify the ListView to get new
                                        // records
    }
}

如何从onItemListener获取文字并在其他功能中使用它来获取httpost?
当我使用getText()时出现错误

public class Wataneya1Activity extends AppCompatActivity {
    Toolbar mToolbar;
    Spinner mSpinner;
    String text;
    Activity context;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    ProgressDialog pd;
    CustomAdapter1 adapter;
    ListView listProg;
    ArrayList<Prog1> records;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wataneya1);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ImageButton mButton = (ImageButton) findViewById(R.id.Button03);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Wataneya1Activity.this.finish();
            }
        });
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        final ActionBar actionBar = getSupportActionBar();
        mSpinner = (Spinner) findViewById(R.id.spinner_rss);
        String[] items = getResources().getStringArray(R.array.days_array);
        List<String> spinnerItems = new ArrayList<>();
        for (int i = 0; i < items.length; i++) {
            spinnerItems.add(items[i]);
        }
        SpinnerAdapter adapter1 = new SpinnerAdapter(actionBar.getThemedContext(), spinnerItems);
        mSpinner.setAdapter(adapter1);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
                text = mSpinner.getSelectedItem().toString();
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
        context = this;
        records = new ArrayList<Prog1>();
        listProg = (ListView) findViewById(R.id.prog_list);
        adapter = new CustomAdapter1(context, R.layout.list_item, R.id.titre_emission, records);
        listProg.setAdapter(adapter);
    }

    public HttpPost fnt (String text) {
            if (text.equals("Mardi")) {
                httppost = new HttpPost("http://192.168.:8080/TuniTV/prog_wataneya1_mardi.php");
            } else if (text.equals("Mercredi")) {
                httppost = new HttpPost("http://192.168:8080/TuniTV/prog_wataneya1_mercredi.php");
            } else if (text.equals("Jeudi")) {
                httppost = new HttpPost("http://192.168/TuniTV/prog_wataneya1_jeudi.php");
            } else if (text.equals("Vendredi")) {
                httppost = new HttpPost("http://192.168:8080/TuniTV/prog_wataneya1_vendredi.php");
            } else if (text.equals("Samedi")) {
                httppost = new HttpPost("http://192.168:8080/TuniTV/prog_wataneya1_samedi.php");
            } else if (text.equals("Dimanche")) {
                httppost = new HttpPost("http://192.168:8080/TuniTV/prog_wataneya1_dimanche.php");
            } else {
                httppost = new HttpPost("http://192.168:8080/TuniTV/prog_wataneya1_lundi.php");
            }

            return  httppost;
        }

1 个答案:

答案 0 :(得分:0)

您会在回调方法中收到点击位置(==索引数据)。用它来查找数据数组中的数据。

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {

            text = spinnerItems.get(arg3);

            Toast.makeText(getApplicationContext(), text,
                    Toast.LENGTH_SHORT).show();
        }
是的...告诉我们有一个错误 - 是废话。 告诉我们它是什么样的错误,甚至显示一些堆栈跟踪?好东西。