通过ListView循环两次

时间:2013-08-13 06:46:29

标签: android listview arraylist

我有自定义ListView的活动。我在每行中有两个TextView,其中一个包含静态文本,另一个包含在Button按下时随机更改的数字。我需要将两个TextView的数据保存在两个单独的ArrayList中(如果数字TextView的值不是0)。这些值按照我的意愿存储在ArrayLists中,但是记录被插入两次;这样当我循环遍历ArrayList并在Toast中显示它时,我获得输入行的值的两倍。

以下是我的代码段:

在按钮上单击添加Number TextView的值

holder.add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            int temp = numPickerValues.get(position);
            temp += 1;
            numPickerValues.set(position, temp);
            notifyDataSetChanged();
        }
    });
holder.num.setText(String.valueOf(numPickerValues.get(position)));

将非0 TextView的值添加到ArrayList

    if(!holder.num.getText().equals("0"))
    {
        materialNames.add(holder.txt.getText().toString());
        materialAmounts.add(holder.num.getText().toString());
    }

这是有趣的部分。 我调试了应用程序以检查问题所在,我发现它在ListView内循环两次,因此将值存储在ArrayLists中两次,但是我没有在ListView中重复这些值。一个TextView的重复值显示在另一个之后,因此它不会完全循环两次,否则值将被其他值分开。

知道发生了什么事吗?

显示值

public String getTest()
{
    test= "";

    for(String i : materialNames)
    {   
        test = test + " " + i;  
    }
    return test;
}

然后我从Button Click

上的另一个活动调用上述方法
btnConfirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                 Toast tt = Toast.makeText(getApplicationContext(), adapter.getTest(), Toast.LENGTH_LONG);
                 tt.show();
            }
        });

自定义适配器类

public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
String test = null;
TextView txtNum;

private ArrayList<MaterialClass> data;
private ArrayList<Integer> numPickerValues;
private ArrayList<String> materialNames;
private ArrayList<String> materialAmounts;

public static LayoutInflater inflater = null;

public static Dialog dialog;
String materialName;

public MaterialListViewAdapter(Context applicationContext,
        int materialdialogcontent, ArrayList<MaterialClass> materials) 
{
    this.data = materials;
    this.numPickerValues = new ArrayList<Integer>();
    this.materialNames = new ArrayList<String>();
    this.materialAmounts = new ArrayList<String>();

    int size = Material.materialList.size();

    for(int i=0; i < size; i++)
    {
        this.numPickerValues.add(0);
    }
    inflater = (LayoutInflater)applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) 
{
    return position;
}

@Override
public long getItemId(int position) 
{
    return position;
}

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

    if(convertView == null)
    {
        holder = new ViewHolder();

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

        holder.txt = (TextView)convertView.findViewById(R.id.txtMaterialName);
        holder.add = (Button)convertView.findViewById(R.id.btnAdd);
        holder.sub = (Button)convertView.findViewById(R.id.btnSub);
        holder.num = (TextView)convertView.findViewById(R.id.txtNum);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)convertView.getTag();
    }

    holder.txt.setText(data.get(position).getName());       

    holder.add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            int temp = numPickerValues.get(position);
            temp += 1;
            numPickerValues.set(position, temp);
            notifyDataSetChanged();
        }
    });

    holder.sub.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View v) 
        {
            int temp = numPickerValues.get(position);
            temp -= 1;
            numPickerValues.set(position, temp);
            notifyDataSetChanged(); 
        }
    });

    holder.num.setText(String.valueOf(numPickerValues.get(position)));

    if(!holder.num.getText().equals("0"))
    {
        materialNames.add(holder.txt.getText().toString());
        materialAmounts.add(holder.num.getText().toString());
    }

    return convertView;
}

public String getTest()
{
    test= "";

    for(String i : materialNames)
    {   
             test = test + " " + i;
    }
    return test;
}

private static class ViewHolder
{
    TextView txt;
    Button add;
    Button sub;
    TextView num;
}

}

1 个答案:

答案 0 :(得分:0)

if(!holder.num.getText().equals("0"))
{
    materialNames.add(holder.txt.getText().toString());
    materialAmounts.add(holder.num.getText().toString());
}

getView中的上述代码可能会运行多次。因此,您可以使用HashMap而不是materialNames和materialAmounts来避免重复。