1

listview動的に設定される を使用して電卓を作成しています。構築時(計算あり)に、onclickListener各項目にanを設定して行を削除します。これはすべて AdapterClass で行われます。すべての情報を保持する MainClass があります。このクラスでは、私のアイテムの現在の合計があります。この現在の合計には、現在の合計を加算/減算/取得するクラスがあります。現在の合計は、「CALCULATE」ボタンが押されたときにのみ設定されます。アイテム (行/計算) を削除すると、メイン クラスの現在の合計を更新する必要があります。onClickアイテムを削除するために使用される は、 myAdapterClassではなく my にあることに注意してくださいMainClass。さらに説明が必要な場合はお知らせください。

より良い説明

ここに画像の説明を入力

主な活動

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

実行中の合計

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;
}

UPDATE / this my CustomListAdapterwith a ViewHolderfrom Beginner

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

4

2 に答える 2

1

問題は、list.setOnItemClickListener を呼び出して、アダプターで layout.setOnClickListener ではなくアクティビティで onclick を接続する場合、リスト内のビューに on click を使用していることです。あなたが求めていることができないと言っているわけではありませんが、これはそれを修正するための推奨される方法です。

このようなもの(テキストビューが存在するメインのアクティビティで):

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

    }
});

アダプターを設定している場所を投稿したことはありませんが、リストビューの名前がわかりません。

于 2013-09-20T14:42:57.863 に答える
0

アダプタ クラスでは、ホルダーを使用し、ホルダーを使用してビューを更新しようとします。ここに例を示します。

 @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;
}

だから今、textview呼ばれるアップデートをしたいとしますtotal

ステップ1 のようなViewHolderクラスで合計を定義します

static class ViewHolder{ /* ここで更新するすべてのビューを定義します*/ TextView total;

}

ステップ2

getView() メソッドで ViewHolder のインスタンスを作成します。ViewHolder ホルダー;

ステップ3 ホルダーを使用して、onclickメソッドでテキストビューを更新します

holder.total.setText("設定したいテキスト");

于 2013-09-13T05:05:16.000 に答える