这是我的主要类,我设置适配器并调用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;
}
}
我没有得到我犯错误的地方 任何帮助都会有所帮助....
答案 0 :(得分:1)
答案 1 :(得分:1)
你必须为你的适配器提供notifyDataSetChanged()......
像这样 for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
.
.
.
.
historyList.add(historyItems);
}
adapter.notifyDataSetChanged();
希望,它会帮助你:)。
答案 2 :(得分:0)
每次添加新项目适配器都会在列表视图中添加新项目。
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();
}