在Android中动态添加和删除ListView中的TextFields

时间:2014-02-03 13:15:28

标签: android listview android-listview

根据要求,我有ListView,每行包含多个TextView

我正在使用CustomAdapter。根据数据(来自服务器),我必须删除 一些TextViews并在ListView的每一行中保持其他人在运行时。删除的TextViews 必须显示在下一行,具体取决于数据。

我已尝试View.Gone,但问题仍然存在。

我的代码是:

public class CustomListViewFBOAdapter extends ArrayAdapter<XYZ>   {
Context context;
List<XYZ> rowItem;
    LinearLayout linearLayout;

TextView a,b,c,d;

Viewholder holder = null;


public CustomListViewFBOAdapter(Context context, int resource,
        List<XYZ> objects) {

    super(context, resource, objects);
    this.context = context;
    this.rowItem = objects;

}

private class Viewholder {  
    TextView aValue;
    TextView bValue;
    TextView cValue;
    TextView dVAlue;
}

@Override
public int getCount() {
    return rowItem.size();
}

@Override
public FBODetails getItem(int position) {
    return rowItem.get(position);
}

@Override
public long getItemId(int position) {
    return rowItem.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    XYZ item = (XYZ) getItem(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {

        convertView = inflater.inflate(R.layout.row, null);

        linearLayout= (LinearLayout)convertView.findViewById(R.id.xmllayout);

        a = (TextView)convertView.findViewById(R.id.a);
        b = (TextView)convertView.findViewById(R.id.b);
        c = (TextView)convertView.findViewById(R.id.c);
        d = (TextView)convertView.findViewById(R.id.d);

        holder = new Viewholder();


        holder.aValue= (TextView) convertView.findViewById(R.id.avalue);
        holder.bValue= (TextView) convertView.findViewById(R.id.bValue);
        holder.cvalue= (TextView) convertView.findViewById(R.id.cvalue);
        holder.dvalue= (TextView) convertView.findViewById(R.id.dvalue);

        convertView.setTag(holder);

    } else {

        holder = (Viewholder) convertView.getTag();

    }




    if(item.getA().equals("$ 0.00"))
    {
        if(holder.aValue.getVisibility() == View.VISIBLE && a.getVisibility() == View.VISIBLE)
        {
           a.setVisibility(View.GONE);
           holder.aValue.setVisibility(View.GONE);
        }
    }
    else 
    {
        holder.aValue.setVisibility(View.VISIBLE);
         a.setVisibility(View.VISIBLE);
         holder.aValue.setText(item.getA());    
    }


    if(item.getB().equals("$ 0.00"))
    {
        if(holder.bValue.getVisibility() == View.VISIBLE && b.getVisibility() == View.VISIBLE)
        {
           b.setVisibility(View.GONE);
           holder.bValue.setVisibility(View.GONE);
        }
    }
    else 
    {
        holder.bValue.setVisibility(View.VISIBLE);
         b.setVisibility(View.VISIBLE);
         holder.bValue.setText(item.getB());    
    }   

            if(item.getC().equals("$ 0.00"))
    {
        if(holder.cValue.getVisibility() == View.VISIBLE && c.getVisibility() == View.VISIBLE)
        {
           c.setVisibility(View.GONE);
           holder.cValue.setVisibility(View.GONE);
        }

    }
    else 
    {
        holder.cValue.setVisibility(View.VISIBLE);
         c.setVisibility(View.VISIBLE);
         holder.cValue.setText(item.getC());    
    }   


            if(item.getD().equals("$ 0.00"))
    {
        if(holder.dValue.getVisibility() == View.VISIBLE && d.getVisibility() == View.VISIBLE)
        {
           d.setVisibility(View.GONE);
           holder.dValue.setVisibility(View.GONE);
        }
    }
    else 
    {
        holder.dValue.setVisibility(View.VISIBLE);
         d.setVisibility(View.VISIBLE);
         holder.dValue.setText(item.getD());    
    }       


    return convertView;
}

}

感谢您的回复。

3 个答案:

答案 0 :(得分:1)

添加使用代码,例如

    RelativeLayout relative = new RelativeLayout(getApplicationContext());
    TextView msgtxt= new TextView(getApplicationContext());
    TextView timetxt= new TextView(getApplicationContext());

    // Msg textview
    RelativeLayout.LayoutParams paramsTxt = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


    //msgtxt.setLayoutParams(paramsTxt);
    msgtxt.setText(msgArrayList.get(pos));
    msgtxt.setTextSize(20);
    msgtxt.setTextColor(getResources().getColor(R.color.white));
    msgtxt.setBackgroundResource(R.drawable.chats);
    msgtxt.setPadding(35,10, 10, 10);

    // RelativeLayout parent of msg textview
    LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
    relativeParams.weight=85;
    //relative.setBackgroundResource(R.drawable.chats);
    relative.addView(msgtxt,paramsTxt);

并删除:

relative.removeView(msgtxt);

答案 1 :(得分:1)

试试这段代码,

 btnOrImage.setOnClickListener(new OnClickListener() 
 {
    @Override
    public void onClick(View v) 
    {
        // delete query fire here whatever related to requirements.
        mList.remove(position);//you can delete your item here
        notifyDataSetChanged();
    }
});

此方法用于从BaseAdapter中将notifyDataSetChanged()刷新到getView()方法

答案 2 :(得分:0)

尝试将TextView a,b,c和d放在实际布局中,从而放入ViewHolder中。然后检查它们的可见性(相应地显示/隐藏它们)。或者添加动态创建的TextView作为@Nand建议。 另外,尝试为TextView设置标签,以便即使在滚动后也能保持其状态。

以下简单的片段为我完成了这项工作。

public class MainActivity extends Activity {

String s[] = {"Meme1","Meme2","Meme3","Meme4","Meme5"};
String s1[] = {"Meme1","Meme2","Meme3","Meme4","Meme5"};
MyAdapter adapter;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.list);
    adapter = new MyAdapter(getApplicationContext(), R.layout.layout,s);
    list.setAdapter(adapter);
}



class MyAdapter extends ArrayAdapter<String> {

    LayoutInflater inflater;
        public MyAdapter(Context context, int resource, String [] data) {
        super(context, resource, data);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null){
        inflater = LayoutInflater.from(getApplicationContext());
            convertView = inflater.inflate(R.layout.layout, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv1.setText(s[position]);
        holder.tv2.setText(s1[position]);

        if(position == 1){
            holder.tv1.setVisibility(View.GONE);
        }else if(position == 2){
            TextView tv = new TextView(getApplicationContext());
            tv.setText("Hello");
            holder.linear.addView(tv);
        }

        return convertView;
    }

    private class ViewHolder{
        TextView tv1, tv2;
        LinearLayout linear;

        public ViewHolder(View v) {
            linear = (LinearLayout) v.findViewById(R.id.linear);
            tv1 = (TextView) v.findViewById(R.id.textView1);
            tv2 = (TextView) v.findViewById(R.id.textView2);
        }
    }

    }

}