-2

如下面的json響應,我想顯示poolquestions與在回收站查看poolquestion有關的答案。我已經爲poolquestions定義了用於recyclerview和model類的適配器,並且解答了保存json值的答案。但我的問題是,當我要從適配器中的模型類中提取數據時,所有的poolanswers都顯示在所有父項poolquestions中。如何根據其位置顯示來自recyclerview適配器中模型類的列表數組數據?

這裏是我的JSON:

{ 
    "data": [ 
    { 
     "pooltitle": "Who is the best Actor in India", 
     "pooldetails": [ 
     { 
      "poolquestions": "Who is the best actor in Bollywood?", 
      "poolanswer": [ 
      { 
       "answer": "Amir Khan", 
       "percent": 38 
      }, 
      { 
       "answer": "Amitabh Bachhan", 
       "percent": 25 
      }, 
      { 
       "answer": "Salman Khan", 
       "percent": 0 
      }, 
      { 
       "answer": "Akshay Kumar", 
       "percent": 38 
      } 
      ] 
     }, 
     { 
      "poolquestions": "Who has most female fan following?", 
      "poolanswer": [ 
      { 
       "answer": "Amitabh Bachhan", 
       "percent": 13 
      }, 
      { 
       "answer": "Amir Khan", 
       "percent": 38 
      }, 
      { 
       "answer": "Salman Khan", 
       "percent": 13 
      }, 
      { 
       "answer": "Akshay Kumar", 
       "percent": 38 
      } 
      ] 
     }, 
     { 
      "poolquestions": "Who is most popular actor in social media?", 
      "poolanswer": [ 
      { 
       "answer": "Amir Khan", 
       "percent": 27 
      }, 
      { 
       "answer": "Amitabh Bachhan", 
       "percent": 36 
      }, 
      { 
       "answer": "Salman Khan", 
       "percent": 18 
      }, 
      { 
       "answer": "Akshay Kumar", 
       "percent": 18 
      } 
      ] 
     } 
     ] 
    } 
    ], 
    "status": 1 
} 

主要活動的AsyncTask代碼:

private List<PollsData> pollDataArrayList; 
private List<PollQstWithOptions> pollQSTOptionsList = new ArrayList<>(); 

if ("1".equalsIgnoreCase(status)) { 
      try { 
       JSONArray data = js.getJSONArray("data"); 
       for (int i = 0; i < data.length(); i++) { 
        JSONObject detailsData = data.getJSONObject(i); 

        JSONArray pollQstArray = detailsData.getJSONArray("pooldetails"); 
        for (int j = 0; j < pollQstArray.length(); j++) { 
         JSONObject poll = pollQstArray.getJSONObject(j); 
         String poolquestion_ = poll.getString("poolquestions"); 
         pollDataArrayList = new ArrayList<>(); 
         JSONArray poolanswerArray = poll.getJSONArray("poolanswer"); 
         for (int k = 0; k < poolanswerArray.length(); k++) { 
          JSONObject pollOptions = poolanswerArray.getJSONObject(k); 
          String options = pollOptions.getString("answer"); 
          int percent = pollOptions.getInt("percent"); 

          PollsData pollDetails = new PollsData(options, percent); 
          pollDataArrayList.add(pollDetails); 
         } 
         PollQstWithOptions mPollQstWithOptions = new PollQstWithOptions(poolquestion_, pollDataArrayList); 
         pollQSTOptionsList.add(mPollQstWithOptions); 
        } 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      mAdapter = new PollAdapter2(ActivityPollDetails.this, pollQSTOptionsList); 
      RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(ActivityPollDetails.this); 
      recyclerView.setLayoutManager(mLayoutManager); 
      recyclerView.setHasFixedSize(true); 
      recyclerView.setAdapter(mAdapter); 

     } 

適配器類:

public class PollAdapter2 extends RecyclerView.Adapter<PollAdapter2.MyViewHolder> { 
    private List<PollQstWithOptions> dataList; 
    Context context; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 

     TextView txt_poll_question; 

     ProgressBar progress1; 
     LinearLayout mainLayout; 
     public MyViewHolder(View view) { 
      super(view); 

      txt_poll_question=(TextView)view.findViewById(R.id.txt_poll_question); 
      mainLayout=(LinearLayout)view.findViewById(R.id.mainLayout); 
     } 

    } 
    public PollAdapter2(Context mContext, List<PollQstWithOptions> dataList) { 
     this.dataList = dataList; 
     this.context=mContext; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.card_poll_qst_with_ans, parent, false); 

     MyViewHolder viewHolder = new MyViewHolder(v); 

     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, int position) { 
     final PollQstWithOptions poll = dataList.get(position); 

     holder.txt_poll_question.setText(poll.getPollQstName()); 

     for(int i=0; i<dataList.size();i++){ 
      PollQstWithOptions itemsData = dataList.get(i); 
      for(int j=0;j<itemsData.getOptionList().size();j++){ 
       System.out.print("child pos:: "+j); 
       PollsData mPollsData = itemsData.getOptionList().get(j); 

       TextView text = new TextView(context); 
       text.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
       text.setText(mPollsData.getAns1()); 
       holder.mainLayout.addView(text); 
      } 

     } 

    } 

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


} 

回答

0

RecyclerView回收的意見,因此而得名。當您嘗試將視圖添加到您的mainLayout時,請確保先刪除所有視圖,因爲如果它已被創建,則它包含以前添加的視圖。這就是爲什麼你看到所有以前添加的佈局答案。

一個建議:

取而代之的將答案視圖動態的linearLayout,你應該在你的回收站視圖,每一個你的問題和答案都使用2個不同的持有人。請參閱this

0

在你onBindView持有人的for循環應該是這樣的

List<PollsData > pollsDataArraylist = dataList.get(position).getPollsDataArrayList(); 
for(int i = 0;i < pollsdataArraylist.size();i++){ 

    PollsData pData = pollsdatArraylist.get(i); 
    textview1.setText(pData.getAnswer().toString()); 
    textview2.setText(String.valueOf(pData.getPrecent())); 

} 
+0

@ cricket_007:感謝您的編輯。 –

0

只是remvoed在onBindView下面的代碼,我有我自己的問題的解決方案。

for(int i=0; i<dataList.size();i++){ 
      PollQstWithOptions itemsData = dataList.get(i); 

最終代碼,

@Override 
    public void onBindViewHolder(final MyViewHolder holder, int position) { 
     final PollQstWithOptions poll = dataList.get(position); 

     holder.txt_poll_question.setText(poll.getPollQstName()); 


      for(int j=0;j<poll.getOptionList().size();j++){ 
       PollsData mPollsData = poll.getOptionList().get(j); 

       TextView text = new TextView(context); 
       text.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
       text.setText(mPollsData.getAns1()); 
       holder.mainLayout.addView(text); 
      } 

    } 
相關問題