Android-每次加载MainActivity时都复制ListView对象
我在一个列表中有20个联系人,您可以单击每个联系人转到他们的详细信息视图。然而,在返回列表时,同样的20个联系人再次添加。我知道onCreate()代码只是再次执行,但我不确定如何解决这个问题Android-每次加载MainActivity时都复制ListView对象,android,listview,Android,Listview,我在一个列表中有20个联系人,您可以单击每个联系人转到他们的详细信息视图。然而,在返回列表时,同样的20个联系人再次添加。我知道onCreate()代码只是再次执行,但我不确定如何解决这个问题 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //ArrayList<contact> contacts = new ArrayList<
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ArrayList<contact> contacts = new ArrayList<contact>();
BufferedReader br = null;
try {
String s;
InputStream stream = getAssets().open("contacts.json");
br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuilder sb = new StringBuilder();
while((s = br.readLine()) != null) {
sb.append(s);
}
s = sb.toString();
JSONArray ja = new JSONArray(s);
for (int i = 0; i < ja.length(); i++) {
JSONObject obj = ja.getJSONObject(i);
contact c = new contact(); // Creates an empty contact object
phone p = new phone(); // Creates an empty phone object
// Retrieves contact information from JSONObject
String detailsURL = obj.getString("detailsURL");
String name = obj.getString("name");
int employeeId = obj.getInt("employeeId");
String company = obj.getString("company");
String imageURL = obj.getString("smallImageURL");
long birthdate = obj.getLong("birthdate");
JSONObject phone = obj.getJSONObject("phone");
String workPhone = phone.getString("work");
String homePhone = phone.getString("home");
if(phone.has("mobile")){
String mobilePhone = phone.getString("mobile");
p.setMobilePhone(mobilePhone);
}
//Sets contact values to retrieved JSON data and adds to the ArrayList
c.setName(name);
c.setEmployeeId(employeeId);
c.setCompany(company);
c.setImageURL(imageURL);
c.setBirthdate(birthdate);
p.setWorkPhone(workPhone);
p.setHomePhone(homePhone);
c.setPhone(p);
c.setDetailsURL(detailsURL);
contacts.add(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("contact", "file");
} catch (IOException e) {
e.printStackTrace();
Log.d("contact", "IO");
}
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
myAdapter adapter = new myAdapter(this, contacts);
list.setAdapter((ListAdapter) adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Intent intent = new Intent(MainActivity.this, DetailView.class);
intent.putExtra("contact", i);
startActivity(intent);
}
});
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//ArrayList联系人=新建ArrayList();
BufferedReader br=null;
试一试{
字符串s;
InputStream=getAssets().open(“contacts.json”);
br=新的BufferedReader(新的InputStreamReader(流,“UTF-8”);
StringBuilder sb=新的StringBuilder();
而((s=br.readLine())!=null){
某人追加;
}
s=sb.toString();
JSONArray ja=新JSONArray;
对于(int i=0;i
只需插入一个检查是否已填充的布尔
控制器,跳过填充列表视图的代码段即可
boolean listViewPopulated = false;
if (!listViewPopulated) {
// Populate your ListView
...
listViewPopulated = true;
}
----编辑---
试着这样做:
class YourClass extends WhatEver {
boolean listViewPopulated = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!listViewPopulated) {
//ArrayList<contact> contacts = new ArrayList<contact>();
BufferedReader br = null;
try {
String s;
InputStream stream = getAssets().open("contacts.json");
br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuilder sb = new StringBuilder();
while((s = br.readLine()) != null) {
sb.append(s);
}
s = sb.toString();
JSONArray ja = new JSONArray(s);
for (int i = 0; i < ja.length(); i++) {
JSONObject obj = ja.getJSONObject(i);
contact c = new contact(); // Creates an empty contact object
phone p = new phone(); // Creates an empty phone object
// Retrieves contact information from JSONObject
String detailsURL = obj.getString("detailsURL");
String name = obj.getString("name");
int employeeId = obj.getInt("employeeId");
String company = obj.getString("company");
String imageURL = obj.getString("smallImageURL");
long birthdate = obj.getLong("birthdate");
JSONObject phone = obj.getJSONObject("phone");
String workPhone = phone.getString("work");
String homePhone = phone.getString("home");
if(phone.has("mobile")){
String mobilePhone = phone.getString("mobile");
p.setMobilePhone(mobilePhone);
}
//Sets contact values to retrieved JSON data and adds to the ArrayList
c.setName(name);
c.setEmployeeId(employeeId);
c.setCompany(company);
c.setImageURL(imageURL);
c.setBirthdate(birthdate);
p.setWorkPhone(workPhone);
p.setHomePhone(homePhone);
c.setPhone(p);
c.setDetailsURL(detailsURL);
contacts.add(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("contact", "file");
} catch (IOException e) {
e.printStackTrace();
Log.d("contact", "IO");
}
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
myAdapter adapter = new myAdapter(this, contacts);
list.setAdapter((ListAdapter) adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Intent intent = new Intent(MainActivity.this, DetailView.class);
intent.putExtra("contact", i);
startActivity(intent);
}
});
}
listViewPopulated = true;
}
}
class你的类扩展了什么{
布尔listViewPopulated=false;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(!listViewPopulated){
//ArrayList联系人=新建ArrayList();
BufferedReader br=null;
试一试{
字符串s;
InputStream=getAssets().open(“contacts.json”);
br=新的BufferedReader(新的InputStreamReader(流,“UTF-8”);
StringBuilder sb=新的StringBuilder();
而((s=br.readLine())!=null){
某人追加;
}
s=sb.toString();
JSONArray ja=新JSONArray;
对于(int i=0;iif (contacts.isEmpty())
try {
...
}