0

我正在尝试构建列表视图,并且列表视图中的每个项目都会打开其特定的新活动。但我收到一些字符串转换错误。请帮忙。我收到此错误:

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

这是我的java文件:

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;
            }
         }
    });
}

}

这是我的 xml 布局:

<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>

4

2 回答 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;

我认为您不能使用 Array 和 String 类,因为它们是 Android 活动。

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

在这里,您还有另一个大错误。根据文档,数组适配器的第二个参数必须是包含TextView在实例化视图时使用的布局文件的资源 ID,而不是 ListView 本身。您应该阅读文档。

于 2013-01-30T08:33:56.990 回答
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;

            }

        }

    });

}
}
于 2013-01-30T08:40:06.497 回答