Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:21:28+00:00 2026-06-07T01:21:28+00:00

Ahm Yester I post my code about my problem on viewing the data in

  • 0

Ahm Yester I post my code about my problem on viewing the data in the listview using listfragment. I got the solution and it works fine. But now the problem is the getItem function is returning an error saying “java.lang.classcastexception : java.lang.string”. Here is my code:

My listfragment:

public class ListTest2 extends ListFragment {



      @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub

          ListAdapter listAdapter = getListAdapter();
          Log.i("here","here");
          ListSql cc = (ListSql) listAdapter.getItem(position);



          //Toast.makeText(getActivity(), "" + l.getSelectedItem() ,Toast.LENGTH_LONG).show();
          ArrayList<String> arr = new ArrayList<String>();
         Log.i("",cc.getName());


    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        List<ListSql> results = new ArrayList<ListSql>();
        ArrayList<String> arr = new ArrayList<String>();
        HotorNot hon = new HotorNot(getActivity());
        hon.open();
        arr = hon.listme(arr);
        hon.close();
        Toast.makeText(getActivity(), arr.toString(), Toast.LENGTH_LONG).show();
        setListAdapter(new SqlParser(getActivity(),arr));
      } 
}

My ListSql Class:

public class ListSql {


    private String Fname;
    private String Fpass;
    private ArrayList<String> arList;

    private Context myContext;
    private List<ListSql> items;
    private LayoutInflater  mInflater;
    private DBhelper myHelper;
    private SQLiteDatabase myDbase;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "_persons_name";
    public static final String KEY_HOTNESS = "person_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;


    public static class DBhelper extends SQLiteOpenHelper{

        public DBhelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(
                    "Create Table " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + " Integer PRIMARY KEY AUTOINCREMENT, " + 
                    KEY_NAME + " TEXT NOT NULL, " + 
                    KEY_HOTNESS + " TEXT NOT NULL);"
                    );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);
        }

    }


    public String getName(){
        return Fname;
    }

    public String getPass(){

        return Fpass;
    }




    public ListSql open(){
        myHelper = new DBhelper(myContext);
        myDbase = myHelper.getWritableDatabase();
        Log.i("open","open");
        return this;
    }
    public void close(){
        myHelper.close();

    }


    public ArrayList<String> Listme(ArrayList<String> arr){
        String sql = "select * from " + DATABASE_TABLE;
        Cursor c = myDbase.rawQuery(sql, null);
        if(c !=null){
            if  (c.moveToFirst()) {
                do {
                    String firstName = c.getString(c.getColumnIndex(KEY_NAME));
                    String pass = c.getString(c.getColumnIndex(KEY_HOTNESS));
                    arr.add("username " + firstName + ",  Password: " + pass);

                }while (c.moveToNext());
            } 

            return arr;

        }

        return null;

    }

}

AND my Baseadapter:

public class SqlParser extends BaseAdapter {

    private Context myContext;
    private ArrayList<String> items;
    private LayoutInflater  mInflater;
    private DBhelper myHelper;
    private SQLiteDatabase myDbase;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "_persons_name";
    public static final String KEY_HOTNESS = "person_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;


    public SqlParser(Context context ,ArrayList<String> arr){
        this.myContext = context;
        this.items = arr;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    private class ViewHolder {
        public TextView textView;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }



    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ArrayList<String> arr = new ArrayList<String>();
        View view = convertView;
        ViewHolder viewHolder;
        TextView namef ;
        ImageView prof;
        Bitmap bMap;
        if (view == null) {
            view = mInflater.inflate(R.layout.listinflate, parent, false);

            /**
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) view.findViewById(R.id.textView1);
            view.setTag(viewHolder); **/


            /*** You can do this manualy without using holder ***/
             namef = (TextView)view.findViewById(R.id.textView1);

            /*** You can do this manualy on setting the tag to individual components rather than using holder  ***/ 
            view.setTag(namef);

        }else {
            viewHolder = (ViewHolder) view.getTag();
            namef = (TextView) view.getTag();

        }
        namef.setText(items.get(position));

        return view;
    }
}

I dont What Causing this error. thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:21:30+00:00Added an answer on June 7, 2026 at 1:21 am

    The exception is thrown by you trying to cast a String to a ListSql in:

    ListSql cc = (ListSql) listAdapter.getItem(position);
    

    Instead , it should be like this:

    String cc = (String) listAdapter.getItem(position);
    

    as your custom adapter returns a String from the getItem method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing an app using XCode that will allow the streaming of
Suppose I want to create trees of a certain set depth, that is, the
I would like to build something like this: at start an empty page with
I am looking for a German pronunciation dictionary in order to use for PocketSphinx
Possible Duplicate: Character reading from file in Python I want to strip a input
I have a page with links to reports. Whenever somebody clicks on one report,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.