我需要幫助。 讓說我有一個從數據庫中獲取列表視圖:從列表視圖中更改文本如果點擊
-- car
-- refrigerator
-- pencil
-- motorcycle
這就是我想要的。當我點擊汽車或其他選項時,它將變爲斷開。
-- broken
-- refrigerator
-- pencil
-- motorcycle
我
lv = (ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
TextView mTextView = (TextView) arg1;
mTextView.setText("broken");
//Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
}});
的問題是,當我再次使用後退按鈕,進入該頁面使用此之前,它會回到像以前一樣....
任何想法? ????
這裏是我完整的代碼
package com.test.first;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Listview extends Activity
{
SQLiteDatabase mydb;
ListView lv;
ArrayList data;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
//=====CODE FOR SHOWING THE DATA IN TOAST =======
lv = (ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
TextView mTextView = (TextView) arg1;
mTextView.setText(data.get(arg2) + " has been bought !");
//Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
}});
mydb = Listview.this.openOrCreateDatabase("My", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS list (id INTEGER PRIMARY KEY AUTOINCREMENT,list varchar);");
data = new ArrayList();
//=VIEW BUTTON TO SHOW DATA INSERTED IN THE LISTVIEW=======
Cursor cursor2=mydb.rawQuery("SELECT * FROM LIST;", null);
if (cursor2.moveToFirst())
{
Toast.makeText(getApplicationContext(), "Shop List", 3000).show();
data.clear();
do
{
data.add(cursor2.getString(cursor2.getColumnIndex("list")));
}
while (cursor2.moveToNext());
//====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================
ArrayAdapter <String> adapter=new ArrayAdapter<String> (Listview.this,android.R.layout.simple_list_item_1,data);
lv.setAdapter(adapter);
}
else
{
Toast.makeText(getApplicationContext(), "DATA NOT AVAILABLE", 3000).show();
}
cursor2.close();
}
}
如果您是從數據庫中取物品列表視圖,那麼你也應該在單擊某個項目更新數據庫。如果你不這樣做,你的應用程序將只從數據庫中獲取數據(原始數據)並向你顯示這些數據,而不是你編輯過的數據。 – Razgriz
你正在使用哪種類型的適配器 – 2013-07-29 10:03:00
是的,我知道這一點。這就是爲什麼我需要一些解決方案來更新只能從列表視圖 –