如何通过单击一次使TTS读取所有Listview项目?

时间:2014-09-14 07:40:17

标签: android android-listview text-to-speech

我在Android中有一个Listview,当你点击它读取的项目时可以被TTS读取,但我的问题是如何通过单击列表视图中的任何项目使TTS读取所有项目?

这是代码

 package learn2crack.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;



import learn2crack.listview.library.JSONParser;



public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
ListView list;
TextView html;
Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=New%20York,NY&sensor=false"; //url is just a sample

//JSON Node Names 
private static final String TAG_OS = "routes";
private static final String TAG_HTM = "html_instructions";
private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;

JSONArray android = null;

 public void onInit(int initStatus) {

        // check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    oslist = new ArrayList<HashMap<String, String>>();



    Btngetdata = (Button)findViewById(R.id.getdata);
    Btngetdata.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
             new JSONParse().execute();


        }
    });


}


private class JSONParse extends AsyncTask<String, String, JSONObject> {
     private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         html = (TextView) findViewById(R.id.html_instruction);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();



    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_OS);
                for (int x = 0; x < android.length(); x++) {
                    JSONArray legs = android.getJSONObject(x).getJSONArray("legs");
                    JSONObject distance = legs.getJSONObject(0).getJSONObject("distance");
                    JSONObject duration = legs.getJSONObject(0).getJSONObject("duration");
                    JSONArray steps = legs.getJSONObject(0).getJSONArray("steps");

                    for (int j = 0; j < steps.length(); j++) {
                            String html_instructions = steps.getJSONObject(j).getString("html_instructions");
                            String s = html_instructions.replaceAll("<(\"[^\"]*\"|'[^']*'|[^'\">])*>", " ");




                //for(int i = 0; i < android.length(); i++){
                //JSONObject c = android.getJSONObject(i);

                // Storing  JSON item in a Variable
                //String ver = c.getString(TAG_VER);
                //String name = c.getString(TAG_NAME);
                //String api = c.getString(TAG_API);
                //String htm = c.getString("html_instructions");



                // Adding value HashMap key => value


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


                map.put(TAG_HTM, s);

                oslist.add(map);
                list=(ListView)findViewById(R.id.list);





                final ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] {TAG_HTM}, new int [] {R.id.html_instruction});

                list.setAdapter(adapter);
                Intent checkTTSIntent = new Intent();
                checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
                startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {



                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        //Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("html"), Toast.LENGTH_SHORT).show();
                        parent.getAdapter();
                        //adapter.getItem(position).toString();
                        String words = adapter.getItem(position).toString();
                       //  String words = html.getText().toString();
                        speakWords(words);

                    }
                    private void speakWords(String speech) {
                        //speak straight away
                        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                }

                });


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



     }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            //the user has the necessary data - create the TTS
        myTTS = new TextToSpeech(this,(OnInitListener) this);
        }
        else {
                //no data - install it now
            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
        }
    }
}

}

0 个答案:

没有答案