2017-08-14 5 views
0

これは私がアダプタを設定しているnotifydatasetchanged()メソッドを呼び出す私のメインクラスです。目的はリストビュー内のユーザーの履歴を表示し、データが変更されたときに更新します述べたように、エラー以前listview notifyDataSetChanged()メソッドにエラーがあります

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; 
} 

}

私がミスをしたところ 任意のヘルプは参考になり得ていないのです

....

答えて

1

ちょっと男私は(任意のnotifydatasetchangedを見ていない)あなたのコードでは、私はちょうどアダプタを参照してください。 解決策==> adapter.notifydatasetchanged();この

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

      historyList.add(historyItems); 
     } 
adapter.notifyDataSetChanged(); 

希望のように...ではない、あなたのアダプタのnotifyDataSetChanged()を与えなければならない方法

1

外でキャッチした後

を配置する必要があり、それはあなたを助ける:)

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(); 

      } 
関連する問題