0

I have no idea why I'm getting it on this line of code (where the arrow is)

protected void onListItemClick(ListView l, View v, int position, long id) {
--->    App selection = (App) l.getItemAtPosition(position);

Custom ArrayAdapter:

public class MyCustomAdapter extends ArrayAdapter<String>{

private ArrayList<String> myarr = new ArrayList<String>();
private LayoutInflater inflater;

//objects = array of whatever you want to add to the list
public MyCustomAdapter(Context context,
        int textViewResourceId, List<String> objects) {
    super(context, textViewResourceId, objects);
    myarr = (ArrayList<String>) objects;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
 public View getView(int position, View convertView, ViewGroup parent) {

      View row = convertView;
      if(row==null){
       row=inflater.inflate(R.layout.list_row, parent,false);
      }

      ((TextView)row.findViewById(R.id.rowtextview)).setText(myarr.get(position));

      return row;
     }

This is the arrayadapter that does the work. I'm assuming the problem is in here.

1
  • its a custom class that i had created. i dont understand why i cant cast it to the object that l.getItemAtPosition returns Commented Nov 11, 2011 at 8:40

2 Answers 2

3

You must have stored String in your ListView. It gives the exception because it cannot cast String to App

EDIT
The objects in your myarr are displayed in your ListView. Those objects are all Strings. In the line where you get exception you are trying to convert the String at position to an App. It cannot do that so you get the exception. So try this:

String selection = (String) l.getItemAtPosition(position);

EDIT2
Change myarr to

private ArrayList<App> myarr = new ArrayList<App>();

Change the following line:

((TextView)row.findViewById(R.id.rowtextview)).setText(myarr.get(position).getText());

Add App class:

public String getText(){/*...*/}
public void setText(String text){/*...*/}
public String toString(){ /*...*/}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain what you mean? Because I have a custom arrayadapter set up, and a list_row.xml which contains how a row in the list should look like. I understand what you mean but I'm not sure what to look for exactly
ok its up. give it a glance. and which xml file would you like to see?
thanks, yeah i figured it out now. unfortunately i need it to be an App type. is there anything i can do in my ListView to help support App class?
0

if it is custom use like this

MyFont f = fontArray[pos];
selected_font = f.getValue();

Comments

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.