自定义ListView适配器强制关闭:(

时间:2010-11-06 05:39:44

标签: android listview android-layout android-linearlayout

我是Android的新手,并且已经按照在线发布的教程来试验Android的编码。我试图显示一个列表,其中包含来自两个不同阵列的输入,但每次我运行代码时我都会继续关闭力量,并且似乎可以弄清楚我做错了什么。这是我的代码(这是来自在线教程)

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical" 
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

  <TextView android:id="@+id/TextView01" 
          android:layout_height="wrap_content" 
          android:text="List of Country &amp; their denotation" 
          android:textStyle="normal|bold" 
          android:gravity="center_vertical|center_horizontal"
          android:layout_width="fill_parent"> 
  </TextView>
  <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
            android:layout_width="fill_parent">
  </ListView>
</LinearLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content" 
              android:gravity="left|center"
              android:layout_width="wrap_content" 
              android:paddingBottom="5px"
              android:paddingTop="5px" 
              android:paddingLeft="5px">

  <TextView android:id="@+id/TextView01" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:gravity="center"
            android:background="@drawable/bg" 
            android:textColor="#FFFF00"
            android:text="hi"/>

  <TextView android:text="@+id/TextView02" 
            android:id="@+id/TextView02"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginLeft="10px" 
            android:textColor="#0099CC"/>

</LinearLayout> 

display.java

package com.example.display

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class customlistview extends Activity {

  private static class EfficientAdapter extends BaseAdapter {
  private LayoutInflater mInflater;

  public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
  }

  public int getCount() {
    return country.length;
  }

  public Object getItem(int position) {
    return position;
  }

  public long getItemId(int position) {
    return position;
  }

  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.listview, null);
      holder = new ViewHolder();
      holder.text = (TextView) convertView.findViewById(R.id.TextView01);
      holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);

      convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      holder.text.setText(curr[position]);
      holder.text2.setText(country[position]);

      return convertView;
    }

    static class ViewHolder {
      TextView text;
      TextView text2;
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView l1 = (ListView) findViewById(R.id.ListView01);
    l1.setAdapter(new EfficientAdapter(this));
  }

  private static final String[] country = { "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia", "Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania", "Luxembourg" };

  private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD", "EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD", "LTL ", "EUR"};

}

此代码也可以在http://www.androidpeople.com/android-custom-listview-tutorial-example/

找到

我已经尝试了几次,改变了不同的值,甚至尝试打印一个阵列,但是一直没有成功,并继续关闭力量。如果有人能够指出代码有什么问题并指出我正确的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您是否在AndroidManifest.xml文件中包含了您的活动文件。 每个活动都需要包含在此xml文件中。 句法:

同时检查您的启动器活动是否已在xml文件中设置