猿问

如何调试错误“字符串资源 ID #0x0”

我想在列表视图中选择项目时获取所选项目的值。


       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String data = getText(position).toString();

            Toast.makeText(Main2Activity.this,data,Toast.LENGTH_SHORT).show();

        }

    });

在此代码中String data = getText(position).toString(),我收到此错误:


字符串资源 ID #0x0


此代码是添加列表视图


void DBadd() {

    String name = addTxt1.getText().toString();

    String info = addTxt2.getText().toString();

    if (name.equals("") || info.equals("")) {

        Toast.makeText(getApplicationContext(), "정보를 입력해 주세요", Toast.LENGTH_SHORT).show();

        return;

    } else {

        db.execSQL("INSERT INTO tableName VALUES (null, '" + name + "', '" + info + "');");

        Toast.makeText(getApplicationContext(), "추가 성공", Toast.LENGTH_SHORT).show();


        addTxt1.setText(""); //입력시 EditText에 입력된값 지움

        addTxt2.setText("");

        cursor = db.rawQuery("SELECT * FROM tableName", null);

        startManagingCursor(cursor);    //엑티비티의 생명주기와 커서의 생명주기를 같게 한다.


        adapter = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_list_item_1);

        adapter2 = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_list_item_1);


        while (cursor.moveToNext()) {

            adapter.add(cursor.getString(1));

            adapter2.add(cursor.getString(2));

        }



        listView.setAdapter(adapter);

        listView2.setAdapter(adapter2);


    }

}

如何获取所选项目的价值?


守着一只汪
浏览 232回答 2
2回答

蝴蝶刀刀

试试这个,它对我有用:&nbsp;listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = listView.getItemAtPosition(position).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Main2Activity.this,data,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });或者,直接从adapter使用中获取字符串值.get(postion):listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = adapter.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Main2Activity.this,data,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });祝你好运

GCT1015

我不确定我是否正确理解了您的问题,但让我猜测所需的行为:listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView textViewOfTheClickedRaw = view.findViewById(android.R.id.text1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = textViewOfTheClickedRaw.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Main2Activity.this, data, Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });这是您如何从 TextView 获取值的方法 android.R.layout.simple_list_item_1但我真的不认为这就是你的应用程序应该如何工作
随时随地看视频慕课网APP

相关分类

Java
我要回答