2013-09-23 19 views
1
package com.example.app; 

import com.example.app.adaptor.*; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ListView; 


public class ListviewExample extends ListActivity { 

static final String[] ASDFGH = new String[] { "ABC", "PQR", 
     "XYZ", "RAA","AA" }; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



    setListAdapter(new etcArray(this, main_activity)); 

} 


@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    // get selected items 
    String selectedValue = (String) getListAdapter().getItem(position); 
      if(selectedValue.equalsIgnoreCase("ABC")) 
    { 
     Intent in = new Intent(getApplicationContext(),ABC.class); 
     startActivity(in); 
    } 
    else 
     if(selectedValue.equalsIgnoreCase("AA")) 
     { 
      Intent in = new Intent(getApplicationContext(),AA.class);   
      startActivity(in); 
     } 

    } 

在上面的程序..使用正常情況下,如果string.equalsIgnorecase(「AA」) 我們可以啓動該具體點擊的意圖!是否有任何方法來啓動任何listview項目點擊???如何在Listview(item)上加載特定活動(Intent)單擊?

回答

0

創建一流的陣列,以及所以你可以指向準確輕鬆......活動

static final Class[] classArray = new Class[] { ABC.class,PQR.class,XYZ.class,RAA.class,AA.class }; 
static final String[] ASDFGH = new String[] { "ABC", "PQR", 
"XYZ", "RAA","AA" }; 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
Intent intent=newIntent(getApplicationContext(),classArray[position]); 
startActivity(intent); 
} 
+0

謝謝你的幫助..類型不匹配錯誤:你不能將String []轉換爲Class [] – Xy1ys

+0

請檢查更新現在..實際上它是Class對象。錯誤我寫了String .. –

0

您可以使用下面的代碼:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    Class<?> nextClass = null; 
    // get selected items 
    String selectedValue = (String) getListAdapter().getItem(position); 
    if(selectedValue.equalsIgnoreCase("ABC")) 
     nextClass = ABC.class; 
    else if(selectedValue.equalsIgnoreCase("AA")) 
     nextClass = AA.class; 
    if(nextClass != null) 
     startActivity(new Intent(context, nextClass)); 

    } 
0

嘗試下面的代碼運行。

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 


    // get selected items 

    if(position == 0) 
    { 
     Intent intent=newIntent(this, Class1.class); 
     startActivity(intent); 
    } else if(poition == 1) 
    { 
     Intent intent=newIntent(this, Class2.class); 
     startActivity(intent); 
    } else if(poition == 1) 
     Intent intent=newIntent(this, Class3.class); 
     startActivity(intent); 

} 
相關問題