如何从数组适配器获取值?

时间:2022-06-18 13:32:06

I have some text from voice recognition.

我有一些来自语音识别的文字。

if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
    match_text_dialog = new Dialog(MainActivity.this);
        match_text_dialog.setContentView(R.layout.activity_dialog_matches_frag);
    textlist = (ListView)match_text_dialog.findViewById(R.id.list);
    matches_text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, matches_text);
    textlist.setAdapter(adapter);
    textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            Speech.setText("You have said " +matches_text.get(position));
            match_text_dialog.hide();
        }
    });
    match_text_dialog.show();
}
super.onActivityResult(requestCode, resultCode, data);

Then i developed a code for get some specific text.

然后我开发了一个代码来获取一些特定的文本。

String sentence = stop.getText().toString();
String[] splitWords = sentence.split(" ");
String[] stopWords = {"I", "want", "to", "go", "to", "the"};

for (int i = 0; i < stopWords.length; i++) {
    for (int j = 0; j < splitWords.length; j++) {
        if (stopWords[i].equalsIgnoreCase(splitWords[j])) {
            splitWords[j] = null;
        }
    }
}

for (int i = 0; i < splitWords.length; i++) {
    if(splitWords[i]!= null) {
        Toast.makeText(getApplicationContext(),splitWords[i] , Toast.LENGTH_LONG).show();
    }
}

What i want to do is get ArrayAdapter values and pass it to my second code. How do i get values and pass it to String sentence ??

我想要做的是获取ArrayAdapter值并将其传递给我的第二个代码。我如何获取值并将其传递给String语句?

1 个解决方案

#1


0  

First of all, let me get this straight... when do you want to pass the string into String sentence? When the list item is clicked?

首先,让我直截了当...你什么时候想把字符串传递给字符串句子?单击列表项时?

If so, make a method, put the 2nd bit of the code inside the method, and take in the string as an argument... Simple really..

如果是这样,创建一个方法,将代码的第二位放在方法中,并将字符串作为参数...简单真的..

public void goToMethod(String sentence) {
         //put the String sentence code here
         String sentence;
         this.sentence = sentence;

         String[] splitWords = sentence.split(" ");
         String[] stopWords = {"I", "want", "to", "go", "to", "the"};

         for (int i = 0; i < stopWords.length; i++) {
              for (int j = 0; j < splitWords.length; j++) {
                   if (stopWords[i].equalsIgnoreCase(splitWords[j])) {
                           splitWords[j] = null;
                   }
               }
         }

         for (int i = 0; i < splitWords.length; i++) {
             if(splitWords[i]!= null) {
                   Toast.makeText(getApplicationContext(),splitWords[i], Toast.LENGTH_LONG).show();
             }
         }
}

And then, from the onItemClick() method in the Activity, call this method that you created.

然后,从Activity中的onItemClick()方法调用您创建的此方法。

public void onItemClick(....) {
        Speech.setText("You have said " +matches_text.get(position));
        match_text_dialog.hide();
        goToMethod(matches_text.get(position));
}

you cannot take text directly out from AdapterView. It doesnt work that way. That is why you get the int position argument, to manipulate the array that was fed in.. :")

你不能直接从AdapterView中取出文本。它不会那样工作。这就是为什么你得到int position参数,来操纵输入的数组..:“)

#1


0  

First of all, let me get this straight... when do you want to pass the string into String sentence? When the list item is clicked?

首先,让我直截了当...你什么时候想把字符串传递给字符串句子?单击列表项时?

If so, make a method, put the 2nd bit of the code inside the method, and take in the string as an argument... Simple really..

如果是这样,创建一个方法,将代码的第二位放在方法中,并将字符串作为参数...简单真的..

public void goToMethod(String sentence) {
         //put the String sentence code here
         String sentence;
         this.sentence = sentence;

         String[] splitWords = sentence.split(" ");
         String[] stopWords = {"I", "want", "to", "go", "to", "the"};

         for (int i = 0; i < stopWords.length; i++) {
              for (int j = 0; j < splitWords.length; j++) {
                   if (stopWords[i].equalsIgnoreCase(splitWords[j])) {
                           splitWords[j] = null;
                   }
               }
         }

         for (int i = 0; i < splitWords.length; i++) {
             if(splitWords[i]!= null) {
                   Toast.makeText(getApplicationContext(),splitWords[i], Toast.LENGTH_LONG).show();
             }
         }
}

And then, from the onItemClick() method in the Activity, call this method that you created.

然后,从Activity中的onItemClick()方法调用您创建的此方法。

public void onItemClick(....) {
        Speech.setText("You have said " +matches_text.get(position));
        match_text_dialog.hide();
        goToMethod(matches_text.get(position));
}

you cannot take text directly out from AdapterView. It doesnt work that way. That is why you get the int position argument, to manipulate the array that was fed in.. :")

你不能直接从AdapterView中取出文本。它不会那样工作。这就是为什么你得到int position参数,来操纵输入的数组..:“)