2016-05-31 35 views
0

我是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
+1

你的MainActivity佈局文件?如果是的話,你必須在其中創建一個'Listview'。獲取對此列表視圖的引用並將適配器設置爲listview.setAdapter(適配器) – jayeshsolanki93

+1

不應再使用ListView。而是使用新的'RecyclerView'。有一個很好的開發者指南。 [見這裏](https://developer.android.com/training/material/lists-cards.html) –

+0

@ jayeshsolanki93是的,我有一個MainActivity和一個ListView。 – Karlom

回答

1

您必須在您的activity_main.xml中創建ListView:

<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); 

嘗試教程:http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

或本:http://www.tutorialspoint.com/android/android_list_view.htm

+0

好吧,當我將兩行添加到'updateDisplay'方法中時,我得到'無法解析'listView''。對此有何想法? – Karlom

+0

@Karlom將'R.id.list'更改爲'R.id.listView' – jayeshsolanki93

+0

更改id名稱後出現同樣的錯誤。 – Karlom

1

ListView裏面content_main佈局並沒有直接的activity_main的一部分。

給一個ID,你content_main佈局:

<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); 
+0

那麼,首先我需要將'+ id ...'改正爲'@ + id ...',然後仍然會'無法解析'listView'。 – Karlom

+0

@Karlom後activity_main.xml – jayeshsolanki93

+0

剛剛添加'activity_main.xml'到問題。 – Karlom

相關問題