我試圖用自定義適配器來填充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();
}
}
那麼你總是使用相同的參數處理程序做等什麼,如果數據是一樣的? – vilpe89
請添加CalendarioAdapter的代碼 –
How?我不能在評論 – Simone