in listview notifyDataSetChanged() 方法e 有错误



这是我的主要类,我在其中设置适配器并调用 notifydatasetchanged(( 方法的目的是在 listView 中显示用户的历史记录并在数据更改时刷新,但如前所述出现错误

public class HistoryActivity extends AppCompatActivity {
// Log tag
private static final String TAG = HistoryActivity.class.getSimpleName();
private static final String url ="http://192.168.0.102/android_login_api/historyfetch.php";
private ProgressDialog pDialog;
private List<HistoryItems> historyList = new ArrayList<HistoryItems>();
private ListView listView;
private CustomListAdapter adapter;
private SQLiteHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, historyList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> user = db.getUserDetails();
String user_email = user.get("email");
gethistory(user_email);
}
private void gethistory(final String email)
{
StringRequest stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG,response.toString());
hideDialog();
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
historyItems.setName(obj.getString("user_email"));
historyItems.setLat_from(obj.getDouble("latitude_from"));
historyItems.setLon_from(obj.getDouble("longitude_from"));
historyItems.setLat_to(obj.getDouble("latitude_to"));
historyItems.setLon_to(obj.getDouble("longitude"));
historyItems.setDate(obj.getString("created_at"));
historyList.add(historyItems);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter;
})
{
protected Map<String,String>getParams(){
Map<String, String> params=new HashMap<>();
params.put("user_email",email);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}

}

这是我的自定义适配器:-

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<HistoryItems> historyItems;

public CustomListAdapter(Activity activity, List<HistoryItems> historyItems) {
this.activity = activity;
this.historyItems = historyItems;
}
@Override
public int getCount() {
return historyItems.size();
}
@Override
public Object getItem(int location) {
return historyItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);

TextView user_email = (TextView) convertView.findViewById(R.id.tv_user_email);
TextView lat_from = (TextView) convertView.findViewById(R.id.tv_lat_from);
TextView lon_from = (TextView) convertView.findViewById(R.id.tv_lon_from);
TextView lat_to = (TextView) convertView.findViewById(R.id.tv_lat_to);
TextView lon_to = (TextView) convertView.findViewById(R.id.tv_lon_to);
TextView date = (TextView) convertView.findViewById(R.id.tv_date);
// getting billionaires data for the row
HistoryItems m = historyItems.get(position);

// name
user_email.setText(m.getUser_email());
//
lat_from.setText(String.valueOf(m.getLat_from()));

lon_from.setText(String.valueOf(m.getLon_from()));
lat_to.setText(String.valueOf(m.getLat_to()));
lon_to.setText(String.valueOf(m.getLon_to()));
date.setText(String.valueOf(m.getDate()));

return convertView;
}

}

我没有到达我犯错的地方 任何帮助都会有所帮助。

嘿伙计,我没有在您的代码中看到任何通知数据集更改((,我只看到适配器; 解决方案 ==> adapter.notifydatasetchanged((;应放在捕获后而不是方法之外

你必须为你的适配器提供通知DataSetChanged(( ...

喜欢这个

for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
.
.
.
.
historyList.add(historyItems);
}
adapter.notifyDataSetChanged();

希望,它会帮助你:)

每次添加新项适配器时,都会在列表视图中添加新项。

for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
historyItems.setName(obj.getString("user_email"));
historyItems.setLat_from(obj.getDouble("latitude_from"));
historyItems.setLon_from(obj.getDouble("longitude_from"));
historyItems.setLat_to(obj.getDouble("latitude_to"));
historyItems.setLon_to(obj.getDouble("longitude"));
historyItems.setDate(obj.getString("created_at"));
historyList.add(historyItems);
adapter.notifyDataSetChanged();
}

最新更新