0
votes

I have a SQLite Database that contains the following information

--------------------------------
Account_Name    Account_Balance
-------------------------------- 
Savings         1000.00

Checking        1000.00

I have the item being output to a Listview. My question is how do I get my OnItemClick to properly tell me the value that was selected? When I click Savings, I want "Savings" to appear through a Toast. When I tried output, I could only output the item position. I don't want the position in the listview, I am more interested in the String value.


    public void onItemClick(AdapterView parent, View view, int position, long id)
    {
        /*What do I need to code here to just have the value outputted*/        

        //Toast toast = Toast.makeText(getApplicationContext(), temp1, Toast.LENGTH_LONG);
        //toast.show();
    }

CODE: main_menu_activity.java

public class main_menu_activity extends Activity implements AdapterView.OnItemClickListener
{
DatabaseHandler db;
ArrayList<Account> account_details;
ListView accountList;
int num;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu_activity);
    accountList = (ListView)findViewById(R.id.accountListView);
    db = new DatabaseHandler(getApplicationContext());

    displayListView();

    // Empty Account table
    //db.deleteFromAccountTable();

    accountList.setOnItemClickListener(this);

    num = 1;
}

// MENU //
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu_activity, menu);

    return super.onCreateOptionsMenu(menu);
}

// MENU //
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // take appropriate action for each action item clicked
    switch(item.getItemId())
    {
        case R.id.action_add_new:
        {
            // perform add new item action
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setMessage("Enter account details:");

            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);

            // Set an EditText view
            final EditText input = new EditText(this);
            input.setHint("Account Name");
            layout.addView(input);

            final EditText input2 = new EditText(this);
            input2.setHint("Account Balance");
            layout.addView(input2);

            alert.setView(layout);

            alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {
                    if (input.getText().toString() != null)
                    {
                        // Create empty Account
                        Account temp_account;
                        temp_account = new Account();

                        // Save information to SQLiteDatabase
                        temp_account.setAccountName(input.getText().toString());
                        temp_account.setAccountBalance(Double.parseDouble(input2.getText().toString()));

                        // Add temp account
                        db.addAccount(temp_account);
                        displayListView();
                    }
                    else
                    {
                        dialogInterface.cancel();
                    }
                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {
                    // Output to logcat
                    /*ArrayList<Account> allAccounts = db.getAllAccounts();
                    for (Account account: allAccounts)
                    {
                        Log.d("Output", Integer.toString(account.getAccountID()));
                        Log.d("Output", account.getAccountName());
                        Log.d("Output", Double.toString(account.getAccountBalance()));
                    }*/
                    // cancel
                    dialogInterface.cancel();
                }
            });

            alert.show();
            return true;
        }
        default:
        {
            return super.onOptionsItemSelected(item);
        }
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    /* CODE TO OUTPUT SELECTED LISTVIEW VALUE */
}

// DISPLAY ACCOUNT LISTVIEW //
public void displayListView()
{
    account_details = new ArrayList<Account>();
    account_details = db.getAllAccounts();
    accountList.setAdapter(new ListViewBaseAdapter(account_details,this));
}
}

listview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >

<TextView
    android:id="@+id/account_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textColor="#00628B"
    android:textSize="22dp"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/account_balance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:textColor="#00628B"
    android:textSize="22dp"/>

</RelativeLayout>
3
Please post the code of your adapter.Benito Bertoli

3 Answers

0
votes

You're going to need to have a custom adapter (and layout file) for the row and then add handler for each View in that row.

Vogella has some awesome tutorials so instead of regurgitating them I will point you there.

0
votes

If you just want to get the name, then why don't you try something like this:

  1. Define an XML layout that contains a row, probably a TextView for each.
  2. Put this code in a custom adapter.
  3. Set the id for the value that contains the savings/checking name, let's say android:id="@+id/accountType
  4. Find that view in the onItemClicked, and get the text.

The code looks like:

String type=((TextView) view.findViewById(R.id.accountType)).getText();
0
votes

I assume you have your own custom adapter for that. So you can just attach OnClickListeners at the getView() method to every needed View (TextView in your case). Then inside this listener do the following:

View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView myView = (TextView) v;
        Toast.makeText(context, myView.getText(),Toast.LENGTH_SHORT).show();
    }
}