我是android的新手,我遵循2014年的Lynda教程,使用适配器制作ListView。下面是MainActivity:
public class MainActivity extends AppCompatActivity {
TextView output;
ProgressBar pb;
List<MyTask> tasks;
List<Flower> flowerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Initialize the TextView for vertical scrolling
output = (TextView) findViewById(R.id.textView);
output.setMovementMethod(new ScrollingMovementMethod());
pb = (ProgressBar) findViewById(R.id.progressBar);
pb.setVisibility(View.INVISIBLE);
tasks = new ArrayList<>();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_do_task) {
if (isOnline()) {
requestData("http://services.hanselandpetal.com/secure/flowers.json");
} else {
Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show();
}
return false;
}
return false;
}
private void requestData(String uri) {
MyTask task = new MyTask();
task.execute(uri);
}
protected void updateDisplay() {
FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList);
setListAdapter(adapter); //<---Here is the error
}在setListAdapter(adapter) I get无法解决方法的错误。
我从another answer了解到,如果MainActivity扩展了ListActivity而不是AppCompatActivity,那么这个错误就解决了。但是,我得到了许多其他我无法解决的错误。所以感谢你的解决方案。
如果您需要,下面是FlowerAdapter类
public class FlowerAdapter extends ArrayAdapter<Flower> {
private Context context;
private List<Flower> flowerList;
public FlowerAdapter(Context context, int resource, List<Flower> objects) {
super(context, resource, objects);
this.context = context;
this.flowerList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_flower, parent, false);
//Display flower name in the TextView widget
Flower flower = flowerList.get(position);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText(flower.getName());
return view;
}
}下面是我在ListView中创建的content_main.xml
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:text="" />activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.flowershop.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/id_content_main"
layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>发布于 2016-05-31 15:20:41
您必须在您的ListView中创建activity_main.xml:
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> 并使用它:
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);试用本教程:discription&aid=65&aaid=90
或者这个:view.htm
发布于 2016-05-31 15:48:02
您的ListView位于content_main布局中,而不是直接作为activity_main的一部分。
为content_main布局提供一个id,如下所示:
<include
android:id="+id/id_content_main"
layout="@layout/content_main" />然后像这样访问它
View contentView = findViewById(R.id.id_content_main);
listView = (ListView) contentView.findViewById(R.id.listView);
listView.setAdapter(adapter);https://stackoverflow.com/questions/37549544
复制相似问题