我的 ListView 是根据用户在非 Activity 类中的 EditText 中输入的内容从 HashMap 动态创建的。我之所以相信它与 setOnItemClickListener 有关,是因为当我在 OnCreate 方法的 Activity 类中手动设置 ListView 值时,它会起作用。 非 Activity 类包含一些方法,例如 AsyncTask 和递归方法。
Activity 类别
public class WordEntry extends AppCompatActivity implements OnClickListener, OnItemClickListener {
//Variable's Declared
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_entry);
/*Code for reading values into HashMap here*/
btn_Resolve = (Button) findViewById(R.id.submit_word);
btn_Resolve.setOnClickListener(this);
//TODO Make list item's clickable.
spin_options = (Spinner) findViewById(R.id.spin_choose_resolver_type);
ArrayAdapter<CharSequence> spin_adapter = ArrayAdapter.createFromResource(this,
R.array.resolver_options, R.layout.simpler_spinner_item);
spin_adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spin_options.setAdapter(spin_adapter);
lv_words = (ListView) findViewById(R.id.lv_word_display);
/*This following two lines were a test to see if manually setting the adapter worked, it did. */
//lv_words.setAdapter(spin_adapter);
//lv_words.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("HelloListView", "You clicked item: " + parent + " at position: " + position);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit_word:
edt_WordEntry = (EditText) findViewById(R.id.word_entry_textbox);
word = edt_WordEntry.getText().toString().toLowerCase();
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(word);
boolean space_found = matcher.find();
View hideKey = this.getCurrentFocus();
if (space_found) {
edt_WordEntry.setText(R.string.no_spaces_allowed);
} else {
ResolveWord fixAnagram = new ResolveWord(word, this,wordHashSet);
ResolveAllWords fixAnagramAll = new ResolveAllWords(word,this,wordHashSet);
/*The following two lines are two other methods that I tried during debugging to try get it working. Separately of course and this debugging was for when the spinner was set to something other than "Anagram's"*/
//fixAnagramAll.lv_words.setOnItemClickListener(new ResolveWord(word,this,wordHashSet));
//lv_words.setOnItemClickListener(new ResolveWord(word,this,wordHashSet));
if(spin_options.getSelectedItem().toString().equals("Anagram;s")) {
hideKeyboard(this);
fixAnagram.execute();
} else {
hideKeyboard(this);
fixAnagramAll.execute();
}
}
break;
default:
break;
}
}
非 Activity 类
public class ResolveWord extends AsyncTask<Void, Integer, ArrayList<String>> implements OnItemClickListener {
/*Variable's Declared*/
public ResolveWord(String letters, Activity wordEntryContext, HashSet<String> passedHashSet) {
/*Variables initiated */
}
/*Some code.....*/
@Override
protected void onPostExecute(ArrayList<String> wordSuccess) {
/*Code which puts data into an arraylist so I can put into the array adadater that follows...*/
ArrayAdapter<String> adapter = new ArrayAdapter<>(thisContext,R.layout.lv_displaywrods_wordentry,
wordSuccess);
//Ways I tried to get the ListView clickable in the non-activity class...
lv_words.setAdapter(adapter);
//lv_words.setOnItemClickListener(new WordEntry());
//lv_words.setOnItemClickListener(this);
/*lv_words.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//I have a break point here to see if the string gets assigned during debugging after clicking a rowitem.
String x = "Test";
}
});*/
//This always returns false
boolean o = lv_words.hasOnClickListeners();
//This always returns true
boolean p = lv_words.isClickable();
}
在上面的代码之上,不久前我还进行了一些编辑,当时我认为我已经解决了这个问题,并在 onCreate 方法的 Activity 类中创建了一个 onItemClickListener
,并使用 为其分配了一个值>getOnItemClickListener
。然后我通过构造函数将其传递给非 Activity 类,但这也无法工作。
最佳答案
在幕后,ArrayAdapter 使用提供给它的 ArrayList(或数组)中每个条目的 toString() 方法,并填充您所使用的构造函数的第二个参数中指定的布局。
如果 wordSuccess 数组列表不为空,则条目到此布局的映射失败。 ArrayAdapter 期望 TextView 或它的派生类作为该构造函数中的第二个参数。您的 lv_displaywords_wordentry 布局文件可能不是一个简单的 TextView,或者它包含一个用某种 ViewGroup 容器包装的 TextView。由于您显示的是简单的列表项,我建议您使用 android.R.layout.simple_list_item_1,如果您的数组列表不为空,您应该会看到显示的单词。
编辑:
正如我所猜测的,从评论中,我了解到您正在尝试传递解析为 ListView 而不是 ArrayAdapter 中的 TextView 的布局。您可以正常使用 ListView,但您的适配器无法将数组列表中的 String 条目映射到 ListView 对象(因为 ListView 代表整个集合,而映射是在一对一的基础上完成的)。这就是为什么您需要传递 TextView (或其自定义子类或其变体)。使用 android 提供的列表项布局,您应该得到一个标准的 TextView。
关于java - 如何在从 Activity 类填充 ListView 的非 Activity 类中正确设置OnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482761/