我有问题,请帮忙。我希望每次单击单选按钮时ListView
更新(刷新)。任何有用的帮助都将受到欢迎。
public class thirdActivity extends Activity {
SQLiteDatabase db;
String date, gorivo, kolicina, cijena, litra, ID, gor;
Integer index=0;
RadioButton vozilo1, vozilo2, vozilo3;
int vozilo= 1;
public ArrayList<HashMap<String, String>> list;
ListViewAdapter adapter=new ListViewAdapter(this, list);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
ListView listView=(ListView)findViewById(R.id.listView1);
adapter.notifyDataSetChanged();
populateList();
adapter.notifyDataSetChanged();
ListViewAdapter adapter=new ListViewAdapter(this, list);
listView.setAdapter(adapter);
vozilo1 = (RadioButton)findViewById(R.id.radio000);
vozilo2 = (RadioButton)findViewById(R.id.radio100);
vozilo3 = (RadioButton)findViewById(R.id.radio200);
OnClickListener listenervoz1 = new OnClickListener() {
@Override
public void onClick(View v) {
vozilo=1;
populateList();
}
};
OnClickListener listenervoz2 = new OnClickListener() {
@Override
public void onClick(View v) {
vozilo=2;
populateList();
}
};
OnClickListener listenervoz3 = new OnClickListener() {
@Override
public void onClick(View v) {
vozilo=3;
populateList();
}};
Button btnvoz1 = (Button) findViewById(R.id.radio000);
btnvoz1.setOnClickListener(listenervoz1);
Button btnvoz2 = (Button) findViewById(R.id.radio100);
btnvoz2.setOnClickListener(listenervoz2);
Button btnvoz3 = (Button) findViewById(R.id.radio200);
btnvoz3.setOnClickListener(listenervoz3);
}
public void populateList() {
// TODO Auto-generated method stub
Integer i = null;
TextView ukupnoCijena = (TextView)findViewById(R.id.ukupno_cijena);
TextView ukupnoKolicina = (TextView)findViewById(R.id.ukupnoKolicina);
TextView tridesetCijena = (TextView)findViewById(R.id.tridesetCijena);
TextView tridesetKolicina = (TextView)findViewById(R.id.tridesetkol);
//TextView sveUkupnoC= (TextView)findViewById(R.id.ukupno_cijena0);
//TextView sveUkupnoK = (TextView)findViewById(R.id.ukupnoKolicina0);
list=new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp=new HashMap<String, String>();
temp.put(FIRST_COLUMN, "ID");
temp.put(SECOND_COLUMN, "DATUM");
temp.put(THIRD_COLUMN, "KOLICINA l");
temp.put(FOURTH_COLUMN, "CIJENA Kn");
temp.put(FIFTH_COLUMN, "Go");
list.add(temp);
db=openOrCreateDatabase("PotrosnjaDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS potrosnjaDB (" +
"ID INTEGER PRIMARY KEY, " +
"datum DATE ," +
"gorivo VARCHAR(10) ,"+
"kolicina NUMBER ," +
"cijena DECIMAL(4,2) ," +
"vozilo NUMBER)"
);
Cursor c=db.rawQuery("SELECT * FROM potrosnjaDB where vozilo="+vozilo+" ;", null);
if(c.moveToFirst())
{
for ( i= c.getCount(); i>0; i--) {
ID=c.getString(0);
date= c.getString(1).replace("-", ".");
gor =c.getString(2).substring(0, 2);
cijena = c.getString(3);
kolicina = c.getString(4);
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put(FIRST_COLUMN, ID); //
temp1.put(SECOND_COLUMN, date); //
temp1.put(THIRD_COLUMN, cijena);
temp1.put(FOURTH_COLUMN, kolicina); //
temp1.put(FIFTH_COLUMN, gor);
list.add(temp1);
c.moveToNext();
}
}
else {
Toast.makeText(thirdActivity.this, "Podaci nisu uneseni", Toast.LENGTH_SHORT).show();
}
c=db.rawQuery("SELECT SUM(cijena) FROM potrosnjaDB where vozilo="+vozilo+" ;", null);
if(c.moveToFirst())
{
ukupnoCijena.setText(c.getString(0)+ " kn.");
}
else {showMessage("Greška", "Podaci nisu pronađeni");}
c=db.rawQuery("SELECT SUM(kolicina) FROM potrosnjaDB where vozilo="+vozilo+" ;", null);
if(c.moveToFirst())
{
ukupnoKolicina.setText(c.getString(0)+ " lit.");
}
else {showMessage("Greška", "Podaci nisu pronađeni");}
c=db.rawQuery("SELECT SUM(cijena) FROM potrosnjaDB where datum >= date('now', '-30 days') AND vozilo="+vozilo+" ;", null);
if(c.moveToFirst())
{
tridesetCijena.setText(c.getString(0)+ " kn.");
}
else {showMessage("Greška", "Podaci nisu pronađeni");}
c=db.rawQuery("SELECT SUM(kolicina) FROM potrosnjaDB where datum >= date('now', '-30 days') AND vozilo="+vozilo+" ;", null);
if(c.moveToFirst())
{
tridesetKolicina.setText(c.getString(0)+ " lit.");
}
else {showMessage("Greška", "Podaci nisu pronađeni");}
/*c=db.rawQuery("SELECT SUM(cijena) FROM potrosnjaDB ;", null);
if(c.moveToFirst())
{
sveUkupnoC.setText(c.getString(0)+ " kn.");
}
else {showMessage("Greška", "Podaci nisu pronađeni");}
c=db.rawQuery("SELECT SUM(kolicina) FROM potrosnjaDB ;", null);
if(c.moveToFirst())
{
sveUkupnoK.setText(c.getString(0)+ " lit.");
}
else {showMessage("Greška", "Podaci nisu pronađeni");} */
}
private void showMessage(String title, String message) {
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setPositiveButton("OK", null);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
如果我点击广播1,我希望ListView
显示SQLite
vozilo1
,rado2
vozilo2
等数据
答案 0 :(得分:2)
确保在每次时调用adapter.notifyDataSetChanged()
时更改ListView
的模型。
我注意到在您的OnClickListener
中,您更改了vozilo
变量(顺便说一下,它应该作为参数传递给您的populateList()
方法),调用{{1}但是,永远不要让你的适配器知道你的列表发生了变化。