我正在填充线性布局,其中包含从API获取的一些数据。每次执行代码时,我都会在我的模型类中保存数据,但之后不会将视图添加到线性布局中。
我在tabLayout中使用的另一个选项卡使用相同的代码,它正在工作,但为此它不起作用。我也在这里附上我的调试器截图。下面是片段的代码,它正在发生。
Sent.java
package fragments;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mranuran.pehchankaun.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import fr.castorflex.android.smoothprogressbar.SmoothProgressBar;
import models.SentMessage;
import utils.SharedPrefManager;
public class Sent extends android.app.Fragment implements SwipeRefreshLayout.OnRefreshListener {
LinearLayout msgHolder,sentLinearLayout;
LayoutInflater inflater;
SmoothProgressBar progressBar;
SwipeRefreshLayout swipeRefreshLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.sent,container,false);
sentLinearLayout=(LinearLayout)view.findViewById(R.id.sentLinearLayout);
msgHolder=(LinearLayout)view.findViewById(R.id.sentMsgHolder);
progressBar=(SmoothProgressBar)view.findViewById(R.id.progressBar);
swipeRefreshLayout=(SwipeRefreshLayout)view.findViewById(R.id.swipe_refresh_layout);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
populateSent();
}
});
}
private void populateSent() {
msgHolder.removeAllViews();
progressBar.setVisibility(View.VISIBLE);
progressBar.progressiveStart();
inflater=(LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RequestQueue rq= Volley.newRequestQueue(getActivity());
StringRequest sr=new StringRequest(Request.Method.POST, "http://mranuran.com/pehchan_kaun/getSent.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject json=new JSONObject(response);
JSONArray userArray=json.optJSONArray("messages");
if(userArray.length()>0){
SentMessage msg;
View v;
for(int i=0;i<userArray.length();i++){
JSONObject singleMsgObj=userArray.getJSONObject(i);
msg=new SentMessage(singleMsgObj.getString("id"),singleMsgObj.getString("message"),singleMsgObj.getString("creation_date"),singleMsgObj.getString("receiver_name"));
v=inflater.inflate(R.layout.sent_msg_single_row,null,false);
TextView creationDate=(TextView) v.findViewById(R.id.creationDate);
TextView messageTV=(TextView)v.findViewById(R.id.msg);
TextView receivername=(TextView)v.findViewById(R.id.receiverName);
creationDate.setText(msg.getCreation_date());
messageTV.setText(msg.getMessage());
receivername.setText(msg.getReceiver_name());
msgHolder.addView(v);
}
}else{
Snackbar.make(sentLinearLayout,"You have not sent any messages to anyone.",Snackbar.LENGTH_SHORT).show();
progressBar.progressiveStop();
swipeRefreshLayout.setRefreshing(false);
return;
}
progressBar.progressiveStop();
swipeRefreshLayout.setRefreshing(false);
} catch (JSONException e) {
e.printStackTrace();
progressBar.progressiveStop();
swipeRefreshLayout.setRefreshing(false);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Snackbar.make(sentLinearLayout,"Something went wrong.Try again",Snackbar.LENGTH_SHORT).show();
progressBar.progressiveStop();
swipeRefreshLayout.setRefreshing(false);
return;
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<>();
map.put("user_id",new SharedPrefManager(getActivity()).getUserID());
return map;
}
};
rq.add(sr);
}
@Override
public void onRefresh() {
populateSent();
}
}
答案 0 :(得分:0)
添加requestLayout
进行更新
msgHolder.addView(v);
msgHolder.requestLayout();