2

データベースから、コンテンツとボタン (テキストから音声へ) 変換を含むリスト ビューにデータを取得しています。私のリストビューでは、そのボタンが正しく表示され、クリック可能な機能も機能しています。ちゃんと。

Mainactivity.java

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

            mDbHelper=new GinfyDbAdapter(MainActivity.this);
            mDbHelper.open();
             Cursor projectsCursor = mDbHelper.fetchAllProjects();
             if(projectsCursor.getCount()>0)
               {
               fillData(projectsCursor);

               Log.i("filling", "...");
               }
               else
               {
                    new GetDataAsyncTask().execute();
               }


            //lv1 =(ListView)findViewById(R.id.list);   
            //lv =(ListView)findViewById(R.id.list);



            btnGetSelected = (Button) findViewById(R.id.btnget);
            btnGetSelected.setOnClickListener(this);

            myFilter = (EditText) findViewById(R.id.myFilter);

    }



        private class GetDataAsyncTask extends AsyncTask<Void, Void, Void> {
            private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);

            protected void onPreExecute() {
                Dialog.setMessage("Loading.....");
                Dialog.show();
            }
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                /*mDbHelper=new GinfyDbAdapter(MainActivity.this); // initialize mDbHelper before.
                mDbHelper.open();
                Cursor projectsCursor = mDbHelper.fetchAllProjects();
                if(projectsCursor.getCount()>0)
                {
                fillData(projectsCursor);
                }*/
                for(int i=0; i<ID.size(); i++){
                    mDbHelper=new GinfyDbAdapter(MainActivity.this);
                     mDbHelper.open();
                     mDbHelper.saveCategoryRecord(new Category(ID.get(i),TITLE.get(i),CONTENT.get(i),COUNT.get(i)));
                     }
                Dialog.dismiss();
            }

            @Override
            protected Void doInBackground(Void... params) {

                getData();
                return null;
            }

        }

        public void getData() {
              try
              {
          HttpClient httpclient = new DefaultHttpClient();
          httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
          HttpGet request = new HttpGet("http://192.168.1.18:3000/api/v1/posts.json");
          // HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/users/mbbangalore/uploads?v=2&alt=jsonc");     

          HttpResponse response = httpclient.execute(request);
          HttpEntity resEntity = response.getEntity();
          String _response=EntityUtils.toString(resEntity); // content will be consume only once
           Log.i("................",_response);
          httpclient.getConnectionManager().shutdown();
          JSONObject jsonObject = new JSONObject(_response);
          JSONArray contacts = jsonObject.getJSONArray("post");//(url);
              for(int i = 0; i < contacts.length(); i++){
                  JSONObject c = contacts.getJSONObject(i);
                  String id = c.getString("id");
                  String title = c.getString("title");
                  String  content = c.getString("content");
                  String  count = c.getString("count");
                  ID.add(id);
                  TITLE.add(title);
                  CONTENT.add(content);
                  COUNT.add(count);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
        }

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

               if(projectsCursor!=null)
               {
               String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT};
               int[] to = new int[]{R.id.text2, R.id.text1, R.id.count};
                dataAdapter  = new SimpleCursorAdapter(
                 this, R.layout.activity_row, 
                 projectsCursor, 
                 from, 
                 to,
                 0);
                setListAdapter(dataAdapter);
                tts = new TextToSpeech(this, this);
                final ListView lv = getListView();
                lv.setTextFilterEnabled(true); 
                lv.setOnItemClickListener(new OnItemClickListener() {
                   public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {                    
                       if(arg1.getId()== R.id.btnaudioprayer && arg1.isClickable() ){


                             btnaudioprayer = (ImageButton) findViewById(R.id.btnaudioprayer);

                                   txtText = (EditText) findViewById(R.id.text1);
                                   Toast.makeText(MainActivity.this,txtText .getText().toString(),Toast.LENGTH_SHORT).show();
                           speakOut();

                        }    
                    }        
                }); 




               }else
               {
                   Log.i("...........","null");
               }

           }

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

        public void onInit1(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() {

            String text = txtText.getText().toString();

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

テキスト読み上げのこのリンクをたどりましたhttp://www.androidhive.info/2012/01/android-text-to-speech-tutorial/ リストビューでそのコードを使用しながら、うまく機能するソースコードをダウンロードします機能が正常に動作していません。

4

0 に答える 0