2017-10-22 9 views
2

私はアンドロイドアプリケーションで作業しています。アプリケーションを実行するとリサイクルビューに何も表示されませんが、デバッグモードでアプリケーションをチェックすると、リサイクルビューでデータが正常に表示されます。私は自分のWebサービスが正常に動作していることを確認し、データを正常に渡しました。どうすればこれを達成できますか?Recyclviewはデバッグモード中にデータを表示していますが、noramlで動作していません

ManageQuestionActivity、Javaの

public class ManageQuestionActivity extends AppCompatActivity implements RecyclerView.OnScrollChangeListener{ 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private RecyclerView listView; 
    private RecyclerView.LayoutManager layoutManager; 
    private RecyclerView.Adapter adapter; 
    private QuestionsListAdapter listAdapter; 
    private List<QuestionsItem> timeLineItems; 
    private int requestCount = 1; 
    private ProgressDialog pDialog; 
    public static String id, message, token, encodedString; 
    int pageCount, totalPages; 
    SQLiteHandler db; 
    SessionManager session; 
    ConnectionDetector cd; 
    EditText edtSearch; 
    Boolean isInternetPresent = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_manage_question); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     listView = (RecyclerView) findViewById(R.id.list); 
     edtSearch = (EditText) findViewById(R.id.edtSearch); 
     listView.setHasFixedSize(true); 
     layoutManager = new LinearLayoutManager(this); 
     listView.setLayoutManager(layoutManager); 
     //Adding an scroll change listener to recyclerview 
     listView.setOnScrollChangeListener(this); 

     // Progress dialog 
     pDialog = new ProgressDialog(this); 
     pDialog.setCancelable(false); 

     cd = new ConnectionDetector(this); 
     isInternetPresent = cd.isConnectingToInternet(); 

     db = new SQLiteHandler(this); 

     // session manager 
     session = new SessionManager(this); 

     // Fetching user details from sqlite 
     HashMap<String, String> user = db.getUserDetails(); 
     id = user.get("id"); 
     token = user.get("token"); 

     getData(); 

     timeLineItems = new ArrayList<>(); 

     adapter = new QuestionsListAdapter(timeLineItems, this); 
     listView.setAdapter(adapter); 

    } 

    public void getTimeLineData(final String token, final String page) { 


     String tag_string_req = "req_register"; 
     // making fresh volley request and getting json 
     StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.questions, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 
       VolleyLog.d(TAG, "Response: " + response.toString()); 
       if (response != null) { 
        try { 
         JSONObject jObj = new JSONObject(response); 
         boolean error = jObj.getBoolean("status"); 
         String message = jObj.getString("message"); 
         if (error) { 
          totalPages = jObj.getInt("totalPages"); 
          pageCount = jObj.getInt("page"); 

          int limit = jObj.getInt("limit"); 
          parseJsonFeed(response); 
         } 

        } catch (Exception e) { 

        } 

       } 
      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
      } 
     }) { 

      @Override 
      protected Map<String, String> getParams() { 
       // Posting params to register url 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("my_token", token); 
       params.put("page", page); 
       params.put("limit", "20"); 

       return params; 
      } 
     }; 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 

    } 

    private void parseJsonFeed(String response) { 
     try { 
      JSONObject jsonObj = new JSONObject(response); 
      JSONArray feedArray = jsonObj.getJSONArray("data"); 
      for (int i = 0; i < feedArray.length(); i++) { 
       JSONObject feedObj = (JSONObject) feedArray.get(i); 

       QuestionsItem item = new QuestionsItem(); 
       item.setId(feedObj.getInt("id")); 
       item.setQuestion(feedObj.getString("question")); 
       String options = feedObj.getString("multi_ans_option"); 
       String[] parts = options.split("\\|"); 
       String part1 = parts[0]; 
       String part2 = parts[1]; 
       String part3 = parts[2]; 
       String part4 = parts[3]; 
       item.setAnsOne(part1); 
       item.setAnsTwo(part2); 
       item.setAnsThree(part3); 
       item.setAnsFour(part4); 
       item.setAnswer(feedObj.getString("answer")); 
       timeLineItems.add(item); 
      } 

      // notify data changes to list adapater 
      adapter.notifyDataSetChanged(); 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void getData() { 
     //Adding the method to the queue by calling the method getDataFromServer 
     getTimeLineData(token, String.valueOf(requestCount)); 
     //Incrementing the request counter 
     requestCount++; 
    } 

    //This method would check that the recyclerview scroll has reached the bottom or not 
    private boolean isLastItemDisplaying(RecyclerView recyclerView) { 
     if (recyclerView.getAdapter().getItemCount() != 0) { 
      int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition(); 
      if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1) 

       return true; 
     } 
     return false; 
    } 

    @Override 
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
     //Ifscrolled at last then 
     if (isLastItemDisplaying(listView)) { 
      //Calling the method getdata again 
      getData(); 
     } 
    } 
} 

activity_manage_question.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="@null" /> 

</LinearLayout> 

QuestionsItem.java

public class QuestionsItem { 
    private int id; 
    private String question, ansOne, ansTwo, ansThree, ansFour, answer; 

    public QuestionsItem() { 
    } 

    public QuestionsItem(int id, String question, String ansOne, String ansTwo, String ansThree, String ansFour, String answer) { 
     super(); 
     this.id = id; 
     this.question = question; 
     this.ansOne = ansOne; 
     this.ansTwo = ansTwo; 
     this.ansThree = ansThree; 
     this.ansFour = ansFour; 
     this.answer = answer; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getQuestion() { 
     return question; 
    } 

    public void setQuestion(String question) { 
     this.question = question; 
    } 

    public String getAnsOne() { 
     return ansOne; 
    } 

    public void setAnsOne(String ansOne) { 
     this.ansOne = ansOne; 
    } 

    public String getAnsTwo() { 
     return ansTwo; 
    } 

    public void setAnsTwo(String ansTwo) { 
     this.ansTwo = ansTwo; 
    } 

    public String getAnsThree() { 
     return ansThree; 
    } 

    public void setAnsThree(String ansThree) { 
     this.ansThree = ansThree; 
    } 

    public String getAnsFour() { 
     return ansFour; 
    } 

    public void setAnsFour(String ansFour) { 
     this.ansFour = ansFour; 
    } 

    public String getAnswer() { 
     return answer; 
    } 

    public void setAnswer(String answer) { 
     this.answer = answer; 
    } 
} 

QuestionsListAdapter.java

public class QuestionsListAdapter extends RecyclerView.Adapter<QuestionsListAdapter.ViewHolder> { 
    private List<QuestionsItem> timeLineItems; 
    String message, storyId, token, ide; 
    private Context context; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
    ConnectionDetector cd; 
    Boolean isInternetPresent = false; 
    private ProgressDialog pDialog; 
    private SessionManager session; 
    private SQLiteHandler db; 
    int newPosition; 

    public QuestionsListAdapter(List<QuestionsItem> timeLineItems, Context context) { 
     super(); 
     this.context = context; 
     this.timeLineItems = timeLineItems; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.questios_item, parent, false); 
     ViewHolder viewHolder = new ViewHolder(v); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder holder, final int position) { 

     pDialog = new ProgressDialog(context); 
     pDialog.setCancelable(false); 

     db = new SQLiteHandler(context); 

     // session manager 
     session = new SessionManager(context); 

     // Fetching user details from sqlite 
     HashMap<String, String> user = db.getUserDetails(); 
     token = user.get("token"); 

     //Getting the particular item from the list 
     QuestionsItem item = timeLineItems.get(position); 

     holder.txtQues.setText(item.getQuestion()); 

     holder.txtAnsOne.setText(item.getAnsOne()); 

     holder.txtAnsTwo.setText(item.getAnsTwo()); 

     holder.txtAnsThree.setText(item.getAnsThree()); 

     holder.txtAnsFour.setText(item.getAnsFour()); 

     holder.txtAns.setText(item.getAnswer()); 

     holder.btnEdit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       final QuestionsItem m = timeLineItems.get(position); 

       String ide = String.valueOf(m.getId()); 
       String ques = String.valueOf(m.getQuestion()); 
       String Option1 = String.valueOf(m.getAnsOne()); 
       String Option2 = String.valueOf(m.getAnsTwo()); 
       String Option3 = String.valueOf(m.getAnsThree()); 
       String Option4 = String.valueOf(m.getAnsFour()); 
       String answer = String.valueOf(m.getAnswer()); 

       Intent intent = new Intent(context, UpdateQuestionActivity.class); 
       intent.putExtra("id", ide); 
       intent.putExtra("ques", ques); 
       intent.putExtra("option1", Option1); 
       intent.putExtra("option2", Option2); 
       intent.putExtra("option3", Option3); 
       intent.putExtra("option4", Option4); 
       intent.putExtra("answer", answer); 
       context.startActivity(intent); 
      } 
     }); 

     holder.btnDelete.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       final QuestionsItem m = timeLineItems.get(position); 
       newPosition = holder.getAdapterPosition(); 
       ide = String.valueOf(m.getId()); 
       alertBox(); 
      } 
     }); 

    } 

    @Override 
    public int getItemCount() { 
     return timeLineItems.size(); 
    } 

    @Override 
    public int getItemViewType(int position) 
    { 
     return position; 
    } 

    class ViewHolder extends RecyclerView.ViewHolder{ 

     TextView txtQues, txtAnsOne, txtAnsTwo, txtAnsThree, txtAnsFour, txtAns, btnEdit, btnDelete; 

     //Initializing Views 
     public ViewHolder(View itemView) { 
      super(itemView); 
      txtQues = (TextView) itemView.findViewById(R.id.txtQues); 
      txtAnsOne = (TextView) itemView.findViewById(R.id.txtAnsOne); 
      txtAnsTwo = (TextView) itemView.findViewById(R.id.txtAnsTwo); 
      txtAnsThree = (TextView) itemView.findViewById(R.id.txtAnsThree); 
      txtAnsFour = (TextView) itemView.findViewById(R.id.txtAnsFour); 
      txtAns = (TextView) itemView.findViewById(R.id.txtAns); 
      btnEdit = (TextView) itemView.findViewById(R.id.btnEdit); 
      btnDelete = (TextView) itemView.findViewById(R.id.btnDelete); 
     } 
    } 

    public void alertBox(){ 

     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     //Uncomment the below code to Set the message and title from the strings.xml file 
     //builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title); 

     //Setting message manually and performing action on button click 
     builder.setMessage("Do you want to delete question ?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         // Check for empty data in the form 

          cd = new ConnectionDetector(context); 
          isInternetPresent = cd.isConnectingToInternet(); 

          if (isInternetPresent){ 

           DeleteQuestion(token, ide); 

          }else { 
           final SweetAlertDialog alert = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE); 
           alert.setTitleText("No Internet"); 
           alert.setContentText("No connectivity. Please check your internet."); 
           alert.show(); 
          } 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // Action for 'NO' Button 
         dialog.cancel(); 
        } 
       }); 

     //Creating dialog box 
     AlertDialog alert = builder.create(); 
     //Setting the title manually 
     alert.setTitle("Question"); 
     alert.show(); 
    } 

    private void DeleteQuestion(final String token, final String qid) { 
     // Tag used to cancel the request 
     String tag_string_req = "req_register"; 

     pDialog.setMessage("Please wait ..."); 
     showDialog(); 

     StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.updateQues, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 
       hideDialog(); 

       try { 
        JSONObject jObj = new JSONObject(response); 
        boolean error = jObj.getBoolean("status"); 
        if (error) { 
         String errorMsg = jObj.getString("message"); 
         Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show(); 
         timeLineItems.remove(newPosition); 
         notifyItemRemoved(newPosition); 
         notifyItemRangeChanged(newPosition, timeLineItems.size()); 


        } else { 

         // Error occurred in registration. Get the error 
         // message 
         String errorMsg = jObj.getString("message"); 
         Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show(); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(context, "Oops something went wrong...", Toast.LENGTH_LONG).show(); 
       hideDialog(); 
      } 
     }) { 

      @Override 
      protected Map<String, String> getParams() { 
       // Posting params to register url 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("my_token", token); 
       params.put("qid", qid); 

       return params; 
      } 

     }; 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 
    } 

    private void showDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hideDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

questios_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="5dp" 
     android:background="@color/white"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Question : " 
       android:textColor="#000000" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/txtQues" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/feed_item_profile_name" 
       android:textStyle="bold" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Option 1 : " 
       android:textColor="#000000" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/txtAnsOne" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/feed_item_profile_name" 
       android:textStyle="bold" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Option 2 : " 
       android:textColor="#000000" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/txtAnsTwo" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/feed_item_profile_name" 
       android:textStyle="bold" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Option 3 : " 
       android:textColor="#000000" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/txtAnsThree" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/feed_item_profile_name" 
       android:textStyle="bold" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Option 4 : " 
       android:textColor="#000000" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/txtAnsFour" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/feed_item_profile_name" 
       android:textStyle="bold" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Answer : " 
       android:textColor="#000000" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/txtAns" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="@dimen/feed_item_profile_name" 
       android:textStyle="bold" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:weightSum="2"> 

      <TextView android:id="@+id/btnEdit" 
       android:layout_width="0dp" 
       android:layout_height="30dp" 
       android:text="Edit" 
       android:background="@drawable/rounded_square_comment" 
       android:gravity="center" 
       android:textColor="#000000" 
       android:layout_weight="1" 
       android:textSize="15sp" /> 

      <TextView 
       android:id="@+id/btnDelete" 
       android:layout_width="0dp" 
       android:layout_height="30dp" 
       android:text="Delete" 
       android:background="@drawable/rounded_square_comment" 
       android:gravity="center" 
       android:textColor="#000000" 
       android:layout_weight="1" 
       android:textSize="15sp" /> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

内の任意のエラーメッセージAPI呼び出し? –

+0

私のwebserviceがデータを正常に与えて、デバッグにチェックインしたため、エラーメッセージは表示されません。 –

+0

データを呼び出す前にアダプタを設定する必要があります – Rahul

答えて

1

listView.setAdapterはここに

timeLineItems = new ArrayList<>(); 

    adapter = new QuestionsListAdapter(timeLineItems, this); 
    listView.setAdapter(adapter); // Cut from Here 

をカットし、この方法で貼り付けないでください。

private void parseJsonFeed(String response) { 
    try { 
     JSONObject jsonObj = new JSONObject(response); 
     JSONArray feedArray = jsonObj.getJSONArray("data"); 
     for (int i = 0; i < feedArray.length(); i++) { 
      JSONObject feedObj = (JSONObject) feedArray.get(i); 

      QuestionsItem item = new QuestionsItem(); 
      item.setId(feedObj.getInt("id")); 
      item.setQuestion(feedObj.getString("question")); 
      String options = feedObj.getString("multi_ans_option"); 
      String[] parts = options.split("\\|"); 
      String part1 = parts[0]; 
      String part2 = parts[1]; 
      String part3 = parts[2]; 
      String part4 = parts[3]; 
      item.setAnsOne(part1); 
      item.setAnsTwo(part2); 
      item.setAnsThree(part3); 
      item.setAnsFour(part4); 
      item.setAnswer(feedObj.getString("answer")); 
      timeLineItems.add(item); 

      listView.setAdapter(adapter); //Paste Here 
     } 

     // notify data changes to list adapater 
     adapter.notifyDataSetChanged(); 


    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
関連する問題