我是Android开发的新手。我已经实现了一个将Json数据加载到listview的活动。我现在有两个活动。第一个是登录Activity。第二个是加载列表视图活动.json数据正在加载完美。但我的问题是,当我按下后退按钮时,列表视图数据逐个删除。我不知道问题出在哪里。
这是我的主要活动
public class MainActivity extends Activity {
ListView list;
TextView ver;
TextView name;
TextView api;
ImageButton login;
public static ArrayList<HashMap<String, String>> oslist;
//URL to get JSON Array
private static String url = "http://api.learn2crack.com/android/jsonos/";
//JSON Node Names
static final String TAG_OS = "android";
static final String TAG_VER = "ver";
static final String TAG_NAME = "name";
static final String TAG_API = "api";
JSONArray android = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loging);
oslist = new ArrayList<HashMap<String, String>>();
login = (ImageButton)findViewById(R.id.imageButton1);
new ArrayList<HashMap<String, String>>();
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
ver = (TextView)findViewById(R.id.title);
name = (TextView)findViewById(R.id.artist);
api = (TextView)findViewById(R.id.time);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
JsonParser jParser = new JsonParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
// TODO Auto-generated method stub
pDialog.dismiss();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String ver = c.getString(TAG_VER);
String name = c.getString(TAG_NAME);
String api = c.getString(TAG_API);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_VER, ver);
map.put(TAG_NAME, name);
map.put(TAG_API, api);
oslist.add(map);
Intent reult = new Intent(MainActivity.this,ViewList.class);
//reult.putStringArrayListExtra("map", oslist);
//startActivity(reult);
reult.putExtra("arraylist", oslist);
startActivityForResult(reult, 500);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
这是我的Listview活动
public class ViewList extends Activity {
private static final String TAG_VER = "ver";
private static final String TAG_NAME = "name";
private static final String TAG_API = "api";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Intent getres = getIntent();
//HashMap<String, String> hashMap = (HashMap<String, String>)getres.getSerializableExtra("map");
final ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
ListView list=(ListView)findViewById(R.id.list);
/* ListAdapter adapter = new SimpleAdapter(ViewList.this, arl,
R.layout.row,
new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
R.id.title,R.id.artist, R.id.time});*/
NewsRowAdapter adapter = new NewsRowAdapter(ViewList.this, R.layout.row, arl);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ViewList.this, "You Clicked at "+arl.get(+position).get("name"), Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的适配器类
public class NewsRowAdapter extends BaseAdapter {
/*//ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
//ArrayList<HashMap<String, String>> data;
private List<Item> items;
//private Item objBean;
private int row;*/
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/*public NewsRowAdapter(Activity act, int resource, HashMap<String, List<String>> listChildData)) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.data = arrayList;
}*/
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
TextView title = (TextView) vi.findViewById(R.id.title);
TextView artist = (TextView) vi.findViewById(R.id.artist);
TextView time = (TextView) vi.findViewById(R.id.time);
ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
title.setText(song.get(MainActivity.TAG_VER));
artist.setText(song.get(MainActivity.TAG_NAME));
time.setText(song.get(MainActivity.TAG_API));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);
Button accept = (Button) vi.findViewById(R.id.btnaccept);
accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//CustomizedListView getlist = new CustomizedListView();
final int x = (int) getItemId(position);
Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();
}
});
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();
}
});
return vi;
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int possision) {
// TODO Auto-generated method stub
return possision;
}
@Override
public long getItemId(int possision) {
// TODO Auto-generated method stub
return possision;
}
}
请某人帮助我......
答案 0 :(得分:0)
是的,它会这样做,当您按下后退按钮时,您的活动就会完成,因此活动中的视图将被删除,您必须将数据保存在单独的类中,外部活动或保持活动活着。