0
public class MainActivity extends Activity {

static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
        "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
        "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,
        R.layout.activity_main, FRUITS));
    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

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

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

出现以下错误:

Gradle: cannot find symbol method
setListAdapter(android.widget.ArrayAdapter<java.lang.String>)
Gradle: cannot find symbol method getListView()
4

7 回答 7

2

您的代码以:

公共类 MainActivity 扩展 Activity {

由于您使用的是 getListView() ,因此您必须改为扩展 ListActivity 。尝试

公共类 MainActivity 扩展 ListActivity {

于 2013-08-21T07:53:58.640 回答
1

扩展列表活动而不是活动,因为您正在使用 getlistView()

于 2013-08-21T07:57:35.603 回答
0

您应该扩展ListActivity 而不是 Activity 并且在您的 xml listview id 中必须是

    android:id="@android:id/list"
于 2013-08-21T08:02:16.237 回答
0

您应该扩展 ListActivity而不是Activity. 你的问题应该早就过去了。

于 2013-08-21T07:55:11.880 回答
0
  1. 扩展ListActivity而不是仅Activity.

    public class MainActivity extends ListActivity { }
    
  2. 在您的R.layout.activity_main使用中<ListView android:id="@android:id/list"/>

  3. 为列表创建一个适配器。(以下仅举例说明。)

     ArrayAdapter arrayAdapter = new ArrayAdapter(this,  // Context.
                ndroid.R.layout.simple_list_item_1, // Specify the row template to use
                your_array);    // your array having data
    
  4. setListAdapter(arrayAdapter );

于 2013-08-21T08:17:25.533 回答
0

尝试这个..

 ListView listView = (ListView) findViewById(R.id.list);
        listView.setTextFilterEnabled(true);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,FRUITS));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
于 2013-08-21T08:00:53.213 回答
0

示例 listactivity 代码,根据您的需要进行更改:

文件:res/layout/list_fruit.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp" >
</TextView>

文件:ListFruitActivity.java

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

public class ListFruitActivity extends ListActivity {

    static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
            "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
            "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // no more this
        // setContentView(R.layout.list_fruit);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS));

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

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

    }

}
于 2013-08-21T08:00:54.807 回答