2017-10-22 8 views
2

저는 Android 응용 프로그램에서 작업 중이며 하나의 문제에 직면하고 있습니다. 내 애플 리케이션을 실행하면 리사이클 뷰에 아무 것도 나타나지 않지만 디버그 모드에서 애플리케이션을 체크하면 데이터가 리사이클 뷰에 성공적으로 표시됩니다. 내 웹 서비스가 제대로 작동하고 데이터를 성공적으로 제공하는지 확인했습니다. 내가 어떻게 이걸 얻을 수 있니?Recyclview는 디버그 모드에서 데이터를 표시하지만 noraml에서는 작동하지 않습니다.

ManageQuestionActivity 자바

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

(210)

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