2017-03-05 41 views
0

我正在使用循環來讀取列表視圖中的所有旋轉元素(在cardview中)。閱讀列表視圖中的所有值Android

但是循環只給出一個項目,即最後一個項目。

我已經包含了所有的源代碼。

可能是什麼問題?

是否有任何其他方式來閱讀所有列表中的項目?

主要佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:id="@+id/ll_gpa" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

      <ListView 
       android:id="@+id/listSubs" 
       android:scrollbars="none" 
       android:layout_height="0dip" 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       /> 
<Button 
      android:text="Calculate GPA" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnGpa" 
      android:textAlignment="center" 
      android:layout_gravity="center_horizontal" /> 
    </LinearLayout> 

</RelativeLayout> 

單項佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/cv_subject" 
    android:layout_marginBottom="2dp" 
    android:layout_marginTop="2dp" 
    > 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="15dp" 
     android:id="@+id/rl_subject" 
     > 

     <TextView 
      android:id="@+id/subTitle" 
      android:textSize="16sp" 
      android:text="Subject" 
      android:layout_toStartOf="@+id/subGrade" 
      android:layout_toLeftOf="@+id/subGrade" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="none" 
      /> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:spinnerMode="dropdown" 
      android:dropDownWidth="40dp" 
      android:textAlignment="center" 
      android:entries="@array/grades" 
      android:id="@+id/subGrade" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

MainActivity

public class MainActivity extends AppCompatActivity { 

    private static SubjectArrayAdapter adapter; 
    private ProgressDialog progress; 
    JsonArray subs = new JsonArray(); 
    JsonArray dataGrades = new JsonArray(); 
    ListView listView; 
    ArrayList<SubjectClass> subjects = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     res = new GetSubNames().execute(); 
subs = res.getAsJsonArray("subjects"); 

       for (int i = 0; i < subs.size(); i++){ 
        JsonObject temp = subs.get(i).getAsJsonObject(); 
        subjects.add(new SubjectClass(temp.get("subCode").getAsString(), temp.get("subTitle").getAsString())); 
       } 
       adapter = new SubjectArrayAdapter(subjects,GpaActivity.this); 
       listView.setAdapter(adapter); 
       adapter.notifyDataSetChanged(); 
     Button btnGpa = (Button) findViewById(R.id.btnGpa); 
     btnGpa.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new CalcGPA().execute(); 
       JsonArray temparr = new JsonArray(); 

       for (int i = 0; i < listView.getChildCount(); i++) { 
        // Get row's spinner 
        View listItem = listView.getChildAt(i); 

        JsonObject temp = new JsonObject(); 

        Spinner spinner = (Spinner) listItem.findViewById(R.id.subGrade); 

        temp.addProperty("subject",subjects.get(i).getCode()); 
        temp.addProperty("grade", spinner.getSelectedItem().toString()); 
        temparr.add(temp); 
       } 

       dataGrades = temparr; 
      } 
     }); 
    } 
} 

自定義類

class SubjectClass{ 
    String title; 
    String code; 

    SubjectClass(String code, String title) { 
     this.code = code; 
     this.title = title; 
    } 

    String getTitle(){ 
     return this.title; 
    } 

    String getCode(){ 
     return this.code; 
    } 

    void setCode(String code){ 
     this.code = code; 
    } 

    void setTitle(String title){ 
     this.title = title; 
    } 

} 

定製ArrayAdapter

public class SubjectArrayAdapter extends ArrayAdapter<SubjectClass>{ 

     private ArrayList<SubjectClass> dataSet; 
     Context mContext; 

     // View lookup cache 
     private static class ViewHolder { 
      TextView txtName; 
     } 

     public SubjectArrayAdapter(ArrayList<SubjectClass> data, Context context) { 
      super(context, R.layout.item_subject, data); 
      this.dataSet = data; 
      this.mContext=context; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // Get the data item for this position 
      SubjectClass dataModel = getItem(position); 
      // Check if an existing view is being reused, otherwise inflate the view 
      ViewHolder viewHolder; // view lookup cache stored in tag 

      final View result; 

      if (convertView == null) { 

       viewHolder = new ViewHolder(); 
       LayoutInflater inflater = LayoutInflater.from(getContext()); 
       convertView = inflater.inflate(R.layout.item_subject, parent, false); 
       viewHolder.txtName = (TextView) convertView.findViewById(R.id.subTitle); 
       result = convertView; 

       convertView.setTag(viewHolder); 
      } else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
       result = convertView; 
      } 

      viewHolder.txtName.setText(dataModel.getCode() + " " + dataModel.getTitle()); 
      // Return the completed view to render on screen 
      return convertView; 
     } 
    } 

回答

0
for (int i = 0; i < listView.getCount(); i++) { 
        // Get row's spinner 
        View listItem = listView.getItemAtPosition(i); 

        JsonObject temp = new JsonObject(); 

        Spinner spinner = (Spinner) listItem.findViewById(R.id.subGrade); 

        temp.addProperty("subject",subjects.get(i).getCode()); 
        temp.addProperty("grade", spinner.getSelectedItem().toString()); 
        temparr.add(temp); 
       } 

檢查的主要活動上面的代碼。使用.getCount()代替.getChildCount() 並使用.getItemAtIndex()代替.getChildAt()。 我希望這能解決這個問題。

+0

不工作。無法將SubjectClass轉換爲View,會出現錯誤。 –