简体   繁体   English

如何在Android中动态更改ListView的列表项?

[英]How to dynamically change List items for the ListView in Android?

Here's my data structure class: 这是我的数据结构类:

public class FirstAidSteps implements Parcelable {
    //These are the fields
    private final String stepName;
    private final ArrayList<String> steps;
    .
    .
    .
}

Here's the adapter I've written: 这是我编写的适配器:

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

Here's the XML for List Item: 这是列表项的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>

I want to add the contents of ArrayList steps to the ListView. 我想将ArrayList 步骤的内容添加到ListView。 And I can't predefine the XML for steps because they can have any size. 而且我无法为步骤预定义XML,因为它们可以有任意大小。

I want the output something like below: 我希望输出如下所示:

 ___________________
| StepName          |
|   Step            |
|   Step            |
|-------------------|
| StepName          |
|   Step            |
|-------------------|
| StepName          |
|___________________|

So how to do this? 那么该怎么做呢?

Assuming the number of steps is fairly small, I usually use a LinearLayout here, and add the necessary views. 假设步骤数量很少,我通常在这里使用LinearLayout并添加必要的视图。

To do this, you simply need to add a LinearLayout to your item view to contain the steps. 为此,您只需要向您的项目视图添加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>

Then you add steps to your item view via the adapter. 然后,通过适配器将步骤添加到项目视图中。 Make sure you clear the previous steps in the view (also making sure you use the ViewHolder pattern). 确保清除视图中的先前步骤(也请确保使用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;
    }
}

In this case, createStepView() is an exercise for the reader. 在这种情况下, createStepView()是读者的练习。

Depending on your layout and possible number of steps, this may not be the best solution for you. 根据您的布局和可能的步骤数,这可能不是您的最佳解决方案。 If you will potentially have a lot of steps you may want to look into some sort of recycler view, or other recycling container. 如果您可能要执行很多步骤,则可能需要查看某种回收者视图或其他回收容器。

cahnge your xml file to this 将您的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>

change steps to this 更改此步骤

private final ArrayList<ArrayList<String>> steps;
private final ArrayList<String> stepsName;

and in adapter 和适配器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM