2013-11-22 1 views
0

저는 안드로이드 개발을 처음 사용했습니다. Json 데이터를 listview에로드하는 작업을 구현했습니다. 나는 지금 두 가지 활동을하고있다. 첫 번째는 로그인 활동입니다. 두 번째는 목록보기 활동을로드합니다 .Json 데이터가 완벽하게로드됩니다. 하지만 내 문제는, 내가 다시 버튼을 누르면, 목록보기 데이터가 하나씩 제거됩니다. 나는 문제가 어디 있는지 전혀 모른다. 여기 목록보기 항목은 뒤로 버튼이 안드로이드 눌렀을 때 삭제되었습니다

는이

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

어떤 사람이 나를 도와주세요 ...

내 어댑터 클래스 내 목록보기 활동

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

     } 
    } 
    } 

입니다

답변

0

예 예 w 그렇게하면, 뒤로 버튼을 누르면 활동이 끝나게되므로 활동 내부의보기가 삭제됩니다. 활동을 벗어난 별도의 수업이나 데이터 활동을 유지해야합니다.

+0

안녕 arju, 어떻게 할 수있는 몇 가지 예를주세요 ... 제발 :) –

+0

누르면 다시 다른 활동으로 이동하거나 응용 프로그램을 종료 –

+0

이전 활동에 활동으로 이동해야합니다. 하지만 이전 프로젝트에 자동으로 추가되기 때문에 코드를 작성하지 않았습니다. –

관련 문제