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()
答案 0 :(得分:2)
您的代码以:
开头公共类MainActivity扩展了Activity {
由于您使用的是getListView(),因此您必须扩展ListActivity。尝试
公共类MainActivity扩展了ListActivity {
答案 1 :(得分:1)
扩展listactivity 不是活动 因为你使用的是getlistView()
答案 2 :(得分:0)
您应延伸 ListActivity
而不是Activity
。你的问题应该早就过去了。
答案 3 :(得分: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();
}
});
答案 4 :(得分: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();
}
});
}
}
答案 5 :(得分:0)
你应该扩展 ListActivity而不是Activity,而你的xml listview id必须
android:id="@android:id/list"
答案 6 :(得分:0)
扩展ListActivity
而非Activity
。
public class MainActivity extends ListActivity { }
在R.layout.activity_main
使用<ListView android:id="@android:id/list"/>
为列表创建适配器。(以下只是一个示例来解释。)
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
setListAdapter(arrayAdapter );