Note that there are some explanatory texts on larger screens.

plurals
  1. POError method getString undefind in arraylist<entry> (hashmap)
    text
    copied!<p>Now i use hashmap to add value from SQLite to show inListview but it's error when i</p> <pre><code>map.put("item_title", friends.getString("nickname")); map.put("item_fname", friends.getString("fname")); map.put("item_lname", friends.getString("lname")); </code></pre> <p>error tell me "method getString is undefind in friendEntry"</p> <p>This is my code in friendEntry</p> <pre><code>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; } } </code></pre> <p>FriendsListActivity</p> <pre><code>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&lt;FriendEntry&gt; friends; private ArrayList&lt;String&gt; 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&lt;HashMap&lt;String, String&gt;&gt; MyArrList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); HashMap&lt;String, String&gt; map; for (int i = 1;i&lt;=friends.size();i++){ // set value for data map = new HashMap&lt;String, String&gt;(); 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; } } } </code></pre> <p>This is my FriendsDB </p> <pre><code> 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&lt;FriendEntry&gt; selectAll(){ ArrayList&lt;FriendEntry&gt; friends = new ArrayList&lt;FriendEntry&gt;(); // 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload