user1977768
user1977768

Reputation: 41

Android : String conversion error in list view

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>

Upvotes: 0

Views: 314

Answers (2)

Anukool
Anukool

Reputation: 5391

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;

            }

        }

    });

}
}

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

            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.

Upvotes: 1

Related Questions