如何在非活动类中正确设置setOnItemClickListener,该类从活动类填充ListView

时间:2016-07-20 13:31:38

标签: java android

我的ListView是从HashMap动态创建的,具体取决于用户在非活动类中输入EditText的内容。我相信它与setOnItemClickListener有关的原因是因为它在OnCreate方法的Activity类中手动设置ListView值时起作用。 非活动类包含一些方法,例如AsyncTask和递归方法。

活动类

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

}

非活动类

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为其分配了一个值。然后我通过构造函数将它传递给非活动类,但这也无法工作。

4 个答案:

答案 0 :(得分:1)

OnClickListeners中,您可以在onPostExecute方法中设置onPreExecute,在OnClickListener方法中设置doInBackground
您无法在OnClickListener方法中添加lv_words.setOnClickListener(new OnClickListener() { @Override public void onClick() { // Do some coding... } }); 或执行任何UI操作,因为您正在进行网络操作。

所以你可以设置window.onbeforeunload规则:

componentWillUnmount

答案 1 :(得分:1)

在幕后,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。

答案 2 :(得分:0)

您可以尝试为 ResolveWord 设置setter,以便将 WordEntry 对象 - wordEntry传递给 ResolveWord onPostExecute(... )写: lv_words.setOnItemClickListener(wordEntry);

答案 3 :(得分:0)

很难理解你,但我认为你要归档以下内容:

public class ResolveWord extends  AsyncTask<Void, Integer, ArrayList<String>> {
    private Activity wordEntryContext;

   public ResolveWord(String letters, Activity wordEntryContext, HashSet<String> passedHashSet) {
       this.wordEntryContext = wordEntryContext;
       /*Other variables initiated */ 
   } 

   @Override 
   protected void onPostExecute(ArrayList<String> wordSuccess) {
       // your code
       lv_words.setOnItemClickListener((OnItemClickListener)wordEntryContext); 
   }

}