gpt4 book ai didi

java - 在另一个 Activity 中使用字符串数组将项目添加到 Spinner

转载 作者:行者123 更新时间:2023-12-01 06:11:33 28 4
gpt4 key购买 nike

我尝试使用数组将项目添加到微调器中。我使用 .add(array) 但问题是,我的 Spinner 位于我的主要 Activity 中。添加已编辑文本的按钮位于其他 Activity 中。

如何将adapter.add(array)连接到我的主要 Activity ?

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);

}
});
}
}

最佳答案

如果要将数据从第二个 Activity 传递到第一个 Activity ,请在第一个 Activity 中创建一个公共(public)方法,然后通过创建 first_activity.java 类的对象从第二个 Activity 中调用此方法。

例如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);

关于java - 在另一个 Activity 中使用字符串数组将项目添加到 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33691097/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com