0

I am trying to build listview and each item in listview opens its specific new activity. But i am getting some string conversion error. Please help. I am getting this error:

Type mismatch: cannot convert from java.lang.String to com.example.cprograms.String Array.java

Here is my java file:

package com.example.cprograms;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Ctypes extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ctypes);

    String[] types = new String[] {"Arrays", "Strings" };

    setListAdapter(new ArrayAdapter<String>(this,R.layout.ctypes, types));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long arg3) {
            switch(position)
            {
                case 0:  Intent newActivity = new Intent(v.getContext(), Array.class);     
                         startActivity(newActivity);
                         break;
                case 1:  Intent newActivity1 = new Intent(v.getContext(), String.class);     
                         startActivity(newActivity1);
                         break;
            }
         }
    });
}

}

here is my xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Ctypes" >

<ListView
    android:id="@+id/list_ctypes"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >"
</ListView>

1
  • 3
    You should REALLY write down the logical steps of what you need to do because clearly you clearly don't understand the difference between a String and a String Array. This also includes an understand of how an array adapter works with a Listview, without this understanding you can't solve your problem. Commented Jan 30, 2013 at 8:28

2 Answers 2

1
            case 0:  Intent newActivity = new Intent(v.getContext(), Array.class);     
                     startActivity(newActivity);
                     break;
            case 1:  Intent newActivity1 = new Intent(v.getContext(), String.class);     
                     startActivity(newActivity1);
                     break;

I think you can not use Array and String class as they were Android activities.

setListAdapter(new ArrayAdapter<String>(this,R.layout.ctypes, types));

Here you have another big error. According to the documentation, the second parameter of the array adapter has to be the resource ID for a layout file containing a TextView to use when instantiating views, not the ListView itself. You should read the documentation.

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

Comments

0
import java.lang.String;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.ctypes);

    String[] types = new String[] { "Arrays", "Strings" };

    setListAdapter(new ArrayAdapter<String>(this, R.layout.ctypes, types));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long arg3) {
            // TODO Auto-generated method stub

            switch (position) {
            case 0:
                Intent newActivity = new Intent(v.getContext(), com.example.cprograms.Array.class);
                startActivity(newActivity);
                break;
            case 1:
                Intent newActivity1 = new Intent(v.getContext(),
                        com.example.cprograms.String.class);
                startActivity(newActivity1);
                break;

            }

        }

    });

}
}

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.