单击列表视图项目并将数据库项目 ID 传递给下一个 activity?
Clicking a list view item and passing a database item ID to the next activity?
我有一个 activity,其中包含一个从 SQLiteDatabase
加载的列表视图。我想要发生的是,当我单击其中一个项目时,我想要第二个 activity 来加载和显示项目 ID。这是我目前所拥有的:
setOnItemClickListener:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getBaseContext(),Detail.class);
intent.putExtra("DB_ID_EXTRA",IWantThisToBeMyID);
startActivity(intent);
}
}
正在填充 IWantThisToBeMyID:
public static String IWantThisToBeMyID = "com.example.projectloc.assignment.pictures._id";
Detail.class
public class Detail extends AppCompatActivity{
String passedVar = null;
private TextView passedView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
passedVar=getIntent().getStringExtra(Master.dbIDEx);
passedView=(TextView)findViewById(R.id.passedText);
passedView.setText(passedVar);
}
当我单击其中一个列表视图项目时,它会加载新的 activity 但什么也不显示。
试试这个
passedVar=getIntent().getStringExtra("DB_ID_EXTRA");
改为
passedVar=getIntent().getStringExtra(Master.dbIDEx);
当您在 intent
中添加数据时,请查看您正在添加 DB_ID_EXTRA
key
名称以及您的 Detail
activity 你正在使用 Master.dbIDEx
不同的 key
You need to use same key name when passing data through intent
使用这个
passedVar = getIntent().getStringExtra("DB_ID_EXTRA");
而不是
passedVar = getIntent().getStringExtra(Master.dbIDEx);
您传递的密钥是 DB_ID_EXTRA 并使用错误的密钥从 intent 中获取值。只需更改此
public class Detail extends AppCompatActivity{
String passedVar = null;
private TextView passedView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
passedVar=getIntent().getStringExtra("DB_ID_EXTRA");
passedView=(TextView)findViewById(R.id.passedText);
passedView.setText(passedVar);
}
我有一个 activity,其中包含一个从 SQLiteDatabase
加载的列表视图。我想要发生的是,当我单击其中一个项目时,我想要第二个 activity 来加载和显示项目 ID。这是我目前所拥有的:
setOnItemClickListener:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getBaseContext(),Detail.class);
intent.putExtra("DB_ID_EXTRA",IWantThisToBeMyID);
startActivity(intent);
}
}
正在填充 IWantThisToBeMyID:
public static String IWantThisToBeMyID = "com.example.projectloc.assignment.pictures._id";
Detail.class
public class Detail extends AppCompatActivity{
String passedVar = null;
private TextView passedView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
passedVar=getIntent().getStringExtra(Master.dbIDEx);
passedView=(TextView)findViewById(R.id.passedText);
passedView.setText(passedVar);
}
当我单击其中一个列表视图项目时,它会加载新的 activity 但什么也不显示。
试试这个
passedVar=getIntent().getStringExtra("DB_ID_EXTRA");
改为
passedVar=getIntent().getStringExtra(Master.dbIDEx);
当您在 intent
中添加数据时,请查看您正在添加 DB_ID_EXTRA
key
名称以及您的 Detail
activity 你正在使用 Master.dbIDEx
不同的 key
You need to use same key name when passing data through
intent
使用这个
passedVar = getIntent().getStringExtra("DB_ID_EXTRA");
而不是
passedVar = getIntent().getStringExtra(Master.dbIDEx);
您传递的密钥是 DB_ID_EXTRA 并使用错误的密钥从 intent 中获取值。只需更改此
public class Detail extends AppCompatActivity{
String passedVar = null;
private TextView passedView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
passedVar=getIntent().getStringExtra("DB_ID_EXTRA");
passedView=(TextView)findViewById(R.id.passedText);
passedView.setText(passedVar);
}