My ListViewは、非アクティビティクラスのEditTextに入力する内容に応じてHashMapから動的に作成されます。 OnCreateメソッドのActivityクラスでListViewの値を手動で設定したときに動作するため、setOnItemClickListenerを使用していると考えられる理由があります。 非アクティビティクラスには、AsyncTaskや再帰メソッドなどの2つのメソッドが含まれています。私はそれが活動中onItemClickListener
を解決し、作成したと思っていたとき、私はまたすぐ前にいくつかの編集をした上記のコードの上にActivityクラスからListViewを読み込む非アクティビティクラスで正しく設定する方法
活動クラス
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メソッドで定義し、getOnItemClickListener
を使用してこの値を割り当てます。私はそれをコンストラクタを介して非アクティビティクラスに渡しましたが、これも動作しませんでした。
それは本当にハード、あなたが求めているものを理解します。アクティビティクラスとクリックリスナの設定は一切ありません。あなたのコードでは、クリックリスナを設定しません、それはコメントアウトされています。そのようなコメントがなくてもあなたの実際のコードを表示して、わかりやすい方法で質問を策定できますか? – Divers
@Diversコメントアウトされたすべてのコードは、デバッグでテストしたが動作しなかったステップです。私はすでにテストしたことを思い出させるのと同じように、それらを適切な場所に置いていました。アクティビティクラスと非アクティビティクラスの接続は '.execute'メソッドを呼び出して' AsyncTask'プロセスを開始することによって行われます。本当に簡単です - 私は非アクティビティクラスに成功し、onPostExecuteメソッドに到達し、 'onItemClickListener'を設定しようとすると失敗します。編集:それはエラーを出すので失敗しません、それはちょうどクリックに反応しません。 – COYG
@COYHクリックリスナーでクリックイベントが発生しないことを確認しましたか?あなたが 'lv_words.hasOnClickListeners();をチェックしているのは、' ClickListener'があって、絶対に別のものである 'ItemClickListener'ではありません。 – Divers