我是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"));
首先創建你的ListView的一個實例。作爲ListView list =(ListView)findViewById(R.id.main_list);然後調用它的onItemClickListener()方法 –
在適配器den中添加項目後,將其設置爲listview。 .not不應該添加項目到適配器,因爲你在這裏做..deniteiteclick()獲取項目位置書庫傳遞意圖到每個位置。如果你需要代碼我可以提供 – Meghna