gpt4 book ai didi

android - 从 ListView 中删除动态添加的布局扩展基本适配器

转载 作者:行者123 更新时间:2023-11-30 02:00:19 24 4
gpt4 key购买 nike

这里我有一些布局被夸大了,但是当 ei.duedate.size() 的长度为 0 时, View 不会被删除。

我搜索了很多但没有得到如何从 ListView 中删除 View 。

我已经成功地膨胀了 5-6 个子布局,但是当尺寸变为 1-2 时,膨胀的 View 无法移除。

如果你们有任何想法,请帮助我。

下面是我的适配器代码:

   public class ImmunisationAdapter extends BaseAdapter {

@Override
public int getCount() {
// TODO Auto-generated method stub
return ImmuMainArraylist.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return ImmuMainArraylist.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
final ImmuniModel ei = ImmuMainArraylist.get(position);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(getActivity()).inflate(R.layout.raw_main_immunisation, parent, false);
holder.layout = (ViewGroup) view.findViewById(R.id.ll);
holder.imageView = (ImageView) view.findViewById(R.id.optin);
holder.txt_count = (TextViewPlus)view.findViewById(R.id.txt_circle);
holder.mg_type = (ImageView) view.findViewById(R.id.mg_type);
holder.txt_main = (TextViewPlus) view.findViewById(R.id.txt_main);
holder.txt_new_elder = (TextViewPlus) view.findViewById(R.id.txt_new_elder);

Log.e("SIZE OF CHILDIMMUNISATION",ei.duedate.size()+"");
for (int i = 0; i < ei.duedate.size(); i++) {

View Chiledview = LayoutInflater.from(getActivity()).inflate(R.layout.raw_child_immunisation, null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
CheckBox Jabchk = (CheckBox) Chiledview.findViewById(R.id.chk);
TextViewPlus txt_date = (TextViewPlus) Chiledview.findViewById(R.id.txt_date);
TextViewPlus txt_lbl = (TextViewPlus) Chiledview.findViewById(R.id.text_admin);
Jabchk.setText("Jab " + (i+1));
Chiledview.setTag(i);
Chiledview.setLayoutParams(layoutParams);
txt_date.setText(ei.duedate.get(i).due_on);

if (ei.duedate.get(i).status.equals("Administered")) {
Jabchk.setChecked(true);

txt_lbl.setText("Administered on ");
}else if (ei.duedate.get(i).status.equals("Planned")) {
Jabchk.setChecked(false);
txt_lbl.setText("Estimated due date ");
}
holder.layout.addView(Chiledview, i);
Chiledview.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

int pos = (Integer) v.getTag();

Intent intent = new Intent(getActivity(), EditJabImmunisationActivity.class);
intent.putExtra("pos", (pos+1) + "");
intent.putExtra("status", ei.duedate.get(pos).status);
editor.putString(Utils.JABNAME, ei.Name);
editor.putString(Utils.JABID, ei.duedate.get(pos).id);
editor.putString(Utils.JABDATE, ei.duedate.get(pos).due_on);
editor.commit();
intent.putExtra("location", ei.duedate.get(pos).jab_location);
intent.putExtra("type", ei.duedate.get(pos).jab_type);
intent.putExtra("note", ei.duedate.get(pos).note);
intent.putExtra("doc", ei.duedate.get(pos).doc_name);
intent.putExtra("facility", ei.duedate.get(pos).facility);

startActivity(intent);
getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});

}

view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
((ViewGroup)holder.layout.getParent()).removeView(holder.layout);
holder.txt_main.setText(ImmuMainArraylist.get(position).Name);
if (ImmuMainArraylist.get(position).category.equals("Drops")) {
holder.mg_type.setImageResource(R.drawable.drop);
}else if (ImmuMainArraylist.get(position).category.equals("Injection")) {
holder.mg_type.setImageResource(R.drawable.syring);
}else if (ImmuMainArraylist.get(position).category.contains("Pill")) {
holder.mg_type.setImageResource(R.drawable.pill);
}
holder.imageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindowDogs = popupwindow.popupWindowDogs(position);
popupWindowDogs.showAsDropDown(v, -5, 0);
}
});
holder.txt_new_elder.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("helof", ImmuMainArraylist.get(position).vaccin_id);
try {
AddnewJab(ImmuMainArraylist.get(position).vaccin_id, token);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
return view;
}

}

最佳答案

您需要考虑 View 回收。 holder.layout ViewGroup 中可能已经有子项,因此需要先删除它们。

@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
final ImmuniModel ei = ImmuMainArraylist.get(position);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(getActivity()).inflate(R.layout.raw_main_immunisation, parent, false);
holder.layout = (ViewGroup) view.findViewById(R.id.ll);
holder.imageView = (ImageView) view.findViewById(R.id.optin);
holder.txt_count = (TextViewPlus)view.findViewById(R.id.txt_circle);
holder.mg_type = (ImageView) view.findViewById(R.id.mg_type);
holder.txt_main = (TextViewPlus) view.findViewById(R.id.txt_main);
holder.txt_new_elder = (TextViewPlus) view.findViewById(R.id.txt_new_elder);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
holder.layout.removeAllViews();
}

Log.e("SIZE OF CHILDIMMUNISATION",ei.duedate.size()+"");
for (int i = 0; i < ei.duedate.size(); i++) {

View Chiledview = LayoutInflater.from(getActivity()).inflate(R.layout.raw_child_immunisation, null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
CheckBox Jabchk = (CheckBox) Chiledview.findViewById(R.id.chk);
TextViewPlus txt_date = (TextViewPlus) Chiledview.findViewById(R.id.txt_date);
TextViewPlus txt_lbl = (TextViewPlus) Chiledview.findViewById(R.id.text_admin);
Jabchk.setText("Jab " + (i+1));
Chiledview.setTag(i);
Chiledview.setLayoutParams(layoutParams);
txt_date.setText(ei.duedate.get(i).due_on);

if (ei.duedate.get(i).status.equals("Administered")) {
Jabchk.setChecked(true);

txt_lbl.setText("Administered on ");
} else if (ei.duedate.get(i).status.equals("Planned")) {
Jabchk.setChecked(false);
txt_lbl.setText("Estimated due date ");
}
holder.layout.addView(Chiledview, i);
Chiledview.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int pos = (Integer) v.getTag();

Intent intent = new Intent(getActivity(), EditJabImmunisationActivity.class);
intent.putExtra("pos", (pos+1) + "");
intent.putExtra("status", ei.duedate.get(pos).status);
editor.putString(Utils.JABNAME, ei.Name);
editor.putString(Utils.JABID, ei.duedate.get(pos).id);
editor.putString(Utils.JABDATE, ei.duedate.get(pos).due_on);
editor.commit();
intent.putExtra("location", ei.duedate.get(pos).jab_location);
intent.putExtra("type", ei.duedate.get(pos).jab_type);
intent.putExtra("note", ei.duedate.get(pos).note);
intent.putExtra("doc", ei.duedate.get(pos).doc_name);
intent.putExtra("facility", ei.duedate.get(pos).facility);

startActivity(intent);
getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});

}

holder.txt_main.setText(ImmuMainArraylist.get(position).Name);
if (ImmuMainArraylist.get(position).category.equals("Drops")) {
holder.mg_type.setImageResource(R.drawable.drop);
}else if (ImmuMainArraylist.get(position).category.equals("Injection")) {
holder.mg_type.setImageResource(R.drawable.syring);
}else if (ImmuMainArraylist.get(position).category.contains("Pill")) {
holder.mg_type.setImageResource(R.drawable.pill);
}
holder.imageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindowDogs = popupwindow.popupWindowDogs(position);
popupWindowDogs.showAsDropDown(v, -5, 0);
}
});
holder.txt_new_elder.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("helof", ImmuMainArraylist.get(position).vaccin_id);
try {
AddnewJab(ImmuMainArraylist.get(position).vaccin_id, token);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
return view;
}

我刚刚向您展示了快速简便的方法。更好的方法是不要调用 removeAllViews(),而是在循环遍历 ei.duedate 时调用 getChildAt(i) 查看 raw_child_immunisation View 是否已经存在并使用它而不是扩充新 View 。您还必须使用 removeViews(start, count) 在循环结束时清除任何残留的 raw_child_immunisation View 。

以下是该策略的代码更改:

@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
final ImmuniModel ei = ImmuMainArraylist.get(position);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(getActivity()).inflate(R.layout.raw_main_immunisation, parent, false);
holder.layout = (ViewGroup) view.findViewById(R.id.ll);
holder.imageView = (ImageView) view.findViewById(R.id.optin);
holder.txt_count = (TextViewPlus)view.findViewById(R.id.txt_circle);
holder.mg_type = (ImageView) view.findViewById(R.id.mg_type);
holder.txt_main = (TextViewPlus) view.findViewById(R.id.txt_main);
holder.txt_new_elder = (TextViewPlus) view.findViewById(R.id.txt_new_elder);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
int childCount = holder.layout.getChildCount();
if (childCount > ei.duedate.size()) {
// remove any extra views
holder.layout.removeViews(ei.duedate.size(), childCount - ei.duedate.size());
}
}

Log.e("SIZE OF CHILDIMMUNISATION",ei.duedate.size()+"");
for (int i = 0; i < ei.duedate.size(); i++) {

// if a view already exists, use it
View Chiledview = holder.layout.getChildAt(i);
if (Chiledview == null) {
// otherwise create a new view and add it to the view group
Chiledview = LayoutInflater.from(getActivity()).inflate(R.layout.raw_child_immunisation, null);
holder.layout.addView(Chiledview, i);
}
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
CheckBox Jabchk = (CheckBox) Chiledview.findViewById(R.id.chk);
TextViewPlus txt_date = (TextViewPlus) Chiledview.findViewById(R.id.txt_date);
TextViewPlus txt_lbl = (TextViewPlus) Chiledview.findViewById(R.id.text_admin);
Jabchk.setText("Jab " + (i+1));
Chiledview.setTag(i);
Chiledview.setLayoutParams(layoutParams);
txt_date.setText(ei.duedate.get(i).due_on);

if (ei.duedate.get(i).status.equals("Administered")) {
Jabchk.setChecked(true);

txt_lbl.setText("Administered on ");
} else if (ei.duedate.get(i).status.equals("Planned")) {
Jabchk.setChecked(false);
txt_lbl.setText("Estimated due date ");
}
Chiledview.setOnClickListener(new View.OnClickListener() {
.
.
.
(rest of code the same)

关于android - 从 ListView 中删除动态添加的布局扩展基本适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31587005/

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