这是我的数据结构类:
public class FirstAidSteps implements Parcelable {
//These are the fields
private final String stepName;
private final ArrayList<String> steps;
.
.
.
}
这是我写过的适配器:
public class StepsAdapter extends ArrayAdapter<FirstAidSteps> {
public StepsAdapter(Context context, ArrayList<FirstAidSteps> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FirstAidSteps steps = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
TextView stepName = (TextView) convertView.findViewById(R.id.step_heading);
stepName.setText(steps.getStepName());
return convertView;
}
}
这是列表项的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/step_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="25sp" />
</LinearLayout>
我想将ArrayList 步骤的内容添加到ListView中。我无法为步骤预定义XML,因为它们可以具有任何大小。
我希望输出如下所示:
___________________
| StepName |
| Step |
| Step |
|-------------------|
| StepName |
| Step |
|-------------------|
| StepName |
|___________________|
那怎么办呢?
答案 0 :(得分:1)
假设步数相当小,我通常在这里使用LinearLayout,并添加必要的视图。
为此,您只需在项目视图中添加LinearLayout即可包含这些步骤。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/step_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/steps"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
然后通过适配器向项目视图添加步骤。确保清除视图中的前面步骤(同时确保使用ViewHolder模式)。
public class StepsAdapter extends ArrayAdapter<FirstAidSteps> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.stepName = (TextView) v.findViewById(R.id.step_heading);
holder.steps = (LinearLayout) v.findViewById(R.id.steps);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
final FirstAidSteps steps = getItem(position);
holder.stepName.setText(steps.getStepName());
holder.steps.removeAllViews();
for (String step : steps.getSteps()) {
holder.steps.addView(createStepView(step));
}
return v;
}
private static class ViewHolder {
TextView stepName;
LinearLayout steps;
}
}
在这种情况下,createStepView()
是读者的练习。
根据您的布局和可能的步骤数量,这可能不是最适合您的解决方案。如果您可能有很多步骤,您可能需要查看某种回收站视图或其他回收容器。
答案 1 :(得分:0)
将您的xml文件更改为此
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/step_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/steps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
更改此步骤
private final ArrayList<ArrayList<String>> steps;
private final ArrayList<String> stepsName;
并在适配器
中public StepsAdapter(Context context, ArrayList<ArrayList<String>> steps, ArrayList<String> stepsName) {
super(context, 0, steps, stepsName);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
TextView stepName = (TextView) convertView.findViewById(R.id.step_heading);
LinearLayout mLinearLayout = (LinearLayout) v.findViewById(R.id.steps);
stepName.setText(stepsName.get(position));
mLinearLayout.removeAllViews();
ArrayList<String> single_step= steps.get(position);
if(single_step.size() > 0){
for (int size =0; size <single_step.size(); size++) {
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
text.setGravity(Gravity.CENTER);
text.setPadding(5, 2, 5, 2);
text.setText(single_step.get(size));
mLinearLayout.addView(text);
}
}
return convertView;
}