我尝试将一个项添加到带有数组的微调器中。我使用.add(数组),但问题是,我的Spinner是我的主要活动。并且用于添加已编辑文本的按钮位于其他活动中。
如何将adapter.add(数组)连接到我的主要活动?
MainActivity.java
public class MainActivity extends AppCompatActivity {
String selected;
Spinner spinner;
WebView myWebView;
List<CharSequence> server;
ArrayAdapter<CharSequence> adapter;
//Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add_server:
Intent intent = new Intent(this, AddIP.class);
this.startActivity(intent);
return true;
case R.id.menu_refresh:
myWebView.reload();
return true;
default:
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//WebView
myWebView = (WebView) findViewById(R.id.webView);
myWebView.setWebViewClient(new WebC());
WebSettings webSettings = myWebView.getSettings();
//JavaScript erlauben
webSettings.setJavaScriptEnabled(true);
//Spinner
spinner = (Spinner) findViewById(R.id.server_spinner);
server = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(this,
R.array.server_array_name, android.R.layout.simple_spinner_item, server);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selected = parent.getItemAtPosition(pos).toString();
myWebView.loadUrl(selected);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
AddIP.java
public class AddIP extends AppCompatActivity {
Button btn_back, btn_add;
EditText server_ip, server_name;
String add_server_ip, add_server_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_ip);
server_ip = (EditText)this.findViewById(R.id.edit_server_address);
server_name= (EditText)this.findViewById(R.id.edit_server_name);
//Back Button
btn_back=(Button)findViewById(R.id.btn_back);
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
//Add IP Button
btn_add=(Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add_server_ip = server_ip.getText().toString();
add_server_name= server_name.getText().toString();
MainActivity.adapter.add(add_server_ip);
MainActivity.adapter.add(add_server_name);
}
});
}
}
答案 0 :(得分:0)
如果要将数据从第二个活动传递到第一个活动,请在第一个活动中创建一个公共方法,通过创建first_activity.java类的对象从第二个活动中调用此方法。
e.g。 First_Activity.java
public void dataAddAdapter(String text){
adapter.add(text);
adapter.notifydatasetchanged();
}
Second_Activity.java
First_Activity first_activity = new First_Activity();
first_activity.dataAddAdapter(Your_string_data);
答案 1 :(得分:-1)
我认为你正在寻找这个http://developer.android.com/training/basics/intents/result.html
应用于您的代码,这将是:
<强> MainActivity.java 强>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add_server:
Intent intent = new Intent(this, AddIP.class);
this.startActivityForResult(intent, 1); //Better store 1 on a constant
return true;
case R.id.menu_refresh:
myWebView.reload();
return true;
default:
return true;
}
}
<强> AddIP.java 强>
//Add IP Button
btn_add=(Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add_server_ip = server_ip.getText().toString();
add_server_name= server_name.getText().toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("server_ip", add_server_ip);
returnIntent.putExtra("server_name", add_server_name);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
哦!您还需要override
onActivityResult
来检索数据并更新您的视图
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String server_ip=data.getStringExtra("server_ip");
String server_name=data.getStringExtra("server_name");
//Update your view here
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}