如何从AsyncTask到TextSwitcher Java Android使用String []

时间:2014-10-12 22:24:39

标签: java android android-asynctask

我是Android的初学者,所以我向你寻求帮助.. 我想从AsyncTask传递我的String数组并将其用于textSwitcher。我不知道怎么样..但是当我开始时我尝试了这个并向我显示错误。提前感谢您的建议

这是我的MainActivity.java:

public class MainActivity extends Activity {

Button next;
TextSwitcher textSwitcher;

Animation slide_in_left, slide_out_right;

String[] TextToSwitched = null;

int curIndex;

private static String url = "...get_all_words.php";

//JSON Node name
private static final String TAG_WORDS = "words";
private static final String TAG_POLISH = "polish";
private static final String TAG_ENGLISH = "english";

JSONArray words = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ButtonTextSwitcher();

}

private void ButtonTextSwitcher() {

    final String[] tts = new JSONParse().get(TextToSwitched);

    next = (Button) findViewById(R.id.next);
    textSwitcher = (TextSwitcher) findViewById(R.id.textswitcher);

    slide_in_left = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_in_left);
    slide_out_right = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);

    textSwitcher.setInAnimation(slide_in_left);
    textSwitcher.setOutAnimation(slide_out_right);

    textSwitcher.setFactory(new ViewFactory(){

    @Override
    public View makeView() {
        TextView textView = new TextView(MainActivity.this);
        textView.setTextSize(30);
        textView.setTextColor(Color.RED);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setShadowLayer(10, 10, 10, Color.BLACK);

        return textView;
    }});

    curIndex = 0;
    textSwitcher.setText(tts[curIndex]);

    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(curIndex == tts.length-1){
                curIndex = 0;
                textSwitcher.setText(tts[curIndex]);
            }else{
                textSwitcher.setText(tts[++curIndex]);
            }
        }
    });
}

private class JSONParse extends AsyncTask<String, String, String[]> { //AsyncTask<Params, progress, result>

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Pobieranie danych ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String[] doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            words = json.getJSONArray(TAG_WORDS);

            JSONObject c = words.getJSONObject(0);

            String polish = c.getString(TAG_POLISH);
            String english = c.getString(TAG_ENGLISH);

            TextToSwitched = new String[]{polish, english};

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    return get(TextToSwitched);
    }

    public String[] get(String[] textToSwitched) {
        return this.get(textToSwitched);
    }

}

}

0 个答案:

没有答案