I have created three different activities with three different ListViews similar to this and created another activity with a Button.
The purpose is to select any item from any ListView and it will change the Button text to the selected item I choose.
Here is the code I use to create the ListViews:
public class C_Camera extends AppCompatActivity {
ListView listView_C_Camera;
String[] listview_c_camera = new String[]{
"a" , "b", "c", "d" , "e" , "f","g", "h","i","j"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c__camera);
listView_C_Camera = findViewById(R.id.listview_c_camera);
ArrayAdapter<String> CanonCamera = new ArrayAdapter<>(this , R.layout.listview_row,listview_c_camera);
listView_C_Camera.setAdapter(CanonCamera);
listView_C_Camera.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String C_Camera_Temp = listview_c_camera[position];
Intent intent = new Intent(C_Camera.this, BuildCamera.class);
intent.putExtra("C_Camera",C_Camera_Temp);
startActivity(intent);
}
});
}
}
I have another activity with a Button that will get the values of the three different ListViews.
public class BuildCamera extends AppCompatActivity {
Button camerabuild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_build_camera);
camerabuild = findViewById(R.id.camerabuild);
String C_Camera_Holder = getIntent().getStringExtra("C_Camera");
String N_Camera_Holder = getIntent().getStringExtra("N_Camera");
String S_Camera_Holder = getIntent().getStringExtra("S_Camera");
camerabuild.setText(C_Camera_Holder);
camerabuild.setText(N_Camera_Holder);
camerabuild.setText(S_Camera_Holder);
...
Here I'm facing a problem.
If I use String C_Camera_Holder = getIntent().getStringExtra("C_Camera")
and comment the others it will work and change the text,
String C_Camera_Holder = getIntent().getStringExtra("C_Camera");
// String N_Camera_Holder = getIntent().getStringExtra("N_Camera");
// String S_Camera_Holder = getIntent().getStringExtra("S_Camera");
camerabuild.setText(C_Camera_Holder);
// camerabuild.setText(N_Camera_Holder);
// camerabuild.setText(S_Camera_Holder);
but if I uncomment the second one ( String N_Camera_Holder = getIntent().getStringExtra("N_Camera")
) the first one will stop changing the text and the second will do and so on.
String C_Camera_Holder = getIntent().getStringExtra("C_Camera");
String N_Camera_Holder = getIntent().getStringExtra("N_Camera");
// String S_Camera_Holder = getIntent().getStringExtra("S_Camera");
camerabuild.setText(C_Camera_Holder);
camerabuild.setText(N_Camera_Holder);
// camerabuild.setText(S_Camera_Holder);
the button will always carry the last setText() function that you call.
camerabuild.setText(C_Camera_Holder);
camerabuild.setText(N_Camera_Holder);
camerabuild.setText(S_Camera_Holder); <<----------which is, this one.
to make it dynamic on your activity intents you can use if
statement.
like this
if(C_Camera_Holder != null){
camerabuild.setText(C_Camera_Holder);
}else if(N_Camera_Holder != null){
camerabuild.setText(N_Camera_Holder);
}else if(S_Camera_Holder != null){
camerabuild.setText(S_Camera_Holder);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.