2017-02-16 98 views
-3

我一直在努力,現在這個問題相當長的一段時間,所以希望有人將能夠伸出援助之手。ArrayAdapter沒有刷新或更新適配器查看內容

本質上我有一個ArrayAdapter,它將一個ArrayList對象CourseCardModel提供給一個自定義可滑動視圖,該視圖擴展了AdapterView

我可以安裝適配器並首次查看第一次絕對沒問題,課程卡片顯示出來並可以輕掃,但是當我嘗試更改與ArrayAdapter關聯的CourseCardModels的ArrayList時,視圖不會按照隨着新的數據,它只是凍結。

連接到適配器的數據需要更改的原因是由於我希望用戶能夠過濾課程卡,因此例如他們只能從劍橋大學查看卡。

這個問題的任何幫助將非常感激,我有這麼蒐羅了類似的問題,但還沒有找到那個爲我工作的解決方案。試圖改變數據

// Set up and assign card adapter 
    ca = new CustomCardAdapter(CardsActivity.this, android.R.layout.simple_list_item_1, generateCourseCards()); 
    flingContainer.init(CardsActivity.this, ca); 

// Update the data in the adapter 
         ca.clear(); 
         ca.addAll(coursesToReturn); 
         ca.notifyDataSetChanged(); 

GenerateCourseCards

ArrayAdapter代碼

public class CustomCardAdapter extends ArrayAdapter { 

private TextView courseName, uniName, entryStandards, courseDuration, studyMode, qualification, 
     studentSatisfaction, gradProspects, t1, t2, t3, t4, t5, t6; 

ArrayList<CourseCardModel> items; 



View v; 


LayoutInflater vi; 

public CustomCardAdapter(Activity context, int resource, ArrayList<CourseCardModel> courses) { 
    super(context, resource, courses); 

    this.items = courses; 

    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 


@NonNull 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    v = convertView; 
    if (v == null) { 

     v = vi.inflate(R.layout.course_card_inner_template, parent , false); 
    } 

    CourseCardModel c = (CourseCardModel) getItem(position); 

    if (c != null) { 

     courseName = (TextView) v.findViewById(R.id.courseCardCourseName); 
     uniName = (TextView) v.findViewById(R.id.courseCardUniName); 
     entryStandards = (TextView) v.findViewById(R.id.courseCardEntryStandards); 
     courseDuration = (TextView) v.findViewById(R.id.courseCardCourseDuration); 
     studyMode = (TextView) v.findViewById(R.id.courseCardStudyMode); 
     qualification = (TextView) v.findViewById(R.id.courseCardQualification); 
     studentSatisfaction = (TextView) v.findViewById(R.id.courseCardStudentSatisfaction); 
     gradProspects = (TextView) v.findViewById(R.id.courseCardGraduateProspects); 


     t1 = (TextView) v.findViewById(R.id.cardTV1); 
     t2 = (TextView) v.findViewById(R.id.cardTV2); 
     t3 = (TextView) v.findViewById(R.id.cardTV3); 
     t4 = (TextView) v.findViewById(R.id.cardTV4); 
     t5 = (TextView) v.findViewById(R.id.cardTV5); 
     t6 = (TextView) v.findViewById(R.id.cardTV6); 

     v.setBackgroundResource(R.drawable.newcard); 

     courseName.setText(c.getCourse().getCourseName()); 
     uniName.setText(c.getCourse().getUniversity()); 
     entryStandards.setText(c.getCourse().getEntryStandards()); 
     courseDuration.setText(c.getCourse().getCourseDuration()); 
     studyMode.setText(c.getCourse().getStudyMode()); 
     qualification.setText(c.getCourse().getQualification()); 
     gradProspects.setText(c.getCourse().getGradProspects() + "/100"); 
     studentSatisfaction.setText(c.getCourse().getStudentSatisfaction() + "/5"); 



    } 

    if(position ==0) 
    { 
     //float alpha = (float) 0.8; 
     //v.setAlpha(alpha); 
     courseName.setVisibility(View.VISIBLE); 
    } 

    else if (position == 1){ 

     // Prepare the View for the animation 
     v.setVisibility(View.VISIBLE); 

     float alpha = (float) 0.8; 
     float alpha2 = (float) 0.3; 
     courseName.setAlpha(alpha2); 
     uniName.setAlpha(alpha2); 
     entryStandards.setAlpha(alpha2); 
     courseDuration.setAlpha(alpha2); 
     studyMode.setAlpha(alpha2); 
     qualification.setAlpha(alpha2); 
     studentSatisfaction.setAlpha(alpha2); 
     gradProspects.setAlpha(alpha2); 
     t1.setAlpha(alpha2); 
     t2.setAlpha(alpha2); 
     t3.setAlpha(alpha2); 
     t4.setAlpha(alpha2); 
     t5.setAlpha(alpha2); 
     t6.setAlpha(alpha2); 
     v.setAlpha(alpha); 


    } 
    else { 
     v.setVisibility(View.INVISIBLE); 
    } 

    return v ; 
} 

} 

代碼()方法返回CourseCardModel對象的初始列表來填充視圖。

回答

0

存在該更新開箱,並在有些情況下它沒有的情況。對於非工作情況下,它有助於重新分配適配器它-S顯示元素

在viewPager我用

 mAdapter.notifyDataSetChanged(); 
     mViewPager.setAdapter(mAdapter); 

在GridView控件和ListView我用

  mGridView.setAdapter(mAdapter); 
      mList.setAdapter(mDataAdapter); 
+0

嗨,可惜這次沒工作。當我打電話mAdaper.Clear()它清除所有的內容,並從屏幕移開的觀點,但是從來沒有將新的數據或更新它。 – freshwaterjoe