gpt4 book ai didi

android - 使用 onClick 搜索 Listview 显示错误结果

转载 作者:行者123 更新时间:2023-11-29 19:20:40 24 4
gpt4 key购买 nike

我的 ListView 工作正常,但我正在使用 SearchView 进行搜索以在 ListView 中查找项目。当我最初单击原始 ListView 中的一个项目时,它会显示一个与所单击项目相关的对话框,但在完成搜索后,显示的对话框是错误的。更具体地说,它在该位置显示原始 ListView 项的对话框。请帮忙

    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);

}
}

}

最佳答案

你在这里做错了

titlePosition = allTitles.get(position);
expense = expenseManager.getExpense(titlePosition);

因为它将从该位置的原始列表中提供数据。您必须使用适配器类的 getItem 方法。在该方法中传递位置,因为它已过滤数据。更多信息请查看 here

更新

titlePosition = (String) adapter.getItem(position);
expense = expenseManager.getExpense(titlePosition);

试试这段代码。

关于android - 使用 onClick 搜索 Listview 显示错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42580294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com