0

Now i use hashmap to add value from SQLite to show inListview but it's error when i

map.put("item_title", friends.getString("nickname"));
map.put("item_fname", friends.getString("fname"));
map.put("item_lname", friends.getString("lname"));

error tell me "method getString is undefind in friendEntry"

This is my code in 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;
    }
}


}

This is my 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;
    }
1
  • As points out in the answers, your class does not feature a method getString. Did you mean to call toString as inherited from Object. If you did, beware that the method might not output what you expect if you don't override it. Without your own implementation, it will likely only print the instance reference. Commented Aug 21, 2013 at 5:51

3 Answers 3

1

You can use this:

map.put("item_title", friends.getNickname());

Because you have created with Getter setter method... And you can also get similar for others..

Sign up to request clarification or add additional context in comments.

4 Comments

It's still error (the method getNickname()is undefind for the type ArrayList<friendEntry>)
this is the problem because you are adding to it ArrayList of String and then you are getting the Size of your friends list in getCount method.. So there is two options...1) The data in which you are inserted in ArrayList<STring>, pass this list to your adpater 2) Make another arraylist for friendEntry class and pass to that list to your adapter.
How can i do if i want to use ArrayList from friendEntry
i get all friend by db.selectall and put it in friends .Now i want to get value(fname,lname,nickname) in array friends to put it in map
0

Use like this

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

Comments

0

Your Friends class does not a method called getString(string)

Use this

map.put("item_title", friends.get(i).getNickname());
map.put("item_fname", friends.get(i).getFname());
map.put("item_lname", friends.get(i).getLname());

To set a nickname you need to call friends.setNickname("name") coz you have the below

    public void setNickname(String nickname) {
    this.nickname = nickname;
    }

Edit: I also see you have this showAllList() but you do not call it any where in your activity class. Call showAllList() after setContentView(R.layout.friendlist_layout).

You probably need to do this db = new FriendsDB(ActivityName.this) before doing friends = db.selectAll().

3 Comments

Thank you very much ,Yes i missing
@ThanapoomNaNakhon you are missing some code as suggested in the edit
@ThanapoomNaNakhon as you see the comments here stackoverflow.com/questions/18353974/…. i already mentioned about initializing db here in the dit section

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.