Search code examples
androidandroid-listviewandroid-event

How to make an item selected in ListView to execute after a button is clicked?


I have a ListView and I want to select (highlight) an item first and then click a button before it goes to another activity.

Here's an example of what I want to do:

This is how I made my ListView(I get its entry from a database)

    ListView lv = (ListView) findViewById(R.id.lvListOfCustomers);

    String strDBName = "db_customers.s3db";
    File fileDB = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
            strDBName);
    SQLiteDatabase dbLibrary = SQLiteDatabase.openOrCreateDatabase(fileDB,
            null);

    // Start - Cursor and Queries of List of Customers(ListView)
    String sqlQuery = "select _id, customer_name as cName, customer_address as cAddress, customer_status as cStatus from tbl_customers";
    Cursor cur = (Cursor) dbLibrary.rawQuery(sqlQuery, null);

    @SuppressWarnings("deprecation")
    SimpleCursorAdapter sca = new SimpleCursorAdapter(
            this.getApplicationContext(),
            R.layout.lv_list_of_customers_txtview, cur, new String[] {
                    "cName", "cAddress", "cStatus" }, new int[] {
                    R.id.tvCustomersName, R.id.tvCustomersAddress,
                    R.id.tvCustomersId }, CursorAdapter.FLAG_AUTO_REQUERY);
    lv.setAdapter(sca);

    // End - Cursor and Queries of List of Customers(ListView)

    // Start - Make the items highlighted
    int selectedListItem = getIntent().getIntExtra("PositionInList", -1);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setSelection(selectedListItem); 
    // End - Make the items highlighted

and this is what I've made for my Button

    case R.id.bCallRemarks:
        // TODO Auto-generated method stub
        Button bCRemarks = (Button) findViewById(R.id.bCallRemarks);
        bCRemarks.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ListView.OnItemClickListener oicl = new ListView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> av, View view,
                            int pos, long id) {
                        // TODO Auto-generated method stub
                        Cursor c = (Cursor) av.getItemAtPosition(pos);
                        String cName = c.getString(c
                                .getColumnIndexOrThrow("cName"));
                        Intent intent = new Intent(getApplication(),
                                CallRemarks.class);
                        intent.putExtra("CustomerName", cName);

                        Toast.makeText(av.getContext(), cName,
                                Toast.LENGTH_SHORT).show();

                         Intent CallRemarksScreen = new Intent(
                         getApplicationContext(), CallRemarks.class);
                         startActivity(CallRemarksScreen);
                    }

                };

            }

        });

I asked this because there's a ListView and multiple buttons in the bottom. So I have to select first item before choosing a functionality (button).


Solution

  • I put out the onItemClick of ListView outside the onClick of the Button

            @Override
            public void onItemClick(AdapterView<?> av, View view, int pos,
                    long id) {
                // TODO Auto-generated method stub
                selectedItem = true;
                Cursor c = (Cursor) av.getItemAtPosition(pos);
                String cName = c.getString(c.getColumnIndexOrThrow("cName"));
                intent = new Intent(getApplication(), CallRemarks.class);
                intent.putExtra("CustomerId", id);
    
                Toast.makeText(av.getContext(), "id: " + id, Toast.LENGTH_SHORT)
                        .show();
    
            }
        });
    
    and then here is my button
    
        case R.id.bCallRemarks:
            // TODO Auto-generated method stub
            Button bCRemarks = (Button) findViewById(R.id.bCallRemarks);
            bCRemarks.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectedItem == false) {
                        Toast.makeText(getApplicationContext(),
                                "No Customer Selected", Toast.LENGTH_SHORT)
                                .show();
                    } else {
                        // Intent intent = new Intent(
                        // getApplicationContext(), CallRemarks.class);
                        startActivity(intent);
                    }
                }
    
            });
            break;
    

    I initialized a Boolean flag to check if an item is selected or not, in case that I clicked a button without selecting an item in the ListView.

    private boolean selectedItem = false;