2016-10-07 56 views
0

我試圖用自定義適配器來填充ListView。
我想使用NotifyDatasetChanged刷新佈局,但它不起作用。
我從HTTP請求檢索一些JSON數據,然後我操縱結果字符串,然後填充ListViewNotifyDatasetChanged無法在自定義適配器上工作

我的代碼有什麼問題? 更新代碼(對於諮詢)

public class CalendarioFragment extends Fragment { 
    ListView listView; 
    ArrayList<Calendario> calenList; 
    CalendarioAdapter adapter; 
    String json; 
    ArrayList<String> arrayId = new ArrayList<String>(); 
    ArrayList<String> arrayData = new ArrayList<String>(); 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.calendario_layout, null); 

     listView = (ListView) view.findViewById(R.id.listCale); 
     listView.setEmptyView(view.findViewById(R.id.emptyElement)); 

     calenList = new ArrayList<Calendario>(); 
     adapter = new CalendarioAdapter(getActivity(), calenList); 
     listView.setAdapter(adapter); 

     execQuery("query", "0"); 

     return view; 
    } 

    private void execQuery(final String query, final String taskId) { 
     final MyAsyncTask asyncTask = new MyAsyncTask(new AsyncResponse() { 
      @Override 
      public void onTaskCompleted(String output) { 

       if (output == null) { 
        Toast.makeText(getActivity(), "Nessuna connessione internet attiva!", Toast.LENGTH_SHORT).show(); 
       } else { 
        json = output; 

        try { 
         ParseJson(); 
         calenList.clear(); 

         String[] ids = arrayId.toArray(new String[arrayId.size()]); 
         String[] date = arrayData.toArray(new String[arrayData.size()]); 

         for (int i = 0; i < ids.length; i++) { 
          Calendario calendario = new Calendario(); 
          calendario.setId(ids[i]); 
          calendario.setData(date[i]); 

          calenList.add(calendario); 
          adapter.updateData(calenList); 
         } 

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

     asyncTask.execute(query, taskId); 
    } 

    private void ParseJson() throws JSONException { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray jsonArray = jsonObject.getJSONArray("risposta"); 

     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject JO = jsonArray.getJSONObject(i); 
      arrayId.add(JO.getString("ID")); 
      arrayData.add(JO.getString("DATA")); 
     } 
    } 
} 

這是CustomAdapterCode:

import android.content.Context; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class CalendarioAdapter extends BaseAdapter { 
    private ArrayList listData; 
    private LayoutInflater layoutInflater; 
    final static String TAG = "sb.dl"; 

    public CalendarioAdapter(Context context, ArrayList listData) { 
     Log.d(TAG, "CalendarioAdapter"); 
     this.listData = listData; 
     layoutInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public int getCount() { 
     return listData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d(TAG, "CalendarioAdapter.getView"); 
     ViewHolder holder; 

     if (convertView == null) { 
      convertView = layoutInflater.inflate(R.layout.calendario_row_layout, null); 
      holder = new ViewHolder(); 
      holder.edId = (TextView) convertView.findViewById(R.id.edId); 
      holder.edData = (TextView) convertView.findViewById(R.id.edData); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Calendario newsItem = (Calendario) listData.get(position); 
     holder.edId.setText(newsItem.getId()); 
     holder.edData.setText(newsItem.getData()); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView edId; 
     TextView edData; 
    } 

public void updateData(ArrayList<Calendario> updatedData) { 
    listData = updatedData; 
    this.notifyDataSetChanged(); 
} 
} 
+0

那麼你總是使用相同的參數處理程序做等什麼,如果數據是一樣的? – vilpe89

+0

請添加CalendarioAdapter的代碼 –

+0

How?我不能在評論 – Simone

回答

0

問題:

listDataCale類的內部的變量。當您爲其設置新值時,Cale類中的變量正在被修改。數據副本的其餘部分設置未被更改。換句話說,當您修改Cale類中的listData時,CalendarioAdapter中的數據不會更新。

解決方案:

您需要將它更改後更新listData再次適配器。爲此,您需要在CalendarioAdapter適配器內部創建一個方法,該方法將負責更新適配器內部的數據,然後調用notifyDataSetChanged()

此方法添加到您的CalendarioAdapter適配器:

public void updateData(ArrayList<Calendario> updatedData) { 
    listDataInYourAdapter = newData; 
    this.notifyDataSetChanged(); 
} 

我們使用這種方法,在你的Cale類替換此:

adapter.notifyDataSetChanged(); 

與此:

updateData(listData); 
+0

@Simone我的回答對你有幫助嗎? – Marat

0

後這三條線

listData = new ArrayList<Calendario>(); 
adapter = new CalendarioAdapter(getActivity(), listData); 
listView.setAdapter(adapter); 

你永遠不應該重新分配listData。一旦完成,適配器中的列表引用就會脫離,並且不再可以通知更新。

所以,你需要clear(),然後addAll()的名單上。

例如,

ParseJson(); // You should really pass 'output' as a parameter here 
listData = getListData(); // Don't re-assign 

而是執行此操作,然後通知

ParseJson(); 
listData.clear(); 
listData.addAll(getListData()); 

或者,爲什麼叫listData.addAll(getListData());?你已經有了這個方法,它可以做正確的清理,添加和通知。

private ArrayList<Calendario> getListData() { 

    listData.clear(); // Cleared 
    String[] ids = arrayId.toArray(new String[arrayId.size()]); 
    String[] date = arrayData.toArray(new String[arrayData.size()]); 

    for (int i = 0; i < ids.length; i++) { 
     Calendario calendario = new Calendario(); 
     calendario.setId(ids[i]); 
     calendario.setData(date[i]); 

     listData.add(calendario); // Adding 
    } 

    adapter.notifyDataSetChanged(); // Notify 

    return listData; 
} 

所以,實際上,你只需要在的AsyncTask這兩行(並再次,應加outputParseJson參數,你並不需要保存json = output)。

ParseJson(); 
getListData(); 
+0

對不起..總是一樣的結果:( – Simone

+0

我不確定你是否完全按照我的答案。我看到在更新中你仍然有'listData = updatedData;' –

+0

我解決了..謝謝 – Simone

0

我解決了! 這是一個愚蠢的事情!

在parseJson子,我需要清除數組之前填補他們!

private void ParseJson() throws JSONException { 
    JSONObject jsonObject = new JSONObject(json); 
    JSONArray jsonArray = jsonObject.getJSONArray("risposta"); 

    arrayId.clear(); 
    arrayData.clear(); 

     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject JO = jsonArray.getJSONObject(i); 
      arrayId.add(JO.getString("ID")); 
      arrayData.add(JO.getString("DATA")); 
     } 
    } 
0

重新分配列表 - >這不是建議的行爲。 這是正確的做法是如何做到這一點: 適配器內部:public void update(List<ItemMessage> updatedList){ list.clear(); list.addAll(updatedList); notifyDataSetChanged();}

  • 不要忘記使用
相關問題