读取列表视图Android中的所有值

时间:2017-03-05 13:39:16

标签: java android listview

我正在使用循环来读取listview中的所有微调器元素(在cardview内)。

但是循环给出了一个项目,即最后一项。

我已经包含了所有源代码。

可能是什么问题?

还有其他方法可以阅读listview中的所有项目吗?

主要布局

<?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;
        }
    }

1 个答案:

答案 0 :(得分: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()。 我希望这能解决这个问题。