我试图将字符串从EditText发送到从AddPLayer类到SelectPLayer类的ListView(R.id.selectview)中。我可以使用意图使AddPlayer活动启动SelectPLayer活动,但无法获得EditText输入来填充ListView。我希望这是一个更好的解释,对不起,谢谢。
//Addplayer class to add players using intent to separate activities
public class AddPlayer extends Activity{
//Declare buttons and edittext
Button listButtonplayer1;
Button listButtonplayer2;
EditText playerIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerinput);
//Identify buttons and editext
playerIn = (EditText) findViewById(R.id.inputname);
listButtonplayer1 = (Button)findViewById(R.id.addbutton);
listButtonplayer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Using intent to send input to SelectPLayer Class
Intent i = new Intent(AddPlayer.this,SelectPlayer.class);
//Using string text_key to get and send string through intent
i.putExtra("text_key", playerIn.getText().toString());
//Start intent activity
startActivity(i);
}
});
//Second button I am trying to use for SelectPLayer2 activity only.
listButtonplayer2 = (Button)findViewById(R.id.addbutton2);
listButtonplayer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Using intent to send input to SelectPLayer2 Class
Intent in = new Intent(AddPlayer.this, SelectPlayer2.class);
in.putExtra("text", playerIn.getText().toString());
startActivity(in);
}
});
}
}
//Class to select player 1
public class SelectPlayer extends Activity {
//Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class
public static ArrayList<String> list = new ArrayList<>();
public static ArrayAdapter<String> adapter;
public static ListView selectView;
TextView title;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectplayer);
//Identify selectview ListView
selectView = findViewById(R.id.selectview);
selectView.setClickable(true);
selectView.setVisibility(View.VISIBLE);
//Using adapter for ListView menu
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
selectView.setAdapter(adapter);
selectView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user
Intent intent = new Intent(SelectPlayer.this, GameEmulator.class); //starting an intent to call GameEmulator Activity
intent.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent
startActivity(intent); // starting GameEmulator Activity
//Using intent to retrieve string from AddPlayer Activity
i = getIntent();
String data = i.getExtras().getString("text_key");
list.add(data);
changeList();
}
});
}
public void changeList()
{
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
13
尝试这个。