用一个ArrayList of Objects android填充CardView

时间:2015-04-07 22:06:51

标签: java android arrays android-cardview android-recyclerview

我有一些代码可以将对象添加到arrayList并在ListView中显示 - 这非常有效

然后我通过意图发送数组 - 它被完美地接收

立即...

我想使用此数组来填充 (android.support.v7.widget.CardView)我使用RecyclerView来显示卡片,但我唯一的问题是{{1} } adapter遍历给定的数组,并使用位置在数组中为每个项目添加一张卡片。

我希望能够使用Array中的对象来填充单张卡片。如果你需要我更好地解释一下,请告诉我,这是一个非常棘手的问题!

以下是一些代码:

这是将项添加到RecyclerView和数组的代码:

listView

这是使用适配器的代码,因此它接收来自其他活动的数组并将其提供给适配器:

public class AddExerciseView extends ActionBarActivity {

    TrackDatabaseAdapter mTrackDatabaseAdapter;

    TextView workoutName;
    EditText weight, reps;
    double weightCount;
    int repsCount;
    ListView list;
    Button plus, plus2, minus, minus2, update;
    String name;
    int type, category, count;
    Exercise ex;
    ArrayList<Set> array;
    private AddExerciseAdapter mAddExerciseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_workout_view);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            name = extras.getString("ExerciseName");
            type = extras.getInt("ExerciseType");
            category = extras.getInt("ExerciseCategory");
        }
        mTrackDatabaseAdapter = new TrackDatabaseAdapter(this);

        ActionBar actionBar = AddExerciseView.this.getSupportActionBar();

        if (actionBar != null) {
            actionBar.setTitle(name);
        }

        ex = new Exercise(name, type, category);
        Log.v("Exercise Recieved", ex.toString());
        workoutName = (TextView) findViewById(R.id.name);
        weight = (EditText) findViewById(R.id.textWeight);
        reps = (EditText) findViewById(R.id.textRep);
        list = (ListView) findViewById(R.id.rowReps);
        plus = (Button) findViewById(R.id.plus);
        minus = (Button) findViewById(R.id.minus);
        plus2 = (Button) findViewById(R.id.plus2);
        minus2 = (Button) findViewById(R.id.minus2);
        update = (Button) findViewById(R.id.update);

        weightCount = 0;
        repsCount = 0;
        count = 1;

        array = new ArrayList<>();

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!weight.getText().toString().equals("")) {
                    weightCount = Double.parseDouble(weight.getText().toString());
                    weightCount = weightCount + 2.5;
                    weight.setText(String.valueOf(weightCount));
                } else {
                    weight.setText(String.valueOf(2.5));
                }
            }
        });

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weightCount <= 0.0 || weight.getText().toString().equals("")) {
                    weight.setText(String.valueOf(0.0));
                } else {
                    weightCount = Double.parseDouble(weight.getText().toString());
                    weightCount = weightCount - 2.5;
                    weight.setText(String.valueOf(weightCount));
                }
            }
        });

        plus2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!reps.getText().toString().equals("")) {
                    repsCount = Integer.parseInt(reps.getText().toString());
                    repsCount = repsCount + 1;
                    reps.setText(String.valueOf(repsCount));
                } else {
                    reps.setText(String.valueOf(0));
                }
            }
        });

        minus2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (repsCount <= 0 || reps.getText().toString().equals("")) {
                    reps.setText(String.valueOf(0));
                } else {
                    repsCount = Integer.parseInt(reps.getText().toString());
                    repsCount = repsCount - 1;
                    reps.setText(String.valueOf(repsCount));
                }
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (weight.getText().toString().equals("") || weight.getText().toString().equals(String.valueOf(0.0))) {
                    weight.setText(String.valueOf(0.0));
                    Toast.makeText(getApplicationContext(), "You need to enter a weight!", Toast.LENGTH_SHORT).show();
                } else if (reps.getText().toString().equals("") || reps.getText().toString().equals(String.valueOf(0))) {
                    reps.setText(String.valueOf(0));
                    Toast.makeText(getApplicationContext(), "You need to have at least 1 rep!", Toast.LENGTH_SHORT).show();
                } else {
                    double temp2 = Double.parseDouble(weight.getText().toString());
                    int temp3 = Integer.parseInt(reps.getText().toString());
                    Set s = new Set(name, temp2, temp3);
                    array.add(s);
                    mAddExerciseAdapter.notifyDataSetChanged();
                    mTrackDatabaseAdapter.insertEntry(name, temp2, temp3);
                    count++;
                }
            }
        });

        mAddExerciseAdapter = new AddExerciseAdapter(getApplicationContext(), R.layout.row2, array);
        list.setAdapter(mAddExerciseAdapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_exercise, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == R.id.action_done) {
            Intent intent = new Intent(AddExerciseView.this, MainMenuView2.class);
            intent.putParcelableArrayListExtra("array", array);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}

这是适配器,现在它是错误的并且给我一个数组越界错误,我已经知道为什么了,但是我想我会分享我的尝试,每次只创建新卡之前的那个并且只更改1行:

public class MainMenuView2 extends ActionBarActivity {

    //List<Exercise> list = new ArrayList<>();
    TrackDatabaseAdapter mTrackDatabaseAdapter;
    List<Set> set = new ArrayList<>();
    List<Set> test = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().getParcelableArrayListExtra("array") != null) {
            set = getIntent().getParcelableArrayListExtra("array");
            for (Set s : set) {
                Log.d("Parcelable Array", s.toString());
            }
        }
        mTrackDatabaseAdapter = new TrackDatabaseAdapter(this);
        if (mTrackDatabaseAdapter.getAllCategories().size() == 0) {
            test = mTrackDatabaseAdapter.getAllCategories();
        }
        Log.v("test array", test.toString());
        setContentView(R.layout.main_menu2);

        ActionBar actionBar = MainMenuView2.this.getSupportActionBar();

        if (actionBar != null) {
            actionBar.setTitle("ProjectME");
        }

        RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
        recList.setHasFixedSize(false);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);
        CardAdapter ca = new CardAdapter(set);
        recList.setAdapter(ca);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == R.id.action_add) {
            Toast.makeText(this, "Add has been clicked!", Toast.LENGTH_SHORT).show();
            Intent create = new Intent(MainMenuView2.this, CategoryListView.class);
            startActivity(create);
        }

        if (id == R.id.action_copy) {
            Toast.makeText(this, "Copy has been clicked!", Toast.LENGTH_SHORT).show();
        }

        if (id == R.id.action_cal) {
            Toast.makeText(this, "Calendar has been clicked!", Toast.LENGTH_SHORT).show();
            Intent create = new Intent(MainMenuView2.this, CalendarView.class);
            startActivity(create);
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:2)

事实证明,这实际上是不可能的,但它可能会非常非常困难。 我刚刚创建了一个类,它存储了List和练习的名称,然后在适配器中我只使用

    @Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
    Card card = cards.get(i);
    List<Set> sets = card.getSet();
    int x = sets.size();
    int count = 0;

    for(Set set : sets) {
        if(count == 0) {
            contactViewHolder.vName.setText(set.getName());
            contactViewHolder.vReps.setText(set.getReps() + "");
            contactViewHolder.vWeight.setText(set.getWeight() + "");
        } if(count == 1) {
            contactViewHolder.vReps1.setText(set.getReps() + "");
            contactViewHolder.vWeight1.setText(set.getWeight() + "");
        } if(count == 2) {
            contactViewHolder.vReps2.setText(set.getReps() + "");
            contactViewHolder.vWeight2.setText(set.getWeight() + "");
        } if(count == 3) {
            contactViewHolder.vReps3.setText(set.getReps() + "");
            contactViewHolder.vWeight3.setText(set.getWeight() + "");
        } if(count == 4) {
            contactViewHolder.vReps4.setText(set.getReps() + "");
            contactViewHolder.vWeight4.setText(set.getWeight() + "");
        } if(x > 4) {
            contactViewHolder.vShowMore.setText((x - 5) + " More");
        }
        count++;
    }


}

它现在显示我需要的东西,也更容易阅读。