fritz-playmaker
fritz-playmaker

Reputation: 741

Searching Listview with onClick showing wrong results

My Listview works properly, but i am searching using a SearchView to look for an item within the listview. When i initially click on an item in the original listview, it shows a dialog pertaining to the clicked item, but after the search has been done, the dialog which shows is wrong. More specifically, it shows the dialog of the original listview item at that position. Please help

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

        }
    }

}

Upvotes: 1

Views: 773

Answers (1)

Wasim K. Memon
Wasim K. Memon

Reputation: 6067

You are doing wrong over here

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

As it will give data from original list from that position. you have to use getItem method of adapter class. in that method pass position as it has filtered data. for more info check here

Update

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

Try this code.

Upvotes: 2

Related Questions