我有一个自定义的活动ListView
。我TextView
每行有两个 s,其中一个包含静态文本,另一个包含在Button
印刷机上随机更改的数字。我需要将两个TextView
s的数据保存在两个单独ArrayList
的s中(如果数字的值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 TextViews 的值添加到 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;
}
然后我从按钮单击的另一个活动中调用上述方法
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;
}
}