2016-08-29 44 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(); 
    } 

} 

私はその後、私は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に新しい項目を追加してい私は、GridViewの第一位で動的に追加配列項目のショーをしたいです。

+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); 
関連する問題