Reputation: 89
I have a ListView and I would like to have the total number of items in it showed in a textView. It works, but it's not counting anything. I mean it remains 0 all the time, even if I delete or add an item. Thanks !
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();
}
}
Upvotes: 0
Views: 5884
Reputation: 544
You are trying to get count before retrieving data.
Update TextView at the end of displayData()
method.
Edit.
private void displayData() {
...
TextView t = (TextView) findViewById(R.id.t);
t.setText(String.valueOf(userLista.getCount()));
}
Upvotes: 1
Reputation: 2319
You have to update your TextView
every time the item count changes, e.g., at the end of displayData()
.
Upvotes: 0
Reputation: 94
The model objects are managed by the adapter, not by the ListView (which is only responsible for displaying the items). Try the following:
userLista.getAdapter().getCount();
Upvotes: 1