2014-05-14 94 views
0

我是Android編程界的新手。我正在使用this site中的代碼,我想單擊這些圖像以打開新的活動。誰能幫我這個?在ListView上打開活動

這是XML:

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_list" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:divider="@null" 
android:dividerHeight="0px" 
tools:context=".StreamActivity" /> 

和Java代碼的第一部分:

public class StreamActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_stream); 

    StreamAdapter adapter = new StreamAdapter(this); 
    ((ListView) findViewById(R.id.main_list)).setAdapter(adapter); 

    adapter.add(new StreamItem(this, R.drawable.photo1, "Option1", "Click to open")); 
    adapter.add(new StreamItem(this, R.drawable.photo2, "Option2", "Click to open")); 
    adapter.add(new StreamItem(this, R.drawable.photo3, "Option3", "Click to open")); 
    adapter.add(new StreamItem(this, R.drawable.photo4, "Option4", "Click to open")); 
    adapter.add(new StreamItem(this, R.drawable.photo5, "Option5", "Click to open")); 
    adapter.add(new StreamItem(this, R.drawable.photo6, "Option6", "Click to open")); 
    adapter.add(new StreamItem(this, R.drawable.photo7, "Option7", "Click to open")); 
+0

首先創建你的ListView的一個實例。作爲ListView list =(ListView)findViewById(R.id.main_list);然後調用它的onItemClickListener()方法 –

+0

在適配器den中添加項目後,將其設置爲listview。 .not不應該添加項目到適配器,因爲你在這裏做..deniteiteclick()獲取項目位置書庫傳遞意圖到每個位置。如果你需要代碼我可以提供 – Meghna

回答

0

ListView.setOnItemClickListener。您可以讓您的活動實施界面,然後在onCreate()內撥打listView.setOnItemClickListener(this);

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // create an Intent to open the next Activity. 
    // If you need information from the selected item, use 
    StreamItem item = adapter.getItem(position); 
} 
0

的點擊添加您的名單聽衆

yourList.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    

     Intent intent = new Intent(context, YourTargetClass.class); 
     startActivity(intent); 
    } 
}); 
0

退房ListView的指南,如果你還沒有:Android developer ListView guide

這個例子擴展ListActivity並實現onListItemClick。在該方法中,創建一個Intent並調用startActivity。

這裏有很多的信息,並以不同的方式做聽衆另一個例子:ListView tutorial