gpt4 book ai didi

java - 使用对象的 ArrayList 填充 CardView android

转载 作者:行者123 更新时间:2023-11-29 20:49:34 24 4
gpt4 key购买 nike

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

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

现在……

我想用这个数组来填充一个Card (android.support.v7.widget.CardView) 我正在使用一个RecyclerView显示卡片,但我唯一的问题是,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();
}
}

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

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行:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ContactViewHolder> {

private List<Set> contactList;

public CardAdapter(List<Set> contactList) {
this.contactList = contactList;
}

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

@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {

int x = contactList.size();
Log.v("array size", x + "");

if (contactList.get(i) != null) {
contactViewHolder.vName.setText(contactList.get(i).getName());
contactViewHolder.vReps.setText(contactList.get(i).getReps() + "");
contactViewHolder.vWeight.setText(contactList.get(i).getWeight() + "");
}
if (contactList.get(i + 1) != null) {
contactViewHolder.vName.setText(contactList.get(i).getName());
contactViewHolder.vReps1.setText(contactList.get(i + 1).getReps() + "");
contactViewHolder.vWeight1.setText(contactList.get(i + 1).getWeight() + "");
}
if (contactList.get(i + 2) != null) {
contactViewHolder.vName.setText(contactList.get(i + 2).getName());
contactViewHolder.vReps2.setText(contactList.get(i + 2).getReps() + "");
contactViewHolder.vWeight2.setText(contactList.get(i + 2).getWeight() + "");

}
if (contactList.get(i + 3) != null) {
contactViewHolder.vName.setText(contactList.get(i + 3).getName());
contactViewHolder.vReps3.setText(contactList.get(i + 3).getReps() + "");
contactViewHolder.vWeight3.setText(contactList.get(i + 3).getWeight() + "");

}
if (contactList.get(i + 4) != null) {
contactViewHolder.vName.setText(contactList.get(i + 4).getName());
contactViewHolder.vReps4.setText(contactList.get(i + 4).getReps() + "");
contactViewHolder.vWeight4.setText(contactList.get(i + 4).getWeight() + "");

}
if (contactList.size() > 4) {
contactViewHolder.vShowMore.setText("Show " + (contactList.size() - 5) + " more");
}

}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_view, viewGroup, false);

ContactViewHolder cvh = new ContactViewHolder(itemView);
return cvh;
}

public static class ContactViewHolder extends RecyclerView.ViewHolder {

protected TextView vName;
protected TextView vWeight;
protected TextView vReps;
protected TextView vWeight1;
protected TextView vReps1;
protected TextView vWeight2;
protected TextView vReps2;
protected TextView vWeight3;
protected TextView vReps3;
protected TextView vWeight4;
protected TextView vReps4;
protected TextView vShowMore;

public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.editText);
vWeight = (TextView) v.findViewById(R.id.textView7);
vReps = (TextView) v.findViewById(R.id.textView9);

vWeight1 = (TextView) v.findViewById(R.id.textView11);
vReps1 = (TextView) v.findViewById(R.id.textView13);

vWeight2 = (TextView) v.findViewById(R.id.textView15);
vReps2 = (TextView) v.findViewById(R.id.textView17);

vWeight3 = (TextView) v.findViewById(R.id.textView19);
vReps3 = (TextView) v.findViewById(R.id.textView21);

vWeight4 = (TextView) v.findViewById(R.id.textView23);
vReps4 = (TextView) v.findViewById(R.id.textView25);

vShowMore = (TextView) v.findViewById(R.id.textView32);
}
}
}

最佳答案

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

    @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++;
}


}

它现在显示了我需要的内容,而且阅读起来也更简单了。

关于java - 使用对象的 ArrayList 填充 CardView android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29502220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com