2016-01-21 88 views
0

由於我是新來的android的發展我想從陣列中的樣本數據綁定到列表視圖,但我的「M得到錯誤的Android的列表視圖不工作

setListAdapter(new ArrayAdapter<String>this,android.R.layout.simple_list_item_1, city)); 

,也有一些進口聲明未能在Android Studio中導入

mycode的

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.app.ListActivity; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    String[] city = { 
      "Bangalore", 
      "Chennai", 
      "Mumbai" 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     //Listview adapter 
     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, city)); 
    } 
} 
+3

發佈您的錯誤 –

+0

setListAdapter在ListActivity中,而不是在AppCompatActivity中。它可能不是編譯? – yahya

+0

import android.widget.ArrayAdapter;刪除此行並設置擴展BaseAdapter的數組適配器 – KCN

回答

0
public class MainActivity extends AppCompatActivity { 
    ListView listView ; 
    String[] city = { "Bangalore","Chennai","Mumbai" }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar);   
    listView = (ListView) findViewById(R.id.yourlistviewid); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, city); 
    listView.setAdapter(adapter); 
    } 

} 
+0

感謝您的回答。我還處於早期階段,所以我無法找出想要在那裏發生的事情..反正謝天謝地它工作得很好現在我明白了 –