我正在尝试构建listview,listview中的每个项目都会打开其特定的新活动。但我得到一些字符串转换错误。请帮忙。 我收到了这个错误:
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>
答案 0 :(得分: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本身。你应该阅读文档。
答案 1 :(得分: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;
}
}
});
}
}