1

i need help here. Lets say that I have a listview taken from database :

-- car

-- refrigerator

-- pencil

-- motorcycle

This is what i want. When i clicked at car or other option, it will change to broken.

-- broken

-- refrigerator

-- pencil

-- motorcycle

I used this before

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();
 }});

the problem is when i use back button and go to that page again, it will be back like before....

Any idea ?????

Here is my full code

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();
}
}
4

1 回答 1

0

问题是当您按下后退按钮然后重新打开活动时,会使用原始信息重新创建 ListView。此信息不会更改并包含汽车文本,因此 ListView 其他时间包含汽车文本是正常的。
您应该更新您的列表信息,然后调用修改 ListView:

 adapter.notifyDataSetChanged()

如果您更改信息,然后按返回并重新打开 Activty,ListView 就可以了。

于 2013-07-29T10:20:15.127 回答