当我单击某个项目时,列表视图挂起



我有一个填充两个字符串和一个ArrayListselectWord()函数,然后将它们(字符串(放入ListViewTextView

我想做的是当有人点击listItem时,字符串和ArrayList应该更改它们的值并将新值放入TextViewListView中。

我创建了一个函数,该函数从文本文件中选择单词,然后将该数据显示到视图中,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
}
}
);    
}

optionView.setOnItemClickListener(...)使用AdapterView.因此,当您从此ClickListener内部呼叫selectWord();时,它会挂起...为什么?:仅仅因为您正在重新创建ArrayAdapter<String>并在ListView中再次设置它。

与其要求它杀死自己(即从地面重新创建自己(,不如要求ArrayAdapter更改其数据,以便ListView仍然使用相同的ArrayAdapter。在这种情况下,您应该通知更改,如下所示:

  1. 首先从方法中删除ArrayAdapter<String> adapter = ..optionView.setAdapterselectWord()
  2. 创建全局ArrayAdapter并将其设置在selectWord()方法之外的ListView中。
  3. 然后每次您想更改选项时.等调用selectWord().
  4. 然后清除ArrayAdapter并再次重新填充它。
  5. 最后通知更改。

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

// first declare the ArrayAdapter globally as a field
ArrayAdapter<String> adapter;
// inside onCreate() method, initialize the ArrayAdapter (i.e. outside the `selectWord()` method) 
// using this constructor: ArrayAdapter(Context context, int resource)
adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
// after that, set the adapter (it's empty by now)
optionView.setAdapter(adapter);
// fill the Random Options Array
selectWord();
// call update options
updateOptions();
// and use it inside the CLickListener like this
optionView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// whatever ...
selectWord(); //so that it changes the options in the array
updateOptions(); // update options in the same ArrayAdapter
})});

最新更新