从另一个类的ListView中获取ID值

时间:2016-12-12 19:09:19

标签: android listview android-sqlite android-database

我是一名新手程序员,我写了一些代码。我想在另一个类中获取wallet_id,但是当我尝试这个变量时,总是有值= 0.在类ChooseWallet中,wallet_id设置了值wallet_id。但我不知道如何在另一堂课中获得wallet_id。任何的想法?

public class ChooseWallet extends AppCompatActivity {
public int wallet_id;
DatabaseDEO db = new DatabaseDEO(this);
ListView choose;
private SimpleCursorAdapter adapter;
final String[] from = new String[] {Wallets.COLUMNS.name,Wallets.COLUMNS.surname};
final int[] to = new int[] {R.id.name, R.id.surname };



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_wallet);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    choose = (ListView)findViewById(R.id.listselement);
    choose.setEmptyView(findViewById(R.id.null));
    Cursor cursor = db.fetch();
    adapter = new SimpleCursorAdapter(this, R.layout.activity_see_record, cursor, from, to, 0);
    adapter.notifyDataSetChanged();

    choose.setAdapter(adapter);

        choose.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        walllet_id = position+1;
    }
});

}}

2 个答案:

答案 0 :(得分:0)

在我看来,这是不可能的。 Spinner是比ListView更好的解决方案。

答案 1 :(得分:0)

也许您可以尝试使用setOnItemClickListener。 像这样:

    choose.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            walllet_id = position+1;
        }
    });