如何从列表视图中获取ID并发送到其他活动

时间:2016-12-28 19:14:04

标签: android listview

我想点击用户信息列表视图项(填充适配器),获取user_id, 我在主要活动中使用此代码:

    // long click on listview items
        LIST_USER.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("Clicked item id", " "+ id);
//                Intent intent = new Intent(getBaseContext(), UpdateActivity.class);
//                intent.putExtra("EXTRA_ID", id);
//                startActivity(intent);
                return true;
            }
        });

还没问题! 但是当发送user_id来更新活动时数据库已经停止!! 在更新活动中使用此代码:

公共类UpdateActivity扩展AppCompatActivity {     私有DatabaseHelper DB_HELPER;

private TextView TXT_VIEW;
private EditText EDT_NAME;
private EditText EDT_AGE;
private EditText EDT_GENDER;
private EditText EDT_PASS;
private EditText EDT_DESC;
private EditText EDT_PIC;
private Button BTN_UPDATE;
private Button BTN_DELETE;
private String STR_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update);

    TXT_VIEW = (TextView)  findViewById(R.id.textView);
    EDT_NAME = (EditText) findViewById(R.id.editText);
    EDT_AGE = (EditText) findViewById(R.id.editText2);
    EDT_GENDER = (EditText) findViewById(R.id.editText3);
    EDT_PASS = (EditText) findViewById(R.id.editText4);
    EDT_DESC = (EditText) findViewById(R.id.editText5);
    EDT_PIC = (EditText) findViewById(R.id.editText6);
    BTN_UPDATE = (Button) findViewById(R.id.button) ;
    BTN_DELETE = (Button) findViewById(R.id.button2);

    DB_HELPER = new DatabaseHelper(this);

    STR_ID = getIntent().getStringExtra("EXTRA_ID");
    TXT_VIEW.setText(STR_ID);

在日志信息中显示user_id:

enter image description here enter image description here

感谢&#39; S

1 个答案:

答案 0 :(得分:0)

起始问题在于类型,你传递一个long并将其作为字符串读取,在onCreate中引发了强制转换异常并导致崩溃。这应该使它工作:

private long STR_ID;
//...
STR_ID = getIntent().getLongExtra("EXTRA_ID");
TXT_VIEW.setText(String.valueOf(STR_ID));