【问题标题】:Changing a text view on one class with another class用另一个类更改一个类的文本视图
【发布时间】:2013-09-17 15:03:24
【问题描述】:

我正在构建一个带有动态设置的listview 的计算器。构建时(通过计算),为每个项目设置一个onclickListener 以删除该行。这一切都是通过 AdapterClass 完成的。我有包含所有信息的 MainClass。在这堂课中,它有我的项目总数。这个运行总计有一个类可以添加/减去/检索我的运行总计。运行总计(截至目前)仅在按下“计算”按钮时设置。当我删除一个项目(行/计算)时,我需要它来更新我在主类中的运行总计。请记住,用于删除项目的onClick 在我的AdapterClass 中,而不是我的MainClass 中。如果您需要进一步的解释,请告诉我。

更好的解释

MainActivity

//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);

【问题讨论】:

  • 您需要从哪个类更改哪个文本视图??..
  • 我需要更改MainActivity 中名为total 的文本视图。这将通过onClick 中的列表适配器类完成
  • 您的项目中还有其他活动课程吗?
  • 是的,只有一个,我有一个叫做计算的类。会发的查看修改
  • 在Adapterclass中创建一个主Activity对象,并使用settext方法。确保您的总数被定义为公开

标签: android class listview dynamic textview


【解决方案1】:

问题是,如果您调用 list.setOnItemClickListener 并将 onclick 连接到您的活动中,而不是适配器上的 layout.setOnClickListener,则问题是您正在使用 on click 来查看列表中的视图。请注意,我并不是说您不能做您要求做的事情,但这是修复它的推荐方法。

类似这样的东西(在文本视图所在的主 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();

    }
});

虽然您从未发布过设置适配器的位置,所以我不确定您的列表视图被称为什么。

【讨论】:

  • 我明白你在说什么。我不确定我知道该怎么做。您有任何链接或示例,以便我进行研究吗?
【解决方案2】:

在适配器类中使用持有人并尝试使用持有人更新 veiws.. 这是一个示例

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

    // A ViewHolder keeps references to children views to avoid unneccessary
    // calls
    // to findViewById() on each row.
    ViewHolder holder;
    // When convertView is not null, we can reuse it directly, there is no
    // need
    // to reinflate it. We only inflate a new View when the convertView
    // supplied
    // by ListView is null.

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.sample, null);
        // Creates a ViewHolder and store references to the two children
        // views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.

        holder = (ViewHolder) convertView.getTag();

    }

    // Bind the data efficiently with the holder.
    holder.name.setText(myElements.get(id));
    holder.icon.setImageBitmap(mIcon1);

    return convertView;
}

static class ViewHolder {
    TextView  name;
    ImageView icon;
}

所以现在,假设您要更新名为 totaltextview

第 1 步 在 ViewHolder 类中定义总计,如

静态类 ViewHolder{ /* 在此处定义要更新的所有视图 */ TextView 总数;

}

第 2 步

在 getView() 方法中创建 ViewHolder 的实例。 ViewHolder 支架;

第 3 步 使用持有人在 onclick 方法中更新您的文本视图

holder.total.setText("你要设置的文字");

【讨论】:

  • 我的 ViewHolder 类中应该有什么?
  • 你需要膨胀的视图
  • 我应该如何访问该文本视图?我需要更好的解释
  • 如果我试图更改的文本视图不在我的 MainActivity 类中,这一切都会起作用。那是我的问题。我无法在我的适配器类中访问它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多