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 8064303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:19:44+00:00 2026-06-05T11:19:44+00:00

I’m trying to make a ListView with clickable list entries. What I want it

  • 0

I’m trying to make a ListView with clickable list entries. What I want it to do, when an entry is clicked, is display a toast message. However, as soon as I click an entry, the application crashes. I don’t really see why it does this. This is my code:

package com.andriesse.android.dailyproductivity;

import java.util.ArrayList;

import com.andriesse.android.dailyproductivity.MyDB;

public class Main extends Activity implements OnItemClickListener, OnKeyListener {

ListView listview;

MyDB mydb;
TaskAdapter myadapter;

private class MyTask {
    public MyTask(String t) {
        task = t;
    }

    public String task;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    listview = (ListView) findViewById(R.id.listview);
    listview.setOnItemClickListener(this);

    mydb = new MyDB(this);
    mydb.open();

    myadapter = new TaskAdapter(this);

    listview.setAdapter(myadapter); 

}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String item = ((TextView)view).getText().toString();
    Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}

private class TaskAdapter extends BaseAdapter {
    private LayoutInflater layoutinflater;
    private ArrayList<MyTask> arraylist;

    public TaskAdapter(Context context) {
        layoutinflater = LayoutInflater.from(context);
        arraylist = new ArrayList<MyTask>();
        getdata();
    }

    public void getdata() {
        Cursor c = mydb.getTasks();
        startManagingCursor(c);
        if (c.moveToFirst()) {
            do {
                String task = c.getString(c
                        .getColumnIndex(Constants.TASK_NAME));
                MyTask temp = new MyTask(task);
                arraylist.add(temp);
            } while (c.moveToNext());
        }
    }

    public int getCount() {
        return arraylist.size();
    }

    public MyTask getItem(int i) {
        return arraylist.get(i);
    }

    public long getItemId(int i) {
        return i;
    }

    public View getView(int i, View view, ViewGroup group) {            
        ViewHolder holder;
        if (view == null || view.getTag() == null) {
            view = layoutinflater.inflate(R.layout.rowlayout, null);
            holder = new ViewHolder();
            holder.textview = (TextView) view.findViewById(R.id.textview);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.mytask = getItem(i);
        holder.textview.setText(holder.mytask.task);

        return view;
    }

    public class ViewHolder {
        MyTask mytask;
        TextView textview;
    }
}

private void addItem(String item) {
    if (item.length() > 0) {
        try {
            saveTaskToDB(item);
            myadapter.arraylist.add(new MyTask(item));
            myadapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void saveTaskToDB(String task) {
    mydb.insertTask(task);
}

@Override
public void onDestroy() {
    super.onDestroy();
    mydb.close();
}

public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    return false;
}

}

And here’s my LogCat log:

06-09 20:50:41.743: E/AndroidRuntime(194): Uncaught handler: thread main exiting due to uncaught exception
06-09 20:50:41.773: E/AndroidRuntime(194): java.lang.ClassCastException: android.widget.RelativeLayout
06-09 20:50:41.773: E/AndroidRuntime(194):  at com.andriesse.android.dailyproductivity.Main.onItemClick(Main.java:57)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.widget.ListView.performItemClick(ListView.java:3285
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.os.Handler.handleCallback(Handler.java:587)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.os.Looper.loop(Looper.java:123)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 20:50:41.773: E/AndroidRuntime(194):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 20:50:41.773: E/AndroidRuntime(194):  at java.lang.reflect.Method.invoke(Method.java:521)
06-09 20:50:41.773: E/AndroidRuntime(194):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 20:50:41.773: E/AndroidRuntime(194):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 20:50:41.773: E/AndroidRuntime(194):  at dalvik.system.NativeStart.main(Native Method)
  • 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-05T11:19:45+00:00Added an answer on June 5, 2026 at 11:19 am

    post the stack trace. We are more likely to be able to help if you add it.

    I can tell you though the problem is probably from how you are trying to cast the view to TextView. The view that is coming in as the parameter is probably not the TextView, it is likely some kind of parent Layout. You probably need to use

    TextView mTxt = (TextView)view.findViewById(R.id.yourEdtTxtID); 
    

    to get a reference to the TextView

    Also though you probably shouldn’t be changing the text on a Row’s TextView manually in the click listener like that. If you need to change the value you should be using an Adapter with a data structure that will allow you to change the value to whatever you like, and once the Adapter is set on your ListView it will update the Views on the screen automatically for you when you change the data in your structure.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.