Hello ListView教程问题

时间:2010-08-28 00:52:34

标签: android

我在教程中的代码遇到了一些问题:ListView Tutorial。代码snipets给我带来的问题是:

static final String[] COUNTRIES = new String[] {

Toast.makeText(getApplicationContext(), ((View) view).getText(),

错误分别阅读以下内容:

  • 参数COUNTRIES的非法修饰符;只允许决赛
  • 方法getText()未定义类型View

我不确定如何解决这些问题。我是Java和Android编程的新手,所以请详细解释。我会将整个项目放在这里,但我不太确定如何打包它并把它放在这里。 (就像我说的,我是新手)。提前谢谢。

package com.android.hellolistview;


import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloListView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);



      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, TextView view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_SHORT).show();
        }
      });
    }

private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
    // TODO Auto-generated method stub

}
  static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
        "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
        "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
        "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
        "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
        "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
        "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
        "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
        "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
        "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
        "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
        "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
        "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
        "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
        "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
        "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
        "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
        "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
        "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
        "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
        "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
        "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
        "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
        "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
        "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
        "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
        "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
        "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
        "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
        "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
        "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
        "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
        "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
        "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
        "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
        "Ukraine", "United Arab Emirates", "United Kingdom",
        "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
        "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
      };}

5 个答案:

答案 0 :(得分:1)

我的模拟器也遇到了问题。我得到了“应用程序意外停止...”的消息。

我和你有同样的路线:

public class HelloListView extends ListActivity { 

在我的HelloListView Manifest中,我有以下几行:

<activity android:name=".HelloListViewActivity"

当我把它改成:

<activity android:name=".HelloListView"

应用程序开始在模拟器中工作。将类重命名为HelloListViewActivity也可能会执行相同的操作。还没试过......

答案 1 :(得分:0)

Toast.makeText(getApplicationContext(), ((View) view).getText()

应该是

Toast.makeText(getApplicationContext(), ((TextView) view).getText()

View类没有实现getText方法,而TextView扩展了View并实现了getText方法。在您的代码中,您将视图转换为视图,这实际上没有做任何事情。它需要转换为TextView才能正常工作。

我在想你也可能将你的COUNTRIES字符串数组放在错误的位置。

如果您仍然无法解决问题,请将所有HelloListView代码粘贴到您的问题中。

此外,这可能是一个很好的阅读:http://developer.android.com/guide/topics/ui/index.html

答案 2 :(得分:0)

view没有方法getText(),TextView有方法getText(),TextView扩展View, 所以你的代码必须像:

Toast.makeText(getApplicationContext(), ((TextView) view).getText());

你必须阅读android SDK的api。

答案 3 :(得分:0)

数组的代码应该在onCreate方法之后。

答案 4 :(得分:0)

更改此:公共类HelloListView扩展Activity {  to:public class HelloListView扩展ListActivity { 并添加: 查看源 打印?

1   import android.widget.ArrayAdapter;
2   import android.app.ListActivity;