我已经按照我在网上找到的所有步骤创建了一个程序。它的作用是如果我有连接它将数据发送到远程服务器和服务器响应OK如果数据输入成功。我的应用程序在将数据保存到服务器端时工作正常,但它不会显示在我的recyclerview上。但是当我切换到飞行模式并尝试将数据发送到服务器时,它会在recyclerview上显示它失败但是当它成功时,它不会显示在recyclerview上。也许你们可以提供帮助。我似乎无法找到错误。
主要活动
public class SMSActivity extends AppCompatActivity {
RecyclerView smsRecyclerview;
EditText Smsplace;
RecyclerView.LayoutManager smsLayoutmanager;
SMSAdapter smsAdapter;
ArrayList<SMS> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
smsRecyclerview = (RecyclerView)findViewById(R.id.smsRecyclerView);
Smsplace = (EditText)findViewById(R.id.smsplace);
smsLayoutmanager = new LinearLayoutManager(this);
smsRecyclerview.setLayoutManager(smsLayoutmanager);
smsRecyclerview.setHasFixedSize(true);
smsAdapter = new SMSAdapter(arrayList);
smsRecyclerview.setAdapter(smsAdapter);
readFromLocalStorage();
}
public void submitPlace(View view)
{
String smsplace = Smsplace.getText().toString();
saveToAppServer(smsplace);
Smsplace.setText("");
}
private void readFromLocalStorage()
{
arrayList.clear();
SMSHelper smsHelper = new SMSHelper(this);
SQLiteDatabase database = smsHelper.getReadableDatabase();
Cursor cursor = smsHelper.readFromLocalDatabase(database);
while (cursor.moveToNext())
{
String smsplace = cursor.getString(cursor.getColumnIndex(SMSContract.SMSPLACE));
int smssync_status = cursor.getInt(cursor.getColumnIndex(SMSContract.SMSSYNC_STATUS));
arrayList.add(new SMS(smsplace,smssync_status));
}
smsAdapter.notifyDataSetChanged();
cursor.close();
}
private void saveToAppServer(final String smsplace)
{
if(checkNetworkConnection())
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, SMSContract.SERVER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String Response = jsonObject.getString("response");
if(Response.equals("OK"))
{
saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_OK);
}
else
{
saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_FAILED);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_FAILED);
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("smsplace",smsplace);
return params;
}
}
;
SMSSingleton.getInstance(SMSActivity.this).addToRequestQue(stringRequest);
}
else
{
saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_FAILED);
}
}
public boolean checkNetworkConnection()
{
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
private void saveToLocalStorage(String smsplace,int smssync)
{
SMSHelper smsHelper = new SMSHelper(this);
SQLiteDatabase database = smsHelper.getWritableDatabase();
smsHelper.saveToLocalDatabase(smsplace,smssync,database);
readFromLocalStorage();
}
}
RecyclerAdapter
public class SMSAdapter extends RecyclerView.Adapter<SMSAdapter.MyViewHolder> {
private ArrayList<SMS> arrayList = new ArrayList<>();
public SMSAdapter(ArrayList<SMS> arrayList)
{
this.arrayList = arrayList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.smsitem_view,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.Smsplace.setText(arrayList.get(position).getSmsplace());
int smssync_status = arrayList.get(position).getSmsSync_status();
if(smssync_status == SMSContract.SMSSYNC_STATUS_OK)
{
holder.Smssync_status.setImageResource(R.drawable.ok);
}
else
{
holder.Smssync_status.setImageResource(R.drawable.sync);
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder
{
ImageView Smssync_status;
TextView Smsplace;
public MyViewHolder(View itemView) {
super(itemView);
Smssync_status = (ImageView)itemView.findViewById(R.id.smsImgsync);
Smsplace = (TextView)itemView.findViewById(R.id.txtSmsplace);
}
}
}
*** P.S。我没有包含sqlitedatabase代码。但如果你们需要它。我很高兴。