2016-02-23 67 views
0

當我想要得到我按下的列表的名稱時,我會得到[email protected]。我怎樣才能從我的列表中獲得一個字符串值?問題出在「onItemClick」方法。OrmLite通過適配器從listView中獲取值

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 
private DBHelper mDbHelper = null; 
private DreamDetails dreamDetails; 
private ListView mListView; 
private DrawerLayout drawerLayout; 
private Toolbar mToolbar; 
private Dao<DreamDetails, Integer> dreamDetailsDao; 
private List<DreamDetails> dreamList; 
private int selectedRecordPosition = -1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mListView = (ListView) findViewById(R.id.listViewAllDreams); 
    try { 
     //Getting all Data from DB 
     dreamDetailsDao = getHelper().getDreamDetailsesDao(); 
     dreamList = dreamDetailsDao.queryForAll(); 

     // Set the header of the ListView 
     final LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View view = inflater.inflate(R.layout.list_view, mListView, false); 
     mListView.addHeaderView(view); 

     // my own adapter! 
     mListView.setAdapter(new AdapterArrayDream(this,R.layout.list_view,dreamList,dreamDetailsDao)); 
     mListView.setOnItemClickListener(this); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    initToolbar(); 
    initNavigationView(); 
    FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.fab); 
    myFab.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(MainActivity.this, NewDream.class); 
      startActivity(intent); 
     } 
    }); 
} 

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
    if(i > 0) 
    { 
     selectedRecordPosition = i - 1; 
     // final Intent intent = new Intent(this, ViewDream.class); 
     Log.i("TAAAAAAG", "onClick " + adapterView.getItemAtPosition(selectedRecordPosition)); 
    // intent.putExtra("D", String.valueOf(adapterView.getItemAtPosition(selectedRecordPosition))); 
     // startActivity(intent); 
    } 
} 
private void initNavigationView() { 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawlerLayout); 
} 
private void initToolbar() { 
    mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    mToolbar.setTitle(R.string.app_name); 
    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
     @Override 
     public boolean onMenuItemClick(MenuItem item) { 
      return false; 
     } 
    }); 
    // mToolbar.inflateMenu(R.menu.menu); 
} 

// This is how, DatabaseHelper can be initialized for future use 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_list_dreams, menu); 
    return true; 
} 

private DBHelper getHelper() { 
    if (mDbHelper == null) { 
     mDbHelper = OpenHelperManager.getHelper(this, DBHelper.class); 
    } 
    return mDbHelper; 
} 

}

回答

0

要創建一個查詢,看起來一個帳戶名稱和密碼,您將執行以下操作:

QueryBuilder<Account, String> qb = accountDao.queryBuilder(); 
    Where where = qb.where(); 
    // the name field must be equal to "foo" 
    where.eq(Account.NAME_FIELD_NAME, "foo"); 
    // and 
    where.and(); 
    // the password field must be equal to "_secret" 
    where.eq(Account.PASSWORD_FIELD_NAME, "_secret"); 
    PreparedQuery<Account, String> preparedQuery = qb.prepareQuery();