android - 自定义cursoradapter,是否存在onFinished / onComplete监听器

时间:2014-03-11 09:33:22

标签: android listener android-cursoradapter

我正在尝试在我的自定义cursoradapter中获取总值。我添加了一个方法,它返回一个值数组,我在片段中应用setadapter后调用它。

在适配器中,我还有另一种更新运行总计的方法,从bindview调用。

然而,我总是得到0,并且通过一些日志记录,即使我在应用适配器后调用getTotals,getTotals也会在处理适配器之前触发。

一旦适配器完成处理/添加视图以便我可以调用getTotals,我所知道的是一种了解方法吗?

第二个查询是调用bindView的次数。通过尝试调试它似乎虽然我的光标有6行,我的适配器调用bindview 60次?!?!所以无论如何我的总数都出错了。

public Dialog onCreateDialog(Bundle savedInstanceState) {

    mLayoutInflater = LayoutInflater.from(getActivity()); 

    db = new MyDatabase(getActivity());
    Cursor c = db.getMealDetails(getArguments().getInt("id"));

    ListView mealDetailsList = new ListView(getActivity());
    LinearLayout headerRow = (LinearLayout) mLayoutInflater.inflate(R.layout.meal_details_row, null, false);
    ((TextView) headerRow.findViewById(R.id.meal_details_row_size)).setText("Size");
    ((TextView) headerRow.findViewById(R.id.meal_details_row_cals)).setText("cals");
    ((TextView) headerRow.findViewById(R.id.meal_details_row_protein)).setText("P");
    ((TextView) headerRow.findViewById(R.id.meal_details_row_carb)).setText("C");
    ((TextView) headerRow.findViewById(R.id.meal_details_row_fat)).setText("F");
    mealDetailsList.addHeaderView(headerRow);




    MealDetailsAdapter mealDetailsAdapter = new MealDetailsAdapter(getActivity(),c);
    mealDetailsList.setAdapter(mealDetailsAdapter);

    mTotals = mealDetailsAdapter.getTotals();
    LinearLayout footerRow = (LinearLayout) mLayoutInflater.inflate(R.layout.meal_details_row, null, false);
    ((TextView) footerRow.findViewById(R.id.meal_details_row_cals)).setText(String.valueOf(mTotals[0]));
    ((TextView) footerRow.findViewById(R.id.meal_details_row_protein)).setText(String.valueOf(mTotals[1]));
    ((TextView) footerRow.findViewById(R.id.meal_details_row_carb)).setText(String.valueOf(mTotals[2]));
    ((TextView) footerRow.findViewById(R.id.meal_details_row_fat)).setText(String.valueOf(mTotals[3]));
    mealDetailsList.addFooterView(footerRow);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(mealDetailsList);


            // Create the AlertDialog object and return it
    return builder.create();
}

public class MealDetailsAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
private int[] mTotals;
private int l = 0;

public MealDetailsAdapter(Context context, Cursor c) {
    super(context, c);
    mContext = context;
    mLayoutInflater = LayoutInflater.from(context);
    mTotals = new int[] {0,0,0,0};
    Log.e("a","totals 0 ="+mTotals[0]);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = mLayoutInflater.inflate(R.layout.meal_details_row, parent, false);
    return v;
}

@Override
public void bindView(View v, Context context, Cursor c) {
    Log.e("a","view ="+v.toString());

    String name = c.getString(c.getColumnIndexOrThrow("name"));
    Float protein = Float.parseFloat(c.getString(c.getColumnIndexOrThrow("protein")));
    Float carb = Float.parseFloat(c.getString(c.getColumnIndexOrThrow("carb")));
    Float fat = Float.parseFloat(c.getString(c.getColumnIndexOrThrow("fat")));
    String grams = c.getString(c.getColumnIndexOrThrow("grams"));
    String size = c.getString(c.getColumnIndexOrThrow("quantity_text"));
    String qGram = c.getString(c.getColumnIndexOrThrow("quantity_gram"));
    float ratio = Float.parseFloat(qGram) / Float.parseFloat(grams);
    protein = protein * ratio;
    fat = fat * ratio;
    carb = carb * ratio;
    Float cals = (protein * 4) + (carb * 4) + (fat * 9);

    //String qGram = c.getString(c.getColumnIndexOrThrow("quantity_gram"));
    //String name = c.getString(c.getColumnIndexOrThrow("meal_name"));

    TextView nameTextView = (TextView) v.findViewById(R.id.meal_details_row_name);
    if (nameTextView != null) {
        nameTextView.setText(name);
    }

    TextView sizeTextView = (TextView) v.findViewById(R.id.meal_details_row_size);
    if (sizeTextView != null) {
        sizeTextView.setText(size);
    }

    TextView proteinTextView = (TextView) v.findViewById(R.id.meal_details_row_protein);
    if (proteinTextView != null) {
        proteinTextView.setText(String.format("%.0f", protein));
    }

    TextView carbTextView = (TextView) v.findViewById(R.id.meal_details_row_carb);
    if (carbTextView != null) {
        carbTextView.setText(String.format("%.0f", carb));
    }

    TextView fatTextView = (TextView) v.findViewById(R.id.meal_details_row_fat);
    if (fatTextView != null) {
        fatTextView.setText(String.format("%.0f", fat));
    }

    TextView calsTextView = (TextView) v.findViewById(R.id.meal_details_row_cals);
    if (calsTextView != null) {
        calsTextView.setText(String.format("%.0f", cals));
    }
    int[] tempTotals = new int[]{Math.round(cals),Math.round(protein),Math.round(carb),Math.round(fat)};
    updateTotals(tempTotals);

}

public int[] getTotals() {

    Log.e("a","get totals 0 ="+mTotals[0]);
    return mTotals;
}

public void updateTotals(int[] totals) {


    Log.e("a","l ="+l);
    l++;
    Log.e("a","up totals 0 ="+mTotals[0]);
    Log.e("a","mtotals ="+mTotals.toString());
    Log.e("a","totals ="+totals[0]+" totals ="+totals[1]+"totals ="+totals[2]+"totals ="+totals[3]);
    for (int i = 0; i < totals.length; ++i) {
        mTotals[i] += totals[i];
    }

}

1 个答案:

答案 0 :(得分:2)

你不能以这种方式得到总数。适配器中的BindView仅在列表绘制时才会调用。 即使你将在绘制之后调用getTotals,你也会得到错误的答案,因为适配器只会为屏幕上的项调用bindView。

如我所见,您只将cursorAdapter和ListView用于一行。这不是好主意。 ListView适用于大量行。它非常复杂,可以重复使用行和其他优化技术来处理大数据。

在您的情况下,最好的方法是使用LayoutInflater从xml中提取视图并绑定数据,就像您为标题视图一样。

编辑: 如果你真的有一行,那么最好的方法是进行另一个SQL查询:

SELECT SUM(cal), SUM(carb), SUM(fat) FROM ... WHERE ...

并从该查询中获取数据,并在bindView中进行make计算。