0

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Rogene Sagmit
  • 101
  • 1
  • 8
  • Which part are you hung up on? – Brent Hronik Feb 26 '13 at 02:47
  • I managed to select an item(but without any highlight colors). My intention is that the item selected will be passed to the other activity when the button is clicked. My problem is that nothing happens when I click the button. – Rogene Sagmit Feb 26 '13 at 03:10
  • Can you post your code that you have implemented so far? – Brent Hronik Feb 26 '13 at 03:26
  • @BrentHronik you can see it here, http://www.wepaste.com/listviewbuttonstackoverflow/ – Rogene Sagmit Feb 26 '13 at 03:47
  • Hmm, the code seems to be a little hard to read in the link, but it looks like you create your OnItemClickListener inside of you OnClicked() callback? I would move that outside. – Brent Hronik Feb 26 '13 at 03:53
  • you want hilite selected item and then want to perform click from bottom button ? – Ankitkumar Makwana Feb 26 '13 at 05:05
  • @BrentHronik sorry I don't know how to put the codes here at the comment box because of the limited characters. Yes I put the OnItemClickListener of the ListView inside the onClicked of the Button. Just to make sure of your suggestion I would repeat what you had said. "I would move the OnItemClickListener of the ListView outside the onClick" right? – Rogene Sagmit Feb 26 '13 at 05:09
  • @ankitmakwana yes exactly :) – Rogene Sagmit Feb 26 '13 at 05:10
  • I updated my questions by putting my codes there. Thanks a lot! – Rogene Sagmit Feb 26 '13 at 05:28
  • you can use baseadpter and ontem click you can setbackground wich you required and get details from baseAdpter wich require in Buttonclick simple – Ankitkumar Makwana Feb 26 '13 at 05:42
  • @ankitmakwana sorry I don't quite get it. can you modify the code indicated above so I can understand it more? thanks! – Rogene Sagmit Feb 26 '13 at 05:57
  • @ankitmakwana I think I can't use the baseadapter because I am using a ListView with multiply columns and I am getting the data from the Database. Correct me If I am wrong – Rogene Sagmit Feb 26 '13 at 09:14
  • Yeah you have to pull out the OnItemClickListener outside of you OnClickListener, because it won't be initialized until you click your button the first time. Additionally, what information do you want to to store/pass on to the next Activity when it is clicked on? – Brent Hronik Feb 26 '13 at 14:36
  • @BrentHronik I get it now. Thank you for the suggestion. I'll post the codes later after I'm done. Thanks again! :) – Rogene Sagmit Feb 27 '13 at 03:47

1 Answers1

0

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;
Rogene Sagmit
  • 101
  • 1
  • 8
  • Case Statement because there's a lot of buttons below the ListView. But I'll try the onclick method later. Thanks again Brent! :) – Rogene Sagmit Feb 28 '13 at 01:11
  • Could you post the entire switch statement? I am really not sure what you are trying to do there? – Brent Hronik Feb 28 '13 at 02:56
  • public void onClick(View arg0) { switch (arg0.getId()) {case R.id.bCashRef: Button bCRef = (Button) findViewById(R.id.bCashRef); bCRef.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent CashRefScreen = new Intent(getApplicationContext(), CashRef.class); startActivity(CashRefScreen); } }); break; Just imagine that there's 20 case of buttons that each of them will go to their respective class. – Rogene Sagmit Feb 28 '13 at 06:29
  • Ahh, I would get rid of that switch statement, and just add the onClickListeners to the buttons in the onCreate callback(just make a helper function called initializeButtons, or something like that, so your onCreate callback is still manageable). As it stands now, you have your global onClickListener, and then add an onClickListener to the Button that is clicked. – Brent Hronik Feb 28 '13 at 15:41
  • I see, I get your logic. After I'm done with my task for today I would definitely give it a try. Thanks again! – Rogene Sagmit Mar 01 '13 at 02:29