gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:22 25 4
gpt4 key购买 nike

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

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);

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

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();
}
}

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

最佳答案

首先,让我弄清楚...什么时候你想将字符串传递到String句子中?当单击列表项时?

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

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();
}
}
}

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

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

您无法直接从 AdapterView 中取出文本。但事实并非如此。这就是为什么你得到 int 位置参数,来操作输入的数组..:")

关于java - 如何从数组适配器获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31460401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com