现在我使用hashmap从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中是unfind”
这是我在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;
}
}
FriendsListActivity
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;
}
答案 0 :(得分:1)
您可以使用:
map.put("item_title", friends.getNickname());
因为您使用Getter setter方法创建了...而且您也可以为其他人创建类似的方法..
答案 1 :(得分:0)
您的Friends类不是名为getString(string)
使用此
map.put("item_title", friends.get(i).getNickname());
map.put("item_fname", friends.get(i).getFname());
map.put("item_lname", friends.get(i).getLname());
要设置昵称,您需要致电friends.setNickname("name")
,因为您有以下
public void setNickname(String nickname) {
this.nickname = nickname;
}
编辑:我也看到你有这个showAllList()
,但是你没有在活动课的任何地方调用它。在showAllList()
之后致电setContentView(R.layout.friendlist_layout)
。
在执行db = new FriendsDB(ActivityName.this)
之前,您可能需要执行此操作friends = db.selectAll()
。
答案 2 :(得分:0)
像这样使用
for (int i = 1;i<=friends.size();i++){
// set value for data
map = new HashMap<String, String>();
map.put("item_title", friends.get(i).getNickname());
map.put("item_fname", friends.get(i).getFname());
map.put("item_lname", friends.get(i).getLname());
MyArrList.add(map);
}