アルファベット順に配列をソートしようとしています(単語列で)。私はこの行をArrays.sort(word, String.CASE_INSENSITIVE_ORDER);
と置いた後、exampleとflagのような他の並列文字列配列はもはや単語位置と一致しません。Androidソート配列
word string
がソートされた後にその位置が変更された場合、他の文字列はそれにリンクして位置を変更する必要があります。
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener {
ListView list;
SearchView mSearchView;
ArrayList<Vocabulary> vocabularylist;
ListViewAdapter 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
word = new String[] { "bimbo",
"heartthrob",
"good-for-nothing"};
definition = new String[] { "loira burra",
"arrasa-corações",
"vagabundo"};
example = new String[] { "She's a real bimbo.",
"He's a real heartthrob.",
"What are you doing wasting time here? Get a job, you good-for-nothing!"};
flag = new int[] { R.drawable.bimbo,
R.drawable.heartthrob,
R.drawable.vagabundo};
**// THE PROBLEM IS HERE**
**Arrays.sort(word, String.CASE_INSENSITIVE_ORDER);**
list = (ListView) findViewById(R.id.listview);
mSearchView = (SearchView) findViewById(R.id.search_view);
vocabularylist = new ArrayList<Vocabulary>();
for (int i = 0; i < word.length; i++) {
Vocabulary vocabulary = new Vocabulary(word[i], definition[i], example[i],
flag[i]);
vocabularylist.add(vocabulary);
}
// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(getApplicationContext(), vocabularylist, word, definition, example, flag);
// 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
Vocabulary voc = (Vocabulary) adapter.getItem(position);
i.putExtra("definition", voc.getDefinition());
// Pass word
i.putExtra("word", voc.getWord());
// Pass example
i.putExtra("example", voc.getExample());
// Pass flag
i.putExtra("flag", voc.getFlag());
// Pass position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setQueryHint("Pesquise aqui...");
}
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
public boolean onQueryTextSubmit(String query) {
return false;
}
}
@Geoger Mulligan、もう一度私を救った!どうもありがとう!! –