您好我正在处理像{tinder}这样的CardSwipe
功能。 (有关理解,请参阅附图)
对于此我跟随this code,当使用像bellow这样的静态数据时,我已成功添加ListView
private void loadCards2(){
itemsaArrayList = new ArrayList<>();
for(int i = 0; i < 10; i++){
CardSwipeItems items = new CardSwipeItems();
items.setCardImageUrl("http://i.ytimg.com/vi/PnxsTxV8y3g/maxresdefault.jpg");
items.setCardDescription("But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness.");
itemsaArrayList.add(items);
}
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
myAppAdapter = new CardSwipeAdapter(itemsaArrayList, CardSwipeActivity.this);
flingContainer.setAdapter(myAppAdapter);
initControlls();
}
但是当我尝试使用排球方式动态添加项目时,ListView
中的项目不会添加。 (对于这个凌空
请参阅loadCards()
)
CardSwipeActivity
方法
我尝试了很多方法来动态加载列表视图中的项目。例如:我使用Thread
(有关代码,请参阅this),我也使用Handler
(有关代码,请参阅this),但我无法添加ListView
动态
如果有人知道解决方法,请告诉我。谢谢.......
修改
*My method*
private void loadCards(){
String Url = "myApi";
Log.e("Url", Url);
final ProgressDialog dialog = ProgressDialog.show(CardSwipeActivity.this, null, null);
ProgressBar spinner = new android.widget.ProgressBar(CardSwipeActivity.this, null,android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(spinner);
dialog.setCancelable(false);
dialog.show();
StringRequest request = new StringRequest(Request.Method.GET, Url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
dialog.dismiss();
if(response != null && !response.startsWith("<HTML>")){
Log.e("OnResponse", response);
try {
JSONArray jsonArray = new JSONArray(response);
if(jsonArray.length() > 0){
itemsaArrayList = new ArrayList<>();
for(int i = 0; i< jsonArray.length(); i++){
JSONObject singleObj = jsonArray.getJSONObject(i);
String imgId = singleObj.getString("avatar_file_id");
String name = singleObj.getString("name");
CardSwipeItems items = new CardSwipeItems();
Log.e("imgId", imgId);
Log.e("name", name);
items.setCardImageUrl(imgId);
items.setCardDescription(name);
itemsaArrayList.add(items);
}
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
myAppAdapter = new CardSwipeAdapter(itemsaArrayList, CardSwipeActivity.this);
flingContainer.setAdapter(myAppAdapter);
initControlls();
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
Log.e("Internet", "Internet");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
if(error != null){
Log.e("error", error.toString());
}else{
}
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("token","b32daf7b50c7f21dba80dd0651174e3839c22f56");
params.put("user_id","386");
Log.e("Headers", "**********Headers*************");
Log.e("token","b32daf7b50c7f21dba80dd0651174e3839c22f56");
Log.e("user_id","386");
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(CardSwipeActivity.this);
queue.add(request);
queue.getCache().remove(Url);
}
答案 0 :(得分:1)
您可以通过将项目添加到最初传递给适配器的列表中,将项目动态添加到适配器,如下所示:
itemsaArrayList.add(items);
需要从UI线程通知适配器:
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
要在UI-Thread上调用它,使用必须使用Activity的runOnUiThread()。然后,notifyDataSetChanged()将起作用。
另请查看此post
答案 1 :(得分:0)
如果您从服务器获得响应,请尝试从onResponse方法转移到onCreate方法下面。
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
希望这会起作用