我有一个动态列表视图,单击列表项时,该字符串数组已转换为文本到语音功能,我将该字符串数组存储在字符串中,然后只有我将其转换为文本到语音。

我在这里包括了我的代码。

public class TTSList extends ListActivity implements TextToSpeech.OnInitListener {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;

    private GinfyDbAdapter mDbHelper;
    private SimpleCursorAdapter dataAdapter;
    ListView lv;
    public Button btnadd1;
    private TextToSpeech tts;
    String typed1;
    public String praystring;
    public TextView txtText;

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

        lv =(ListView)findViewById(R.id.list);
          mDbHelper = new GinfyDbAdapter(this);
          btnadd1 = (Button)findViewById(R.id.btnadd);
          btnadd1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    createProject();


                }

            });

            mDbHelper.open();
            fillData();
            registerForContextMenu(getListView());

    }

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    private void fillData() {
        mDbHelper.open();
        Cursor projectsCursor = mDbHelper.fetchAllProjects1();
        //startManagingCursor(projectsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_PRAYERNAME,GinfyDbAdapter.CATEGORY_COLUMN_WDATE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1,R.id.date};


         dataAdapter  = new SimpleCursorAdapter(
          this, R.layout.activity_row2,
          projectsCursor,
          from,
          to,
          0);
         setListAdapter(dataAdapter);

        EditText myFilter = (EditText) findViewById(R.id.myFilter);
          myFilter.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {
           }

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start,
             int before, int count) {
               dataAdapter.getFilter().filter(s.toString());
           }
          });

          dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
                 public Cursor runQuery(CharSequence constraint) {
                     return mDbHelper.fetchProjectByName(constraint.toString());
                 }
             });

            tts = new TextToSpeech(this, this);



    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
         MenuInflater mi = getMenuInflater();
            mi.inflate(R.menu.activity_main, menu);
        return true;
    }
     @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
            switch(item.getItemId()) {
            case R.id.menu_insert:
                createProject();
                return true;

            }

            return super.onMenuItemSelected(featureId, item);
        }
     @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            MenuInflater mi = getMenuInflater();
            mi.inflate(R.menu.activity_menu_long, menu);
        }
     @Override
        public boolean onContextItemSelected(MenuItem item) {
            switch(item.getItemId()) {
            case R.id.menu_delete:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteProject1(info.id);
                fillData();
                return true;
            case R.id.menu_edit:
                AdapterContextMenuInfo info1 = (AdapterContextMenuInfo) item.getMenuInfo();
                Intent i = new Intent(this, TTSprayer.class);
                i.putExtra(GinfyDbAdapter.CATEGORY_COLUMN_ID1, info1.id);
                startActivityForResult(i, ACTIVITY_EDIT);
                return true;
            }
            return super.onContextItemSelected(item);
        }
     private void createProject() {
            Intent i = new Intent(this, TTSprayer.class);
            startActivityForResult(i, ACTIVITY_CREATE);
        }


        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);



            Cursor Release = mDbHelper.fetchProject1(id);
            String Releasename = Release.getString(Release.getColumnIndexOrThrow(GinfyDbAdapter.CATEGORY_COLUMN_GODNAME));
            String Releasename1 = Release.getString(Release.getColumnIndexOrThrow(GinfyDbAdapter.CATEGORY_COLUMN_PRAYFOR));
            String Releasename2 = Release.getString(Release.getColumnIndexOrThrow(GinfyDbAdapter.CATEGORY_COLUMN_PRAYEEENAME));


            List<String> godname = new ArrayList<String>(Arrays.asList(Releasename.split(",")));
            List<String> prayfor = new ArrayList<String>(Arrays.asList(Releasename1.split(",")));
            List<String> people = new ArrayList<String>(Arrays.asList(Releasename2.split(",")));



            for (String temp : godname) {
                 for (String temp1 : prayfor) {
                     for (String temp2 : people) {
                         praystring = temp +"  " + temp1 +"   "+ temp2 + " ";

                    }
                }
            }


            typed1 = praystring;

            speakOut();


        }

        @Override
        public void onDestroy() {
            // Don't forget to shutdown tts!
            if (tts != null) {
                tts.stop();
                tts.shutdown();
            }
            super.onDestroy();
        }

        public void onInit(int status) {

            if (status == TextToSpeech.SUCCESS) {

                int result = tts.setLanguage(Locale.US);

                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                } else {
                    //btnaudioprayer.setEnabled(true);
                    speakOut();
                }

            } else {
                Log.e("TTS", "Initilization Failed!");
            }

        }


         private void speakOut() {

                tts.speak(typed1, TextToSpeech.QUEUE_FLUSH, null);
            }




        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            fillData();
        }


在单击列表中的行时,它必须产生文本到语音的转换,但是现在我没有得到任何转换。

最佳答案

看起来您的tts尚未初始化。和你。

onCreate中添加以下内容

   tts = new TextToSpeech(getParent(),TTSList.this);


添加@Override注释

    @Override
    public void onInit(int status)

关于java - 在listitem点击中使用该功能时,文本到语音无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18440636/

10-13 01:20