本文将分享将列表传递给Android中的另一个活动的详细内容,并且还将对列表传递给函数进行详尽解释,此外,我们还将为大家带来关于android–一个活动到另一个活动之间的延迟、android–从一个活
本文将分享将列表传递给Android中的另一个活动的详细内容,并且还将对列表传递给函数进行详尽解释,此外,我们还将为大家带来关于android – 一个活动到另一个活动之间的延迟、android – 从一个活动获取价值到另一个活动、android – 从活动中调用另一个活动中的方法、android – 使用bundle将数据从一个活动传递到另一个活动 – 不在第二个活动中显示的相关知识,希望对你有所帮助。
本文目录一览:- 将列表传递给Android中的另一个活动(列表传递给函数)
- android – 一个活动到另一个活动之间的延迟
- android – 从一个活动获取价值到另一个活动
- android – 从活动中调用另一个活动中的方法
- android – 使用bundle将数据从一个活动传递到另一个活动 – 不在第二个活动中显示
将列表传递给Android中的另一个活动(列表传递给函数)
我创建了一个列表,并希望将列表传递给另一个活动,但是当我创建intent时,我在putExtra语句中收到错误.只是想知道是否有任何简单的方法来传递字符串列表而不是单个字符串?
谢谢
private List<String> selItemList;
private ListView mainListView = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipes);
Button searchBtn = (Button) findViewById(R.id.searchButton);
searchBtn.setonClickListener(new OnClickListener() {
public void onClick(View v) {
if (selItemList == null) {
Toast.makeText(getApplicationContext()," Please Make A Selection ", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Recipes2.this, XMLParser.class);
intent.putExtra("items_to_parse", selItemList);
startActivityForResult(intent, 0);
}
}
});
解决方法:
您无法在Intent.putExtras(String name,List<?> list);中传递List.
我想你可以使用一个String of String并在putExtras中传递它,如下所示:
private List<String> selItemList;
private ListView mainListView = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipes);
Button searchBtn = (Button) findViewById(R.id.searchButton);
searchBtn.setonClickListener(new OnClickListener() {
public void onClick(View v) {
if (selItemList == null) {
Toast.makeText(getApplicationContext(), "Please Make A Selection", Toast.LENGTH_SHORT).show();
} else {
String[] selItemArray = new String[selItemList.size()];
// copy your List of Strings into the Array, and then pass it in your intent
// ....
Intent intent = new Intent(Recipes2.this, XMLParser.class);
intent.putExtra("items_to_parse", selItemArray);
startActivityForResult(intent, 0);
}
}
});
android – 一个活动到另一个活动之间的延迟
我在Android中开发了一个应用程序.其中包含如此多的数据(String)和图像.字符串数据来自数据库,图像来自/ res文件夹.
在我的应用程序第一活动中显示书籍的类别.然后我选择其中任何一个,然后跳转到下一个活动,显示所有书籍图像和所选类别的简要描述,这些所有数据都来自数据库,带有查询操作,并使用ArrayAdapter填充自定义列表视图.这些工作并显示我想要的所有东西.
但问题是,当我从一个活动中点击类别时,显示第二个活动(选定类别的详细信息)需要1分钟以上的时间.所以,这里的用户被卡住了.这对我的申请来说很糟糕.
那么有没有办法解决这些或任何想法显示一个活动到第二个活动之间的活动加载过程?
提前致谢.
请帮我解决这些问题.
解决方法:
使用AsyncTask作为
public class My_Game_Task extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
//put a preloder
super.onPreExecute();
}
@Override
protected Void doInBackground(String... arg0) {
// Todo Auto-generated method stub
find data from database
return null;
}
@Override
protected void onPostExecute(Void result) {
dismiss preloader
set adapter here
super.onPostExecute(result);
}
}
打电话
oncreate as new My_Game_Task().execute();
这将立即显示下一个活动并显示预加载器
android – 从一个活动获取价值到另一个活动
我如何使用savePreferences和loadPreferences方法
在第一次活动中
private static final String GLOBAL_PREFERENCES = "music_status";
public static void savePreferences(@NonNull Context context, String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}...
protected void onResume() {
super.onResume();
musicGroup = (RadioGroup) findViewById(R.id.radioGroupForMusic);
turnOn = (RadioButton) findViewById(R.id.radioButtonMusicOn);
turnOff = (RadioButton) findViewById(R.id.radioButtonMusicOff);
musicGroup.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButtonMusicOff:
mediaPlayer.pause();
savePreferences(MainMenuActivity.this,"music_status",0);
case R.id.radioButtonMusicOn:
mediaPlayer.start();
savePreferences(MainMenuActivity.this,"music_status",1);
}
}
});
}
在第二次活动中
private static final String GLOBAL_PREFERENCES = "music_status";
public static int loadPreferences(@NonNull Context context, String key, int defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, defaultValue);
}
但是我不知道如何从第一次活动中获得价值
解决方法:
简短的回答是你做不到.除非您的状态变量是静态的.对于这种情况,这是非常糟糕的.
你有三个选择.
SharedPreferences
与我之前的修订版相反.这可能是您案例的最佳选择.
如果您只想保存某些内容的状态,您可能会发现最好不要使用类来保存音乐的状态.你可以这样做@ cricket_007建议并实现SharedPreferences.
您可以使用这些示例函数:
private static final String GLOBAL_PREFERENCES = "a.nice.identifier.for.your.preferences.goes.here";
public static void savePreferences(@NonNull Context context, String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
public static int loadPreferences(@NonNull Context context, String key, int defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(GLOBAL_PREFERENCES, Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, defaultValue);
}
然后,您可以在代码中使用这些功能来保存音乐的状态.而不是status.setStatus(0);和status.setStatus(1);你可以使用Utils.savePreferences(context,“music_status”,1);而不是status.getStatus()你可以使用Utils.loadPreferences(context,“music_status”,0);
Parcelable
One option for you is to implement在您的musicStatus类中可以使用.然后,您可以通过Intent将对象发送到第二个活动.您可以在下面找到实现Parcelable的示例类.
一旦你有了这个,你就可以通过Intent传递它:
musicStatus status = new musicStatus();
status.setStatus(8);
Intent intent = new Intent(this, HighscoreActivity.class);
intent.putExtra("status", status);
startActivity(intent);
课程实施:
public class musicStatus implements Parcelable {
private int status;
public int getStatus() {
return status;
}
public void setStatus(int status){
this.status = status;
}
private musicStatus(Parcel in) {
status = in.readInt();
}
public void writetoParcel(Parcel out, int flags) {
out.writeInt(status);
}
public static final Parcelable.Creator<musicStatus> CREATOR = new Parcelable.Creator<musicStatus>() {
public musicStatus createFromParcel(Parcel in) {
return new musicStatus(in);
}
public musicStatus[] newArray(int size) {
return new musicStatus[size];
}
};
public int describeContents() {
return 0;
}
}
独生子
在这种情况下,这确实是一种反模式.但是,它仍有可能.
public class musicStatus {
private static musicStatus mInstance = null;
private int status;
@NonNull
public static musicStatus getInstance() {
if (mInstance == null) {
synchronized(mInstance) {
if (mInstance == null) {
mInstance = new musicStatus();
}
}
}
}
public int getStatus(){
return status;
}
public void setStatus(int status){
this.status = status;
}
}
android – 从活动中调用另一个活动中的方法
我知道我们无法从另一个Activity中的Activity调用方法.我正试图找出解决这个问题的最佳方法.
这是我的代码.这是我试图调用的方法.这是我的scoreCard活动.
public void numPlayerSetup(){
{
int[] ids = {
R.id.TextView11, R.id.TextView12, R.id.TextView13
};
for(int i : ids) {
TextView tv = (TextView)findViewById(i);
tv.setVisibility(View.INVISIBLE);
}
}
这是我试图调用该方法的方法.得分是scoreCard类的一个对象.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int item = spinner.getSelectedItemPosition();
if(item==1){
Log.i("error","This Sucks");
score.numPlayerSetup();
}
}
我试图将numPlayerSetup方法放在一个不会扩展Activity的不同类中,只包含逻辑,但是我不能在不扩展活动的情况下使用findViewById()方法.
这就是我的称呼方式.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int item = spinner.getSelectedItemPosition();
ArrayList<TextView> myTextViewList = new ArrayList<TextView>();
TextView tv1 = (TextView)findViewById(R.id.TextView14);
myTextViewList.add(tv1);
if(item==1){
Log.i("error","This Sucks");
Setup.numPlayerSetup(myTextViewList);
}
那就是我打电话的课程.
public class Setup {
TextView tv;
public static void numPlayerSetup(ArrayList<TextView> tvs){
for(TextView tv : tvs) {
Log.i("crash","This Sucks");
tv.setVisibility(View.INVISIBLE); //this line is highlighted in the debugger as the line my error is coming from
}
}
}
它将消息记录在logcat中并给我一个空指针异常.调试器说tv的值为null.这是为什么我得到一个空指针异常?
解决方法:
您可以创建一个Utitlity类(而不是Activity)并传入您想要更改的Textviews.并在需要时调用该方法.
public class Setup {
public static void numPlayerSetup(ArrayList<TextView> tvs){
for(TextView tv : tvs) {
tv.setVisibility(View.INVISIBLE);
}
}
}
然后你可以像(在Activity中)一样使用它:
ArrayList<TextView> myTextViewList = new ArrayList<TextView>();
TextView tv1 = (TextView)findViewById(R.id.tv1);
myTextViewList.add(tv1);
Setup.numPlayerSetup(myTextViewList);
android – 使用bundle将数据从一个活动传递到另一个活动 – 不在第二个活动中显示
我目前正在尝试通过REST API调用获取数据,解析它以获取我需要的信息,然后将该信息传递给新活动.我正在使用loopj.com中的异步HTTP客户端作为REST客户端,然后分别使用以下代码将onClick和onCreate用于当前和未来的活动.
Eclipse没有为我的任何代码传递任何错误,但是当我尝试在模拟器中运行时,在新活动/视图打开时我什么也得不到(即空白屏幕).我试图在我的REST CLIENT中使用不同的URL进行编码,但我仍然没有看到任何内容.我甚至通过在onClick中注释try / catch并更改bundle.putString(“VENUE_NAME”,venueName)中的venueName来取消API调用. to searchTerm.仍然,新视图出现但没有显示任何内容.什么没有通过,或者我忘记了第二个活动显示的是什么名字?
public void onClick(View view) {
Intent i = new Intent(this, ResultsView.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String searchTerm = editText.getText().toString();
//call the getFactualResults method
try {
getFactualResults(searchTerm);
} catch (JSONException e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire the second activity
startActivity(i);
}
第二个活动中应该接收意图和捆绑并显示它的方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the message from the intent
//Intent intent = getIntent();
//String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String venname = bundle.getString(MainActivity.VENUE_NAME);
//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venname);
//set the text view as the activity layout
setContentView(textView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setdisplayHomeAsUpEnabled(true);
}
}
谢谢你的帮助.非常感谢.
解决方法:
您可以通过两种方式发送数据.这就是你现在发送它的方式.它没有任何问题.
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
但是,在您的代码(第二个Activity)中,您将Bundle中的键称为MainActivity.VENUE_NAME,但代码中没有任何内容表明您有一个类,该类返回值作为Bundle发送的实际键名.将第二个Activity中的代码更改为:
Bundle bundle = getIntent().getExtras();
//Extract the data…
String venname = bundle.getString("VENUE_NAME");
//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venname);
如果Bundle包含使用此密钥的密钥,您可以检入第二个Activity,并且您将知道密钥在Bundle中不存在.但是,上面的修正将使它适合您.
if (bundle.containsKey(MainActivity.VENUE_NAME)) {
....
}
今天的关于将列表传递给Android中的另一个活动和列表传递给函数的分享已经结束,谢谢您的关注,如果想了解更多关于android – 一个活动到另一个活动之间的延迟、android – 从一个活动获取价值到另一个活动、android – 从活动中调用另一个活动中的方法、android – 使用bundle将数据从一个活动传递到另一个活动 – 不在第二个活动中显示的相关知识,请在本站进行查询。
本文标签: