我正在构建一个带有动态设置的 listview
的计算器。在构建(通过计算)时,为每个项目设置一个 onclickListener
以删除该行。这一切都是通过 AdapterClass 完成的。我的 MainClass 包含所有信息。在这个类中,它有我的项目的总计。此运行总计有一个类将添加/减去/检索我的运行总计。只有在按下“计算”按钮时才会设置运行总计(截至目前)。当我删除一个项目(行/计算)时,我需要它来更新我的主类中的运行总计。请记住,用于删除项目的 onClick
在我的 AdapterClass
中,而不是在我的 MainClass
中。如果您需要进一步的解释,请告诉我。
更好的解释
主 Activity
//This is called in my CALCULATE on click, after all the calulations have been made. THE ONLY PLACE THAT THE RUNNING TOTAL GETS CHANGED
newList=new ArrayList<Calculations>();
Calculations info = new Calculations();
info.SetType("Slab figure "+figureCount);
info.SetFigure(width+"x"+length+"x"+depth);
info.SetFigureAmount(String.format("%.2f",CubicYd)+"");
newList.add(info);
currentTotal.add(CubicYd);
total.setText(String.format("Total: "+"%.2f",currentTotal.getRunningTotal())+" Cubic Yards");
if(newList!=null&&newList.size()>0)
{
newAdapter.notifyDataSetChanged();
newAdapter.add(newList.get(0));
i++;
}
newAdapter.notifyDataSetChanged();
runningTotal
public class runningTotal {
double runningTotal = 0.0;
public double add(double newAmount) {
runningTotal = runningTotal + newAmount;
return runningTotal;
}
public double sub(double newAmount) {
runningTotal = runningTotal - newAmount;
return runningTotal;
}
public double getRunningTotal() {
return runningTotal;
}
public void setRunningTotal() {
runningTotal = 0.0;
}
}
列表适配器
public class CustomListAdapter extends ArrayAdapter<Calculations> {
runningTotal currentTotal = new runningTotal();
Calculations c = new Calculations();
private Context appContext = null;
private ArrayList<Calculations> items = null;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<Calculations> items) {
super(context, textViewResourceId, items);
this.appContext = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) appContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
c = items.get(position);
if (c != null) {
//Set the calculations to the list view
TextView type = (TextView) v.findViewById(R.id.tvType);
TextView figure = (TextView) v.findViewById(R.id.tvFullFigure);
TextView amount = (TextView) v.findViewById(R.id.tvAmount);
RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.list_item_id);
layout.setTag(position);
//set on click to the layout that deletes the line.
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pos = view.getTag().toString();
int _position = Integer.parseInt(pos);
currentTotal.sub(Double.parseDouble(c.getFigureAmount()));
<----This is where I need to update the textView "total" in my mainAcitivity----->
items.remove(_position);
notifyDataSetChanged();
Toast.makeText(appContext, "The amount to be released is " + Double
.parseDouble(c.getFigureAmount()), Toast.LENGTH_LONG).show();
}
});
if (type != null) {
type.setText(c.getType());
}
if (figure != null) {
figure.setText(c.getFigure());
}
if (amount != null) {
amount.setText(c.getFigureAmount());
}
}
return v;
}
}
计算
private String type = "";
private String figure = "";
private String figureTotal = "";
public void SetType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
public void SetFigure(String figure) {
this.figure = figure;
}
public String getFigure(){
return this.figure;
}
public void SetFigureAmount(String figureTotal){
this.figureTotal = figureTotal;
}
public String getFigureAmount(){
return this.figureTotal;
}
更新/这是我的 CustomListAdapter
和初学者的 ViewHolder
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
holder.text = (TextView) v.findViewById(R.id.tvTotal);
v = vi.inflate(R.layout.list_item, null);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(currentTotal.getRunningTotal()+"");
c = items.get(position);
if (c != null){
//Set the calculations to the list view
TextView type = (TextView) v.findViewById(R.id.tvType);
TextView figure = (TextView) v.findViewById(R.id.tvFullFigure);
TextView amount = (TextView) v.findViewById(R.id.tvAmount);
RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.list_item_id);
layout.setTag(position);
//set on click to the layout that deletes the line.
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String pos = view.getTag().toString();
int _position = Integer.parseInt(pos);
double newTotal;
double oldTotal;
oldTotal = Double.parseDouble(c.getFigureAmount());
newTotal = currentTotal.getRunningTotal() - oldTotal;
currentTotal.setRunningTotal(newTotal);
holder.text.setText("total after delete" );
items.remove(_position);
notifyDataSetChanged();
}
});
if(type!=null) {
type.setText(c.getType());
}
if(figure!=null){
figure.setText(c.getFigure());
}
if(amount !=null){
amount.setText(c.getFigureAmount());
}
}
return v;
}
static class ViewHolder{
TextView text;
}
}
我在行中得到 nullPointer
holder.text = (TextView) v.findViewById(R.id.tvTotal);
最佳答案
问题是,如果您调用 list.setOnItemClickListener 并在您的 Activity 中连接您的 onclick,而不是在您的适配器上连接 layout.setOnClickListener,那么您正在对列表中的 View 使用点击。请注意,我并不是说您不能按照您的要求去做,但这是解决问题的推荐方法。
像这样的东西(在 TextView 所在的主 Activity 上):
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Calculations c = (Calculations) parent.getAdapter().getItem(position);
currentTotal.sub(Double.parseDouble(c.getFigureAmount()));
total.setText(String.format("Total: "+"%.2f",currentTotal.getRunningTotal())+" Cubic Yards");
items.remove(_position);
notifyDataSetChanged();
Toast.makeText(appContext, "The amount to be released is " + Double
.parseDouble(c.getFigureAmount()), Toast.LENGTH_LONG).show();
}
});
尽管您从未发布过您设置适配器的位置,所以我不确定您的 ListView 叫什么。
关于android - 用另一个类更改一个类的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777607/