0
Androidが新機能です。私はListViewでフィルタモードでSearchViewを実装しました。アイテムをクリックすると、すべてのデータワード、定義、テストを取得する新しいアクティビティが開きます。それはうまく動作します。しかし、リストビューがフィルタリングされ、アイテムをクリックした後、データから間違った位置に新しいアクティビティが表示されます。私はこれを解決する方法を知らない。助けが必要です。フィルター可能なインタフェースを実装することでSearchViewを使用してAndroidがフィルタリストビューの後で間違った位置に移動する
MainActivity
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener {
// Declare Variables
ListView list;
SearchView mSearchView;
ArrayAdapter<String> adapter;
String[] definition;
String[] word;
String[] example;
int[] flag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Generate sample data into string arrays
definition = new String[] { "loira burra",
"arrasa-corações" };
word = new String[] { "bimbo",
"heartthrob" };
example = new String[] { "She's a real bimbo.",
"He's a real heartthrob." };
flag = new int[] { R.drawable.bimbo,
R.drawable.heartthrob };
// Locate the ListView in listview_main.xml
list = (ListView) findViewById(R.id.listview);
mSearchView = (SearchView) findViewById(R.id.search_view);
// Pass results to ListViewAdapter Class
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, word);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
setupSearchView();
// Capture ListView item click
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(MainActivity.this, SingleItemView.class);
// Pass all data definition
i.putExtra("definition", definition);
// Pass all data word
i.putExtra("word", word);
// Pass all data example
i.putExtra("example", example);
// Pass all data flag
i.putExtra("flag", flag);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
list.clearTextFilter();
} else {
list.setFilterText(newText.toString());
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}
}
のListViewアダプタ
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] definition;
String[] word;
String[] example;
int[] flag;
LayoutInflater inflater;
public ListViewAdapter(Context context, String[] definition, String[] word,
String[] example, int[] flag) {
this.context = context;
this.definition = definition;
this.word = word;
this.example = example;
this.flag = flag;
}
@Override
public int getCount() {
return definition.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtdefinition;
TextView txtword;
TextView txtexample;
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Locate the TextViews in listview_item.xml
txtdefinition = (TextView) itemView.findViewById(R.id.definition);
txtword = (TextView) itemView.findViewById(R.id.word);
txtexample = (TextView) itemView.findViewById(R.id.example);
// Locate the ImageView in listview_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set to the TextViews
txtdefinition.setText(definition[position]);
txtword.setText(word[position]);
txtexample.setText(example[position]);
// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);
return itemView;
}
}
SingleItemActivity
public class SingleItemView extends Activity {
// Declare Variables
TextView txtdefinition;
TextView txtword;
TextView txtexample;
ImageView imgflag;
String[] definition;
String[] word;
String[] example;
int[] flag;
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
// Retrieve data from MainActivity on listview item click
Intent i = getIntent();
// Get a single position
position = i.getExtras().getInt("position");
// Get the list of definition
definition = i.getStringArrayExtra("definition");
// Get the list of word
word = i.getStringArrayExtra("word");
// Get the list of example
example = i.getStringArrayExtra("example");
// Get the list of flag
flag = i.getIntArrayExtra("flag");
// Locate the TextViews in singleitemview.xml
txtdefinition = (TextView) findViewById(R.id.definition);
txtword = (TextView) findViewById(R.id.word);
txtexample = (TextView) findViewById(R.id.example);
// Locate the ImageView in singleitemview.xml
imgflag = (ImageView) findViewById(R.id.flag);
// Load the text into the TextViews followed by the position
txtdefinition.setText(definition[position]);
txtword.setText(word[position]);
txtexample.setText(example[position]);
// Load the image into the ImageView followed by the position
imgflag.setImageResource(flag[position]);
}
}