我的Android应用程序有问题。它是一个简单的字典应用程序。 搜索工作,但当我点击搜索到的单词时,它会重定向到列表中的第一个项目而不是搜索到的一个。
这是我的MainActivity.java
package xxxx;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private EditText filterText;
private ArrayAdapter<String> listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filterText = (EditText)findViewById(R.id.editText);
ListView itemList = (ListView)findViewById(R.id.listView);
DbBackend dbBackend = new DbBackend(MainActivity.this);
String[] terms = dbBackend.dictionaryWords();
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, terms);
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, DictionaryActivity.class);
intent.putExtra("DICTIONARY_ID", position);
startActivity(intent);
}
});
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MainActivity.this.listAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText" />
</RelativeLayout>
我需要做些什么才能让它发挥作用?
我认为问题出在代码的这一部分,但我无法弄清楚如何修复它。
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, DictionaryActivity.class);
intent.putExtra("DICTIONARY_ID", position);
startActivity(intent);
}
});
由于
@edit
ActivityDictionary.java:
package xxx;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DictionaryActivity extends AppCompatActivity {
private TextView wordMeaning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
int dictionaryId = bundle.getInt("DICTIONARY_ID");
int id = dictionaryId + 1;
TextView word = (TextView)findViewById(R.id.word);
wordMeaning = (TextView)findViewById(R.id.dictionary);
DbBackend dbBackend = new DbBackend(DictionaryActivity.this);
QuizObject allQuizQuestions = dbBackend.getQuizById(id);
word.setText(allQuizQuestions.getWord());
wordMeaning.setText(allQuizQuestions.getDefinition());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dictionary, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(DictionaryActivity.this, InfoActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
您正在传递所点击项目的位置。您需要传递所选项目的ID。
参见代码
String selected = terms[position];
// call method in db class to get the id of the selected word
int dbId = dbBackend.getdictionaryWordId(selected);
Intent intent = new Intent(MainActivity.this, DictionaryActivity.class);
intent.putExtra("DICTIONARY_ID", dbId); // pass the id insted of postion
startActivity(intent);
我不确定DictionaryActivity
中是否需要以下行。如果它不起作用,请删除该行。
int id = dictionaryId + 1;