Java 如何在Android中动态更改ListView的列表项?
这是我的数据结构类:Java 如何在Android中动态更改ListView的列表项?,java,android,listview,Java,Android,Listview,这是我的数据结构类: public class FirstAidSteps implements Parcelable { //These are the fields private final String stepName; private final ArrayList<String> steps; . . . } 那么如何做到这一点呢?假设步骤的数量相当少,我通常在这里使用线性布局,并添加必要的视图 为此,只需将Linear
public class FirstAidSteps implements Parcelable {
//These are the fields
private final String stepName;
private final ArrayList<String> 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>
然后通过适配器将步骤添加到项目视图中。确保在视图中清除前面的步骤(同时确保使用ViewHolder图案)
公共类StepsAdapter扩展了ArrayAdapter{
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
视窗座;
如果(v==null){
v=LayoutInflater.from(getContext()).flate(R.layout.list_项,父项,false);
holder=新的ViewHolder();
holder.stepName=(TextView)v.findViewById(R.id.step_标题);
holder.steps=(线性布局)v.findViewById(R.id.steps);
v、 setTag(支架);
}否则{
holder=(ViewHolder)v.getTag();
}
最终急救步骤=获取项目(位置);
holder.stepName.setText(steps.getStepName());
holder.steps.removeallview();
for(字符串步骤:steps.getSteps()){
holder.steps.addView(createStepView(step));
}
返回v;
}
私有静态类视图持有者{
TextView步骤名;
线性布置台阶;
}
}
在本例中,createStepView()
是读者的练习
根据您的布局和可能的步骤数,这可能不是您的最佳解决方案。如果您可能有很多步骤,那么您可能需要查看某种类型的回收器视图或其他回收容器。将您的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;
私有最终数组列表步骤;
私人最终ArrayList 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;
}
public StepsAdapter(上下文上下文、ArrayList步骤、ArrayList步骤名称){
超级(上下文、0、步骤、步骤名称);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.list_项,父项,false);
}
TextView步骤名称=(TextView)convertView.findViewById(R.id.step_标题);
LinearLayout mLinearLayout=(LinearLayout)v.findViewById(R.id.steps);
stepName.setText(stepsName.get(position));
mLinearLayout.removeallview();
ArrayList single_step=steps.get(位置);
如果(单步大小()>0){
对于(int size=0;使用数组列表的位置的大小?使用适配器在列表中添加数组列表..当您想要在任意点更改值时,只需在任意X位置更改数组列表中的值。并使用notifyDatasetchange方法查看列表中的更改view@MustanserIqbal我已经用所需的输出更新了问题。好的,让我添加一个您的步骤名称将始终在列表中?
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;
}
}
<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;
}