gpt4 book ai didi

java - 当我从另一 Activity 获取结果时,ListView 仅更新一个 View 项,如何获取所有过去的结果?

转载 作者:行者123 更新时间:2023-12-01 09:32:09 24 4
gpt4 key购买 nike

当我从另一 Activity 获取结果时,ListView 仅更新一个 View 项,如何获取用户输入的所有过去结果并将其显示在 ListView 中?

我正在使用 Intent 在两个 Activity 之间发送数据。

这个想法是用户列出他/她曾经访问过的所有国家。显示内容可能如下所示:

1964 年瑞典

1970 年芬兰

1979 年丹麦

1980年英格兰

1982 年挪威

...

2009年乌克兰

用户通过按菜单→添加国家/地区添加新国家/地区。这个 Action 呈现一个新页面( Activity ),用户可以在其中添加新条目(国家/地区 +年)。

我已经被困了 8 个小时了,所以任何帮助都会派上用场。谢谢!

MyCountries.java

import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
* Created by root on 2016-09-03.
*/
public class MyCountries extends Activity {

private ListView listView;
private MyAdapter adapter;
private Activity main_activity;
String year="";
String country="";
ArrayList<String> info = new ArrayList<>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_bar);

listView = (ListView)findViewById(R.id.listView);
adapter = new MyAdapter(this,R.layout.list_item, info);

main_activity = this; // Simplifies Intent handling
}

@Override
protected void onStop() {
super.onStop();

//listView.setAdapter(adapter);
}

@Override
protected void onStart() {
super.onStart();

}

@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first

// Activity being restarted from stopped state
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// An XML based menu specification.
// See res/menu/action_menu.xml for details
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_integer_menu:

/* Start new Activity that returns a result */
Intent intent = new Intent(main_activity,Read_Country.class);
main_activity.startActivityForResult(intent,0);

return true;

case android.R.id.home: // Predefined icon ID

// app icon in action bar clicked ==> go home
Intent intent2 = new Intent(this, MainList.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

/*
* Adapter to populate list with colors and integers
*/
class MyAdapter extends ArrayAdapter<Object> {

public MyAdapter(Context context, int textViewResourceId, List list) {
super(context,textViewResourceId, list);
}

@Override // Called when updating the ListView
public View getView(int position, View convertView, ViewGroup parent) {
/* Reuse super handling ==> A TextView from R.layout.list_item */
TextView tv = (TextView) super.getView(position,convertView,parent);

/* Find corresponding entry */
Object obj = getItem(position);

/* Update TextView with string information */
tv.setText(obj.toString());
tv.setBackgroundColor(0xffffffff);
tv.setTextColor(0xff000000); // Black

return tv;
}
}

/** Called when the activity receives a results. */
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
country = result.getStringExtra("result1");
year = result.getStringExtra("result2");
//this is where I update the arrayList, which is an element of the arrayadapter
info.add(year + " " + country);

/* for (int i = 0; i < info.size(); i++) {
System.out.println(info.get(i));
}
//adapter.notifyDataSetChanged();
adapter.add(year + " " + country); // Add new string to data list
*/
listView.setAdapter(adapter);
}
}
}

Reader_Country.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
* Created by root on 2016-09-03.
*/
public class Read_Country extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_country);

/* Assign listener to button */
Button button = (Button)findViewById(R.id.done_button);
button.setOnClickListener(new ButtonClick());
}

private class ButtonClick implements View.OnClickListener {
public void onClick(View v) {
EditText reader = (EditText)findViewById(R.id.country_reader);
String country = reader.getText().toString();

EditText reader2 = (EditText)findViewById(R.id.year_reader);
String year = reader2.getText().toString();

/* Create new result intent */
Intent reply = new Intent();
reply.putExtra("result1", country);
reply.putExtra("result2", year);
setResult(RESULT_OK,reply);
finish(); // Close this activity
}
}
}

最佳答案

我认为解决您的问题的最佳解决方案是使用 SQLDatabase 在用户手机本身上创建本地数据库。

但是,如果您希望用户可以在他登录的任何手机上查看他在您的应用程序中列出的这些国家/地区,那么我建议您使用一些可能的数据库,在我看来 FireBase 是一个真正不错的选择.

希望这对您有帮助:)

关于java - 当我从另一 Activity 获取结果时,ListView 仅更新一个 View 项,如何获取所有过去的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316533/

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