我是Android新手,2014年的Lynda教程使用適配器製作ListView。下面是在MainActivity:如何在MainActivity擴展AppCompatActivity時使用setListAdapter?
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
無法解析method`錯誤。
我知道從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;
}
}
這裏是我在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>
創建的ListView
你的MainActivity佈局文件?如果是的話,你必須在其中創建一個'Listview'。獲取對此列表視圖的引用並將適配器設置爲listview.setAdapter(適配器) – jayeshsolanki93
不應再使用ListView。而是使用新的'RecyclerView'。有一個很好的開發者指南。 [見這裏](https://developer.android.com/training/material/lists-cards.html) –
@ jayeshsolanki93是的,我有一個MainActivity和一個ListView。 – Karlom