今私はハッシュマップを使用してSQLiteから値を追加してinListviewを表示しますが、私が
map.put("item_title", friends.getString("nickname"));
map.put("item_fname", friends.getString("fname"));
map.put("item_lname", friends.getString("lname"));
エラーで「メソッド getString が friendEntry で定義されていません」と表示される
これはfriendEntryの私のコードです
package com.example.sqlite.entry;
public class FriendEntry {
private int id;
private String fname;
private String lname;
private String nickname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
フレンド リスト アクティビティ
package com.example.sqlite;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sqlite.db.FriendsDB;
import com.example.sqlite.entry.FriendEntry;
public class FriendsListActivity extends Activity {
private Context context;
private FriendsDB db;
private ArrayList<FriendEntry> friends;
private ArrayList<String> data;
private TextView hellotext;
private ListView hellolistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.friendlist_layout);
}
public void showAllList(){
//view matching
hellotext = (TextView) findViewById(R.id.hellotext);
hellolistview = (ListView) findViewById(R.id.hellolistview);
//select data
friends = db.selectAll();
if(friends.size()==0){
Toast.makeText(context,"You dont have any friend.",Toast.LENGTH_SHORT).show();
}else{
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 1;i<=friends.size();i++){
// set value for data
map = new HashMap<String, String>();
map.put("item_title", friends.getNickname());
map.put("item_fname", friends.getFname());
map.put("item_lname", friends.getLname());
MyArrList.add(map);
}
//adapter
hellolistview.setAdapter(new adapter());
}
}
private class adapter extends BaseAdapter{
private Holder holder;
@Override
public int getCount() {
// TODO Auto-generated method stub
return friends.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
//create
if( view == null){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_layout,null);
holder = new Holder();
holder.title = (TextView) view.findViewById(R.id.item_title);
view.setTag(holder);
}else{
holder = (Holder) view.getTag();
}
//assign data / wait for data
holder.title.setText(data.get(position));
return view;
}
private class Holder{
public TextView title;
}
}
}
これは私のFriendsDBです
package com.example.sqlite.db;
import java.util.ArrayList;
import com.example.sqlite.entry.FriendEntry;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
//ใช้ในการ inert update delete
public class FriendsDB {
private FriendsDBHelper helper;
private SQLiteDatabase db;
public FriendsDB(Context context){
helper = new FriendsDBHelper(context);
db = helper.getWritableDatabase();
}
//insert
public long insert(String fname,String lname,String nickname){
ContentValues values = new ContentValues();
values.put(FriendsDBHelper.TABLE_KEY_FNAME, fname);
values.put(FriendsDBHelper.TABLE_KEY_LNAME, lname);
values.put(FriendsDBHelper.TABLE_KEY_NICKNAME, nickname);
return db.insert(FriendsDBHelper.TABLE_NAME, null ,values);
}
//select all
public ArrayList<FriendEntry> selectAll(){
ArrayList<FriendEntry> friends = new ArrayList<FriendEntry>();
// cursor คือ ชุดข้อมูล
Cursor cursor = db.rawQuery("SELECT*FROM"+FriendsDBHelper.TABLE_NAME+"WHERE id != ?",new String[]{Integer.toString(0)});
cursor.moveToFirst();
if(cursor.getCount()!=0){
do{
FriendEntry friend = new FriendEntry();
friend.setId(cursor.getInt(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_ID)));
friend.setFname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_FNAME)));
friend.setLname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_LNAME)));
friend.setNickname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_NICKNAME)));
friends.add(friend);
}while(cursor.moveToNext());
}
return friends;
}