2013-08-21 46 views
-1
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; 
} 

以下錯誤出現:方法的onCreate()未找到

公共類MainActivity擴展活動{

既然你:

Gradle: cannot find symbol method 
setListAdapter(android.widget.ArrayAdapter<java.lang.String>) 
Gradle: cannot find symbol method getListView() 

回答

1

您的代碼開頭正在使用getListView()您必須擴展ListActivity。嘗試

公共類MainActivity擴展ListActivity {

+0

錯誤不幸的是顯示allplication停止 – sagarlakhani62

+0

@ sagarlakhani62:然後你有更多的問題。 activity_main.xml中的ListView是否有android:id =「@ id/android:list」? –

+0

我嘗試這個代碼的第一種方法本頁http://www.mkyong.com/android/android-listview-example/ – sagarlakhani62

0

你應該延長ListActivity而不是Activity。你的問題應該早已消失。根據您的需要

1

becz您使用getlistView延長listactivity 不活動 ()

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

    } 

} 
+0

我也嘗試這段代碼,但不工作 – sagarlakhani62

0

你應該延長 ListActivity而不是活動,並在XML列表視圖ID必須是

android:id="@android:id/list" 
0
  1. 擴展ListActivity,而不是隻Activity

    public class MainActivity extends ListActivity { } 
    
  2. 在你R.layout.activity_main使用<ListView android:id="@android:id/list"/>

  3. 創建列表的適配器。(以下只是爲了說明一個例子。)

    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 
    
  4. setListAdapter(arrayAdapter);