我有一个selectWord()
函数,该函数填充两个字符串和一个ArrayList
,然后将它们(字符串)放入ListView
和TextView
中。
我想做的是,当有人单击listItem
时,字符串和ArrayList
应该更改其值,并将新值放在TextView
和ListView
中。
我创建了一个函数,该函数从文本文件中选择单词,然后将数据显示在ClickListener
中的视图中;我所做的是再次调用同一函数,以便它从文本文件中选择数据并将其放入视图中。 (测验类型的应用,选择一个选项,然后选择下一个问题)
几天前,我写了类似的代码来工作。
public class MainActivity extends AppCompatActivity {
private ArrayList<String> words = new ArrayList<>(); //words list
private ArrayList<String> defns = new ArrayList<>(); //deffinitions
private String word;
private String correct;
public ArrayList<String> randOptions = new ArrayList<>();
private Random randy = new Random();
private TextView wordView;
private ListView optionView;
public void readFile() { //works fine
//populate the ArrayLists
String word, defn;
Scanner file = new Scanner(getResources().openRawResource(raw.dictionary1));
while(file.hasNextLine()) {
String line = file.nextLine();
String[] lineArray = line.split(" ");
if (lineArray.length >= 2) {
word = lineArray[0];
defn = lineArray[1];
words.add(word);
defns.add(defn);
}
}
}
public void selectWord() {
readFile(); //read file
//get some data
int rand = randy.nextInt(words.size());
this.word = words.get(rand);
this.correct = defns.get(rand);
//make 4 diff options
randOptions.add(correct);
for(int i=0; i<3; i++) {
rand = randy.nextInt(defns.size());
if(randOptions.contains(defns.get(rand)))
i--;
else
randOptions.add(defns.get(rand));
}
Collections.shuffle(randOptions);
//add the data to views
wordView.setText(this.word);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, randOptions);
optionView.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(layout.activity_main);
wordView = findViewById(id.currentWord);
optionView = findViewById(id.options);
selectWord();
optionView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selected = ((TextView) view).getText().toString();
if (correct.equals(selected)) {
Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
}
selectWord(); //so that it changes the vlaues in views but when I add that
//line my hangs there soon as I click on the list item
}
}
);
}
答案 0 :(得分:0)
optionView.setOnItemClickListener(...)
使用AdapterView
。因此,当您从内部调用selectWord();
时,此ClickListener
会挂起... 为什么?:仅仅是因为您正在重新创建{{1 }},然后在ArrayAdapter<String>
中进行设置。
您可以要求ListView
更改其数据,以使ArrayAdapter
仍使用相同的ListView
,而不是要求它杀死自己(即从地面重新创建)。在这种情况下,您应该通知更改,如下所示:
ArrayAdapter
方法中删除ArrayAdapter<String> adapter = ..
和optionView.setAdapter
。selectWord()
并将其设置在ArrayAdapter
方法之外的ListView
中。selectWord()
。selectWord()
,然后重新填充。ArrayAdapter
// create this method to update the options (re-populate the ArrayAdapter)
// of course ArrayAdapter and randOptions should be GLOBAL
public void updateOptions() {
adapter.clear();
if (randOptions != null){
for (String option : randOptions) {
adapter.insert(option,adapter.getCount());
}
}
adapter.notifyDataSetChanged();
}