如何计算Android中listView中的总项数?

时间:2014-08-17 10:22:14

标签: android android-listview count textview

我有一个ListView,我想在textView中显示其中的项目总数。它有效,但它没有计算任何东西。我的意思是它总是保持0,即使我删除或添加项目。谢谢!

   package ro.radioamatori;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


@SuppressLint("ShowToast")
public class Absente extends Activity {

    private DbHelper_absente mHelpera;
    private SQLiteDatabase dataBase;
    TextView n;




    private ArrayList<String> userId = new ArrayList<String>();
    private ArrayList<String> user_fName = new ArrayList<String>();
    private ArrayList<String> user_lName = new ArrayList<String>();
    private ArrayList<String> d = new ArrayList<String>();
    private ArrayList<String> stime = new ArrayList<String>();
    private ArrayList<String> etime = new ArrayList<String>();
    private ArrayList<String> freq = new ArrayList<String>();
    private ArrayList<String> mode = new ArrayList<String>();
    private ArrayList<String> station = new ArrayList<String>();
    private ArrayList<String> loc = new ArrayList<String>();
    private ArrayList<String> tqsl = new ArrayList<String>();
    private ArrayList<String> mqsl = new ArrayList<String>();
    private ArrayList<String> comm = new ArrayList<String>();

    private ListView userLista;
    private AlertDialog.Builder build;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.absente_listview);

        userLista = (ListView) findViewById(R.id.Lista);

        TextView t = (TextView) findViewById(R.id.t);

        t.setText(String.valueOf(userLista.getCount()));

        int x = (userLista.getCount());


        mHelpera = new DbHelper_absente(this);

        //add new record
        findViewById(R.id.btnAdda).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),
                        Adauga_absente.class);
                i.putExtra("update", false);
                startActivity(i);

            }
        });

        //click to update data
        userLista.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Intent i = new Intent(getApplicationContext(),
                        Adauga_absente.class);
                i.putExtra("Fname", user_fName.get(arg2));
                i.putExtra("Lname", user_lName.get(arg2));
                i.putExtra("ID", userId.get(arg2));
                i.putExtra("date", d.get(arg2));
                i.putExtra("date", stime.get(arg2));
                i.putExtra("date", etime.get(arg2));
                i.putExtra("date", freq.get(arg2));
                i.putExtra("date", mode.get(arg2));
                i.putExtra("date", station.get(arg2));
                i.putExtra("date", loc.get(arg2));
                i.putExtra("date", tqsl.get(arg2));
                i.putExtra("date", mqsl.get(arg2));
                i.putExtra("date", comm.get(arg2));
                i.putExtra("update", true);
                startActivity(i);

            }
        });

        //long click to delete data
        userLista.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    final int arg2, long arg3) {

                build = new AlertDialog.Builder(Absente.this);
                build.setTitle("Esti sigur ca vrei sa stergi? ");
                build.setMessage("Esti sigur ca vrei sa stergi aceasta inregistrare ?");
                build.setPositiveButton("Da",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                Toast.makeText(
                                        getApplicationContext(), "Inregistrarea a fost stearsa", 3000).show();

                                dataBase.delete(
                                        DbHelper.TABLE_NAME,
                                        DbHelper.KEY_ID + "="
                                                + userId.get(arg2), null);
                                displayData();
                                dialog.cancel();
                            }
                        });

                build.setNegativeButton("Nu",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = build.create();
                alert.show();

                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        displayData();
        super.onResume();
    }

    /**
     * displays data from SQLite
     */
    private void displayData() {
        dataBase = mHelpera.getWritableDatabase();
        Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
                + DbHelper.TABLE_NAME, null);

        userId.clear();
        user_fName.clear();
        user_lName.clear();
        d.clear();
        stime.clear();
        etime.clear();
        freq.clear();
        mode.clear();
        station.clear();
        loc.clear();
        tqsl.clear();
        mqsl.clear();
        comm.clear();
        if (mCursor.moveToFirst()) {
            do {
                userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
                user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
                user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
                d.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_D)));
                stime.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_STIME)));
                etime.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ETIME)));
                freq.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FREQ)));
                mode.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_MODE)));
                station.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_STATION)));
                loc.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LOC)));
                tqsl.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_TQSL)));
                mqsl.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_MQSL)));
                comm.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_COMM)));


            } while (mCursor.moveToNext());
        }
        NoteAdapter disadpt = new NoteAdapter(Absente.this,userId, user_fName, user_lName, d, stime, etime, freq, mode, station, loc, tqsl, mqsl, comm);
        userLista.setAdapter(disadpt);
        mCursor.close();
    }



}

3 个答案:

答案 0 :(得分:1)

模型对象由适配器管理,而不是由ListView(仅负责显示项目)管理。请尝试以下方法:

userLista.getAdapter().getCount();

答案 1 :(得分:1)

在检索数据之前,您正试图计算。

displayData()方法结束时更新TextView。

修改

private void displayData() {
    ...

    TextView t = (TextView) findViewById(R.id.t);
    t.setText(String.valueOf(userLista.getCount()));
}

答案 2 :(得分:0)

每次商品计数发生变化时,您都需要更新TextView,例如displayData()结束时。