Android ListView和适配器

Android ListView和适配器,android,android-listview,Android,Android Listview,我已经为ListView创建了自己的适配器。ListView的每一行由两个TextVew表示。我需要在我的ListView中显示一些信息 这是我的活动代码: public class MyCountriesActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我已经为ListView创建了自己的适配器。ListView的每一行由两个TextVew表示。我需要在我的ListView中显示一些信息

这是我的活动代码:

public class MyCountriesActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);    
        int row_ID = R.layout.list_item;
        ListAdapter adapt = new MyAdapter(this,row_ID); 
        setListAdapter(adapt);
        }
}
以下是我的适配器代码:

public class MyAdapter extends ArrayAdapter<String> {

    Activity a;
    int row_ID;
    String[] countries = { "USA", "France" };
    String[] years = { "1992", "2010" };

    public MyAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        a = (Activity) context;
        row_ID = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        if (convertView == null) { // Create new row view
            LayoutInflater inflater = a.getLayoutInflater();
            row = inflater.inflate(row_ID, parent, false);
        } else
            // reuse old row view to save time/battery
            row = convertView;

        /* Add new data to row object */
        TextView country = (TextView) row.findViewById(R.id.country);
        TextView year = (TextView) row.findViewById(R.id.year);
        country.setText(countries[position]);
        year.setText(years[position]);
        return row;
    }
}
main.xml:

03-04 17:41:11.044: E/AndroidRuntime(1277): FATAL EXCEPTION: main
03-04 17:41:11.044: E/AndroidRuntime(1277): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.ledinov.namespace/android.ledinov.namespace.MyCountriesActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.os.Looper.loop(Looper.java:137)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at java.lang.reflect.Method.invoke(Method.java:511)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at dalvik.system.NativeStart.main(Native Method)
03-04 17:41:11.044: E/AndroidRuntime(1277): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.Activity.setContentView(Activity.java:1835)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.ledinov.namespace.MyCountriesActivity.onCreate(MyCountriesActivity.java:21)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.Activity.performCreate(Activity.java:4465)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-04 17:41:11.044: E/AndroidRuntime(1277):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-04 17:41:11.044: E/AndroidRuntime(1277):     ... 11 more
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

如果我猜您在
R.layout.main
布局文件或行布局文件
R.layout.list\u项中没有id为
@android:id/list
ListView
元素,可能不是由单个
Textview
元素组成,在这种情况下,
ArrayAdapter
将与
ClassCastException
一起崩溃,因为它需要单个
Textview
元素

此外,您可能希望向适配器添加以下方法:

public void getCount() {
  return countries.length;
}
否则,即使您的应用程序将运行,您也不会在
列表视图中看到任何元素。如果在
R.layout.list\u项中有两个
textview
,则这是错误的。按如下方式修改适配器:

public MyAdapter(Context context, int textViewResourceId, int textId) {
        super(context, textViewResourceId, textId);
        a = (Activity) context;
        row_ID = textViewResourceId;
}
在哪里实例化适配器:

int row_ID = R.layout.list_item;
int theId = R.id.the_id_of_one_of_the_textview_fromthelayout_above; (the id of the TextView from the R.layout.list_item)
ListAdapter adapt = new MyAdapter(this,row_ID, theId); 

如果我猜您在
R.layout.main
布局文件或行布局文件
R.layout.list\u项中没有id为
@android:id/list
ListView
元素,可能不是由单个
Textview
元素组成,在这种情况下,
ArrayAdapter
将与
ClassCastException
一起崩溃,因为它需要单个
Textview
元素

此外,您可能希望向适配器添加以下方法:

public void getCount() {
  return countries.length;
}
否则,即使您的应用程序将运行,您也不会在
列表视图中看到任何元素。如果在
R.layout.list\u项中有两个
textview
,则这是错误的。按如下方式修改适配器:

public MyAdapter(Context context, int textViewResourceId, int textId) {
        super(context, textViewResourceId, textId);
        a = (Activity) context;
        row_ID = textViewResourceId;
}
在哪里实例化适配器:

int row_ID = R.layout.list_item;
int theId = R.id.the_id_of_one_of_the_textview_fromthelayout_above; (the id of the TextView from the R.layout.list_item)
ListAdapter adapt = new MyAdapter(this,row_ID, theId); 
这样试试看

public class MyAdapter extends ArrayAdapter<String> {

    Activity a;
    int row_ID;
    String[] countries = { "USA", "France" };
    String[] years = { "1992", "2010" };

    public MyAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context = context;
        row_ID = textViewResourceId;
    }



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

View row = convertView;

Holder ho = null;

if (row == null )
{

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(row_ID, parent, false);

ho = new Holder();

txtCountry = (TextView) row.findViewById(R.id.country);
txtYear = (TextView) row.findViewById(R.id.year);

row.setTag(ho);


}

else{


  ho = (Holder)row.getTag();


}


txtCountry.setText(countries[position]);
txtYear.setText(years[position]);

return row;
}

static class Holder
    {
        TextView txtCountry;
        TextView txtYear;
    }

}
公共类MyAdapter扩展了ArrayAdapter{
活动a;
int row_ID;
字符串[]国家={“美国”、“法国”};
字符串[]年={“1992”,“2010”};
公共MyAdapter(上下文上下文,int textViewResourceId){
super(上下文,textViewResourceId);
this.context=上下文;
row_ID=textViewResourceId;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
ho=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
row=充气机。充气(row_ID,父项,false);
ho=新支架();
txtCountry=(TextView)row.findViewById(R.id.country);
txtYear=(TextView)row.findViewById(R.id.year);
row.setTag(ho);
}
否则{
ho=(Holder)row.getTag();
}
txtCountry.setText(国家[职位]);
txtYear.setText(年份[职位]);
返回行;
}
静态类持有者
{
TextView txtCountry;
TextView txtYear;
}
}
这样试试看

public class MyAdapter extends ArrayAdapter<String> {

    Activity a;
    int row_ID;
    String[] countries = { "USA", "France" };
    String[] years = { "1992", "2010" };

    public MyAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context = context;
        row_ID = textViewResourceId;
    }



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

View row = convertView;

Holder ho = null;

if (row == null )
{

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(row_ID, parent, false);

ho = new Holder();

txtCountry = (TextView) row.findViewById(R.id.country);
txtYear = (TextView) row.findViewById(R.id.year);

row.setTag(ho);


}

else{


  ho = (Holder)row.getTag();


}


txtCountry.setText(countries[position]);
txtYear.setText(years[position]);

return row;
}

static class Holder
    {
        TextView txtCountry;
        TextView txtYear;
    }

}
公共类MyAdapter扩展了ArrayAdapter{
活动a;
int row_ID;
字符串[]国家={“美国”、“法国”};
字符串[]年={“1992”,“2010”};
公共MyAdapter(上下文上下文,int textViewResourceId){
super(上下文,textViewResourceId);
this.context=上下文;
row_ID=textViewResourceId;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
ho=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
row=充气机。充气(row_ID,父项,false);
ho=新支架();
txtCountry=(TextView)row.findViewById(R.id.country);
txtYear=(TextView)row.findViewById(R.id.year);
row.setTag(ho);
}
否则{
ho=(Holder)row.getTag();
}
txtCountry.setText(国家[职位]);
txtYear.setText(年份[职位]);
返回行;
}
静态类持有者
{
TextView txtCountry;
TextView txtYear;
}
}


我在main.xml中有元素列表视图。R.layout.list_项由2个文本视图组成。它错了吗?@Dmitry您的
ListView
是否具有id
@android:id/list
?查看我编辑的答案。是的,ListView的id是@android:id/list。@Dmitry检查我的答案,我已经添加了更多信息。我已经像你说的那样更改了代码,但仍然不起作用。我如何在这里复制stacktrace?它太长,1条注释的长度不够。我的main.xml中有元素ListView。R.layout.list_项由2个文本视图组成。它错了吗?@Dmitry您的
ListView
是否具有id
@android:id/list
?查看我编辑的答案。是的,ListView的id是@android:id/list。@Dmitry检查我的答案,我已经添加了更多信息。我已经像你说的那样更改了代码,但仍然不起作用。我如何在这里复制stacktrace?它太长,1条评论的长度不够。你不能在这里添加stacktrace?对不起,我是一个新手。我已将我的LogCat信息复制到lof文件中。但是它有太多的字符1comment@Dmitry:查看下面的问题
android
android listview
标签。这里有一个链接,指向
edit
你的帖子,将你的日志粘贴到编辑你的问题中。谢谢。我添加了log和main.xml内容。日志中说我没有id为“list”的listview,但在.xml文件中listview的id为“list”…您不能在这里添加stacktrace?对不起,我是一个初学者。我已将我的LogCat信息复制到lof文件中。但是它有太多的字符1comment@Dmitry:查看下面的问题
android
android listview
标签。这里有一个链接,指向
edit
你的帖子,将你的日志粘贴到编辑你的问题中。谢谢。我添加了log和main.xml内容。日志中说我没有id为“list”的listview,但在.xml文件中listview的id为“list”。。。