我对 Listview 项目有疑问。在这个Listview中,我将创建一个线性布局,该布局将添加到我原来的布局中。第一次数据非常清晰,但是当我滚动它时,数据是未匹配的。任何人都能帮我解决这个问题吗?我使用了扩展基础适配器
代码
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = ((Activity) context).getLayoutInflater().inflate(R.layout.wishlist_items, parent, false);
holder = new ViewHolder();
holder.wishlistName = (TextView) view.findViewById(R.id.wishlist_name);
holder.wishlistEmail = (TextView) view.findViewById(R.id.wishlist_email);
holder.wishlistRelation = (TextView) view.findViewById(R.id.wishlist_relation);
holder.wishGiftAdvisorText = (TextView) view.findViewById(R.id.gift_advisor_text);
//holder.advisorListview = (ListView) view.findViewById(R.id.listView);
holder.inviteAdvisor = (ImageButton) view.findViewById(R.id.invite_advisor);
holder.advisorContainer = (LinearLayout) view.findViewById(R.id.advisor_container);
holder.editButton = (ImageButton) view.findViewById(R.id.editButton);
holder.deleteButton = (ImageButton) view.findViewById(R.id.deleteButton);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// holder.advisorContainer.removeAllViews();
holder.wishlistName.setText(names[position]);
if(!emails[position].equals("0")) {
holder.wishlistEmail.setText(emails[position]);
} else {
holder.wishlistEmail.setText("");
}
holder.wishlistRelation.setText(relationships[position]);
holder.wishGiftAdvisorText.setText(getResources().getString( R.string.wishlist_getadvisor)+" "+names[position]+"'s "+getResources().getString( R.string.wishlist_title1) );
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
holder.advisorContainer.setLayoutParams(lp);
if(advisors[position].contains("[")) {
try {
array = new JSONArray(advisors[position]);
System.out.println(array.toString(2));
// TODO Auto-generated method stub
// holder.advisorContainer.removeAllViews();
if(array!= null) {
for (int i = 0; i < array.length(); i++) {
JSONObject c = null;
try {
c = array.getJSONObject(i);
if( !advisorList.contains(c.getString("advisor_id"))) {
advisorList.add(c.getString("advisor_id"));
LinearLayout linearLayout_837 = new LinearLayout(WishList.this);
linearLayout_837.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout_837.setBackgroundColor(0xFFFFFFFF);
linearLayout_837.setOrientation(LinearLayout.HORIZONTAL);
linearLayout_837.setPadding(0, 5, 0, 0);
LinearLayout linearLayout_746 = new LinearLayout(WishList.this);
linearLayout_746.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layout_788 = new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.73f);
layout_788.leftMargin = 10;
linearLayout_746.setWeightSum(1);
linearLayout_746.setPadding(10, 0, 0, 0);
linearLayout_746.setLayoutParams(layout_788);
final TextView advisor_name_text = new TextView(WishList.this);
advisor_name_text.setEllipsize(TruncateAt.END);
advisor_name_text.setMaxLines(2);
advisor_name_text.setText(c.getString("name"));
advisor_name_text.setTextColor(getResources().getColor(R.color.Black));
LinearLayout.LayoutParams layout_527 = new LinearLayout.LayoutParams(
0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT,0.4f);
layout_527.gravity = Gravity.CENTER;
advisor_name_text.setLayoutParams(layout_527);
linearLayout_746.addView(advisor_name_text,layout_527);
TextView advisor_email_text = new TextView(WishList.this);
//advisor_email_text.setId(R.id.advisor_email_text);
advisor_email_text.setEllipsize(TruncateAt.END);
advisor_email_text.setMaxLines(2);
advisor_email_text.setText(c.getString("email"));
//advisor_email_text.setTypeface(tf);
advisor_email_text.setTextColor(getResources().getColor(R.color.Black));
LinearLayout.LayoutParams layout_908 = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT,0.6f);
advisor_email_text.setLayoutParams(layout_908);
linearLayout_746.addView(advisor_email_text,layout_908);
LinearLayout linearLayout_739 = new LinearLayout(WishList.this);
linearLayout_739.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layout_778 = new LinearLayout.LayoutParams(
0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.30f);
linearLayout_739.setLayoutParams(layout_778);
RelativeLayout relativeLayout_168 = new RelativeLayout(WishList.this);
RelativeLayout.LayoutParams layout_697 = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT
, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayout_168.setLayoutParams(layout_697);
final ImageButton deleteButton = new ImageButton(WishList.this);
deleteButton.setId(Integer.parseInt(c.getString("advisor_id")));
deleteButton.setBackgroundResource(R.drawable.wishlistremoveicon);
deleteButton.setContentDescription(getResources().getString(R.string.app_name));
RelativeLayout.LayoutParams layout_439 = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
//layout_439.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
layout_439.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout_439.rightMargin = 10;
deleteButton.setLayoutParams(layout_439);
TextView status_text = new TextView(WishList.this);
status_text.setText(c.getString("status"));
if(c.getString("status").equalsIgnoreCase("accepted"))
{
status_text.setTextColor(Color.GREEN);
}
else if(c.getString("status").equalsIgnoreCase("pending"))
{
status_text.setTextColor(getResources().getColor(R.color.Orange));
}
else
{
status_text.setTextColor(getResources().getColor(R.color.Red));
}
RelativeLayout.LayoutParams layout_315 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_315.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
layout_315.addRule(RelativeLayout.LEFT_OF, deleteButton.getId());
layout_315.rightMargin = 5;
status_text.setLayoutParams(layout_315);
relativeLayout_168.addView(deleteButton,layout_439);
relativeLayout_168.addView(status_text,layout_315);
linearLayout_739.addView(relativeLayout_168);
linearLayout_837.addView(linearLayout_746);
linearLayout_837.addView(linearLayout_739);
holder.advisorContainer.addView(linearLayout_837);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Click Operation
}
});
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
}
return view;
}
可以尽快帮助
答案 0 :(得分:1)
取消注释holder.advisorContainer.removeAllViews();
希望这会有用