我的Listview工作正常,但我正在使用SearchView搜索列表视图中的项目。当我最初点击原始列表视图中的某个项目时,它会显示与所单击项目相关的对话框,但在搜索完成后,显示的对话框是错误的。更具体地说,它显示该位置的原始列表视图项的对话框。请帮忙
public class ViewExpense extends ActionBarActivity implements AdapterView.OnItemClickListener,
SearchView.OnQueryTextListener {
ExpenseManager expenseManager;
ExpenseCategoriesManager expenseCategoriesManager;
CategoriesModel categoriesModel;
CurrencyManager currencyManager;
ArrayList<Expense> allExpenses;
ArrayList<String> allTitles;
String titlePosition;
ArrayAdapter<String> adapter;
ListView lv_View_expense;
Dialog dialog;
Expense expense;
private ViewStub idEmpty;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_expense);
// Including back button to ACTION BAR
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
init();
allTitles = new ArrayList<String>();
allExpenses = expenseManager.getAllExpenses();
// Getting all the titles
for (Expense expense : allExpenses) {
allTitles.add(expense.getTitle());
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allTitles);
lv_View_expense.setAdapter(adapter);
lv_View_expense.setTextFilterEnabled(true);
if (allTitles.isEmpty()) {
idEmpty.setVisibility(View.INVISIBLE);
Toast.makeText(ViewExpense.this, "Sorry - Add An Expense First", Toast.LENGTH_LONG).show();
} else {
idEmpty.setVisibility(View.VISIBLE);
}
}
private void init() {
lv_View_expense = (ListView) findViewById(R.id.lv_view_expense);
expenseManager = new ExpenseManager(this);
expenseCategoriesManager = new ExpenseCategoriesManager(this);
lv_View_expense.setOnItemClickListener(this);
currencyManager = new CurrencyManager(this);
categoriesModel = new CategoriesModel();
idEmpty = (ViewStub) findViewById(R.id.idEmpty);
idEmpty.inflate();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
titlePosition = allTitles.get(position);
expense = expenseManager.getExpense(titlePosition);
dialog = new Dialog(ViewExpense.this);
dialog.setContentView(R.layout.expense_view_details);
TextView tvTime, tvTitle, tvAmount, tvCategory, tvComment;
tvTitle = (TextView) dialog.findViewById(R.id.tvViewTitle);
tvAmount = (TextView) dialog.findViewById(R.id.tvViewAmount);
tvCategory = (TextView) dialog.findViewById(R.id.tvViewCategory);
tvComment = (TextView) dialog.findViewById(R.id.tvViewComment);
tvTime = (TextView) dialog.findViewById(R.id.tvViewTime);
dialog.setTitle(expense.getTitle() + " EXPENDITURE");
tvTitle.setText(expense.getTitle());
tvAmount.setText(currencyManager.getCurrency() + " " + expense.getAmount());
tvCategory.setText(expense.getCategory());//**********************************************
tvTime.setText("Created on " + expense.getDate() + " At "
+ expense.getTime());
tvComment.setText(expense.getlocation());
//dialog.create();
dialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_view_expense, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView;
searchView = (SearchView) menu.findItem(R.id.ViewSearch).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
searchView.setOnSearchClickListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText)) {
lv_View_expense.clearTextFilter();
} else {
lv_View_expense.setFilterText(newText);
}
// Toast.makeText(getApplicationContext(),(adapter)+"", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(),(adapter)+"", Toast.LENGTH_LONG).show();
return false;
}
@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();
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
// }
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
case R.id.ViewSearch:
return true;
case R.id.mnView_help:
Toast.makeText(ViewExpense.this, "Tap Expense to View Details", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:2)
你在这里做错了
titlePosition = allTitles.get(position);
expense = expenseManager.getExpense(titlePosition);
因为它将从该位置提供原始列表中的数据。你必须使用适配器类的getItem方法。在该方法中传递位置,因为它已经过滤了数据。有关详情,请查看here
<强>更新强>
titlePosition = (String) adapter.getItem(position);
expense = expenseManager.getExpense(titlePosition);
试试这段代码。