2013-11-22 69 views
0

我是新来的android开发。我已经实现了一个将Json数据加载到列表视图的活动。我现在有两项活动。第一个是登录Activity。第二个是加载列表视图activity。json数据加载完美。但我的问题是,当我按下按钮,列表视图数据逐一删除。我不知道问题出在哪里。当后退按钮被删除时列表视图项被删除Android

这里是我的主要活动

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

     } 
    } 
    } 

这里是我的列表视图活动

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

是的它w如果你按下后退按钮,你的活动就完成了,所以活动中的视图将被删除,或者你必须将数据保存在一个单独的类中,在活动之外或者使活动保持活动状态。

+0

hi arju,你能给我举一些例子怎么做吗......请:) –

+0

回来后,你要转移到其他活动或退出应用程序 –

+0

它应该转移到前一活动的活动。但我没有编码,因为它会自动附加到我以前的项目。 –

相关问题