2016-08-29 2 views
0

첫 번째 위치에서 새 항목을 gridview에 어떻게 추가합니까? 그 후 첫 번째 위치에서 gridview에 새 항목을 추가하는 방법은 무엇입니까?

protected void showList() { 
    try { 
     JSONObject jsonObj = new JSONObject(myJSON); 
     peoples = jsonObj.getJSONArray(TAG_RESULTS); 

     for (int i = 0; i < peoples.length(); i++) { 
      JSONObject c = peoples.getJSONObject(i); 
      String userid = c.getString(Config.KEY_USERID); 
      String thumburl = c.getString(Config.KEY_THUMBURL); 

      HashMap<String, String> persons = new HashMap<String, String>(); 
      persons.put(Config.KEY_USERID, userid); 
      persons.put(Config.KEY_THUMBURL, thumburl); 

      personList.add(persons); 

     } 

     GridView listview = (GridView) findViewById(R.id.gridview); 
     adapter = new ListViewAdapter(NewRetriveData.this, personList); 
     listview.setAdapter(adapter); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

나는 내가 동적으로의 GridView 1 위치에 배열 항목 쇼를 추가하고 싶지는 ArrayList의

GridView listview = (GridView) findViewById(R.id.gridview); 
     adapter = new ListViewAdapter(NewRetriveData.this, personList); 
     listview.setAdapter(null); 
     listview.setAdapter(adapter); 

를 다시로드하고 동적으로 그 후

HashMap<String, String> persons = new HashMap<String, String>(); 
      persons.put(Config.KEY_USERID, "sweety"); 
      persons.put(Config.KEY_THUMBURL, "www.sweety.com/hello.jpg"); 

을 위해 ArrayList 새로운 항목을 추가하고있다.

+0

에서 그것을 사용할 수 있습니다. –

+0

http://stackoverflow.com/questions/20651946/how-to-set-the-first-item-in-gridview-with-default-value-and-populate-the-rest-o 여기를 참조하십시오. –

+0

내 대답 확인 . –

답변

0

먼저 해시 맵을 전역 적으로 선언하십시오. 그런 다음 루프를 시작하기 전에 첫 번째 원시 데이터를 추가하십시오. 그건 그렇습니다.

0

는 어댑터

public class CoustomAdapter extends BaseAdapter { 

     Context myContext; 
     private static LayoutInflater inflater = null; 
     ArrayList<HashMap<String, String>> data; 

     public CoustomAdapter(Context context, ArrayList<HashMap<String, String>> data) { 
      myContext = context; 
      this.data = data; 
      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     void addItem(HashMap<String, String> persons) { 
      data.add(0, persons); // 0 for add it in starting index 
      notifyDataSetChanged(); 
     } 
    ... 

} 

addItem()을 만들어 그리드 항목을 반전하지 첫 번째 위치에 새 항목을 추가 할 활동

HashMap<String, String> persons = new HashMap<String, String>(); 
     persons.put(Config.KEY_USERID, userid); 
     persons.put(Config.KEY_THUMBURL, thumburl); 

     adapter.addItem(persons); 
관련 문제