2014-02-19 52 views
0

我有一個包含以下信息的Android - ListView控件 - 在列表視圖產值屏幕

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

Checking  1000.00 

我有項目被輸出到ListView SQLite數據庫。我的問題是如何讓我的OnItemClick正確告訴我所選的價值?當我點擊儲蓄時,我希望通過吐司顯示「儲蓄」。當我嘗試輸出時,我只能輸出項目位置。我不想在列表視圖中的位置,我更感興趣的字符串值。

 

    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> 
+0

請發佈您的適配器的代碼。 –

回答

0

如果你只是想獲得的名稱,那麼你爲什麼不嘗試這樣的事:

  1. 定義一個包含一排,可能是每一個TextView的XML佈局。
  2. 將此代碼放入自定義適配器中。
  3. 設置包含儲蓄/支票名id的值,讓我們說android:id="@+id/accountType
  4. 發現在onItemClicked這一觀點,並獲得文本。

代碼如下:

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

我假設你有針對自己的定製適配器。因此,您可以在getView()方法中將OnClickListener s附加到每個需要的視圖(您的案例中的TextView)。然後在這個聽衆裏面做下面的事情:

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