みんな私は簡単なリストビューを使用しています。メインと友達を追加する2つのアクティビティがあります。2番目のアクティビティでは、テキストボックスで友達のユーザーIDを取得し、その行をデータベースに追加して、配列リストを更新します。メインアクティビティに戻ったら、onResume()関数でnotifyDataSetChanged()を呼び出し、arraylist値もログに記録します。Arraylistは非常に細かく、行はデータベースに挿入されますが、listviewは更新されません。私は問題のplzの助けをしません。これがdコードです。
public class VayuActivity extends Activity {
/** Called when the activity is first created. */
static boolean first_time=true;
ListView friend_list;
public static List<String> al;
public ArrayAdapter<String> aa;
String dummyfriends[]={"prashant","monika","pramod","dhara"};
public static MessengerDataSource mds;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* if(first_time==true)
{
first_time=false;
setContentView(R.layout.registration);
}
else */
setContentView(R.layout.main);
al=new ArrayList<String>();
for(String i:dummyfriends)
al.add(i);
aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, al);
friend_list=(ListView) findViewById(R.id.friend_list);
friend_list.setAdapter(aa);
mds= new MessengerDataSource(getApplicationContext());
mds.open();
System.out.println("I m in oncreate");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
al=mds.getFriends();
aa.notifyDataSetChanged();
System.out.println("I m in on resume function");
System.out.println("Value of arraylist in on resume fun"+al);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("I m in onpause function");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater mi=getMenuInflater();
mi.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.add_friend:
//Toast.makeText(this, "Add Friend", 2000).show();
Intent i=new Intent(VayuActivity.this,AddFriendActivity.class);
startActivity(i);
break;
case R.id.remove_friend:
Toast.makeText(this, "Remove Friend", 2000).show();
break;
}
return true;
}
}
package com.pdd.vayu;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AddFriendActivity extends Activity implements OnClickListener{
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addfriend);
et=(EditText) findViewById(R.id.E_friendadd);
Button b=(Button) findViewById(R.id.add_friend_button);
b.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String userid=et.getText().toString();
//VayuActivity.al.add(f);
Friend f=new Friend();
f.setUserid(userid);
f.setFname("pratik");
VayuActivity.mds.addFriend(f);
VayuActivity.al=(ArrayList<String>) VayuActivity.mds.getFriends();
System.out.println(VayuActivity.al);
//VayuActivity.aa.notifyDataSetChanged();
}
}
データベースクラス
package com.pdd.vayu;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MessengerDataSource {
private SQLiteDatabase database;
private MyDatabaseHelper mydbh;
public static String USER_ID="userid";
public static String F_NAME="fname";
public static String CHAT_COUNT="chat_count";
public static String M_TEXT="mtext";
public static String SENDER="sender";
public static String RECEIVER="receiver";
public static String M_TYPE="type";
public MessengerDataSource(Context c)
{
mydbh=new MyDatabaseHelper(c);
}
public void open()
{
try
{
database=mydbh.getWritableDatabase();
}
catch(Exception e)
{
System.out.println(e.getLocalizedMessage());
}
}
public void close() {
mydbh.close();
}
public void addFriend(Friend f)
{
ContentValues cv = new ContentValues();
cv.put(USER_ID,f.getUserid());
cv.put(F_NAME, f.getFname());
cv.put(CHAT_COUNT, f.getChat_count());
try
{
database.insert(MyDatabaseHelper.TABLE_FRIEND, null, cv);
}
catch(Exception e)
{
System.out.println("Error while inserting "+e.getLocalizedMessage());
}
}
public void addMessage(Message m)
{
ContentValues cv = new ContentValues();
cv.put(M_TEXT, m.getText());
cv.put(SENDER,m.getSender());
cv.put(RECEIVER,m.getReceiver());
cv.put(M_TYPE, m.getType());
database.insert(MyDatabaseHelper.TABLE_MESSAGE, null, cv);
}
public void deleteFriend(String fname)
{
database.delete(MyDatabaseHelper.TABLE_FRIEND,F_NAME+"="+fname , null);
}
public void deleteMessage(String text)
{
database.delete(MyDatabaseHelper.TABLE_FRIEND,M_TEXT+"="+text , null);
}
public List<String> getFriends()
{
List<String> friends= new ArrayList<String>();
Cursor c=null;
try
{
c=database.query(MyDatabaseHelper.TABLE_FRIEND, new String[]{F_NAME}, null, null, null, null,CHAT_COUNT);
}
catch(Exception e){System.out.println("Error while retrieving frnds list"+e.getLocalizedMessage());
}
if(c.moveToFirst())
{
do
{
String friend=c.getString(c.getColumnIndex(F_NAME));
friends.add(friend);
}
while(c.moveToNext());
}
return friends;
}
public List<String> getMessages(String from)
{
List<String> messages= new ArrayList<String>();
String where=SENDER+"="+from;
Cursor c=database.query(MyDatabaseHelper.TABLE_MESSAGE, new String[]{M_TEXT},where , null, null, null,null);
if(c.moveToFirst())
{
do
{
String message=c.getString(c.getColumnIndex(M_TEXT));
messages.add(message);
}
while(c.moveToNext());
}
return messages;
}
}
データベースヘルパー
package com.pdd.vayu;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static String TABLE_FRIEND="friend";
public static String TABLE_MESSAGE="message";
public static String CREATE_FRIEND="create table if not exists "+
TABLE_FRIEND+" (userid varchar primary key,fname varchar,chat_count int);";
public static String CREATE_MESSAGE="create table if not exists "+
TABLE_MESSAGE+"(mid integer primary key autoincrement," +
"mtext varchar," +"sender varchar , receiver varchar , type varchar)";
//public static String TABLE_FRIENDS="friends";
public static String DB_NAME="MessengerDB.db";
Context context;
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_FRIEND);
db.execSQL(CREATE_MESSAGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
plsヘルプ