ListView hangs when i click on an Item


ListView hangs when i click on an Item



I have a selectWord() function that populates two string and a ArrayList, then it puts them(strings) into ListView and TextView, What I want to do is when someone clicks on the listItem I want the strings and ArrayList to change their values and put new values in TextView and ListView. So I created a function that selects words from text file and then show that data into views, in clickListener what I did is call to the same function again so that it selects data from text file and put it into the views. (a quiz type app, select an option and then next question)



I wrote similar code couple of days ago that work.


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()
{

//read file
readFile();

//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

}
}
);


}





Every time you call selectWord() you're re-initializing the Adapter by doing: ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, randOptions); optionView.setAdapter(adapter); .. this is not the proper way to re-populate the ListView!
– Yahya
26 mins ago



selectWord()


Adapter


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, randOptions); optionView.setAdapter(adapter);


ListView





what should I do then?
– Ahsan Khan AK142
20 mins ago






Wait a moment, I will write an answer
– Yahya
20 mins ago




1 Answer
1



optionView.setOnItemClickListener(...) uses AdapterView. So when you call selectWord(); from inside this ClickListener it hangs... Why?: Simply because you are re-creating the ArrayAdapter<String> and setting it again in the ListView.


optionView.setOnItemClickListener(...)


AdapterView


selectWord();


ClickListener


ArrayAdapter<String>


ListView



Instead of asking it to kill itself (i.e. recreate itself from ground), you can ask the ArrayAdapter to change its data so the ListView will still use the same ArrayAdapter. In this case, you should notify the changes, something like this:


ArrayAdapter


ListView


ArrayAdapter


ArrayAdapter


ListView


selectWord()


selectWord()


ArrayAdapter


public void updateOptions(ArrayAdapter<String> adapter, List randOptions) {

adapter.clear();

if (randOptions != null){
for (String option : randOptions) {
adapter.insert(option,adapter.getCount());
}
}

adapter.notifyDataSetChanged();
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift