2012-07-25 29 views
0

大家好。我是新手。我的目標是當我想點擊ListView中的特定項目時,它應該打開一個我編程的新活動。例如:當我點擊「John」時,我希望它打開一個叫做X的類Mary,應該打開一個類Y,查理,類Z,等等。我試過在代碼的末尾使用「onListItemClick」代碼,但它不起作用。當我點擊來自ListView應用程序的John的名字時,什麼也沒有發生。請在我的代碼出錯的地方幫我把代碼放在哪裏以及代碼是什麼。ListView:應該開一個新的活動

P.S:我宣佈清單文件

所有活動這是我的代碼,我使用:

public class Searchsort extends Activity { 

    private ListView lv1; 
    private EditText ed; 
    private String lv_arr[]={"John","Mary","Carl","Rose","Charlie","Allan", "João"}; 
    private ArrayList<String> arr_sort= new ArrayList<String>(); 
    int textlength=0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     lv1 = (ListView)findViewById(R.id.ListView01); 
     ed = (EditText)findViewById(R.id.EditText01); 

     // By using setAdpater method in listview we an add string array in list. 

     lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr)); 
     ed.addTextChangedListener(new TextWatcher() { 

      public void afterTextChanged(Editable s) { 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       textlength=ed.getText().length(); 
       arr_sort.clear(); 
       for(int i=0;i<lv_arr.length;i++) { 
        if(textlength<=lv_arr[i].length()) { 
         if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) { 
          arr_sort.add(lv_arr[i]); 
         } 
        } 
       } 

       lv1.setAdapter(new ArrayAdapter<String> (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort)); 

      } 
     }); 
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) { 
     if ("John".equals(lv_arr[position])) { 
      Intent myIntent = new Intent(Searchsort.this, X.class); 
      startActivity(myIntent); 

     if ("Mary".equals(lv_arr[position])) { 
      Intent myIntent = new Intent(Searchsort.this, Y.class); 
      startActivity(myIntent); 

     if ("Charlie".equals(lv_arr[position])) { 
      Intent myIntent = new Intent(Searchsort.this, W.class); 
      startActivity(myIntent); 


     } 
    } 

} 

回答

1

覆蓋onListItemClick方法是,如果你的活動擴展ListActivity的唯一途徑。因爲它不,你將不得不在你的onListItemClick方法僅供參考移動你的代碼到適當的方法您的項目設置中點擊收聽

lv1.setOnItemClickListener(new onItemClickListener(){ 

    @Override 
    protected void onListItemClick(AdapterView<?> arg0, View view, int position, long id){ 
     // Your code 
    } 
}); 

,這是一個類似的問題問了幾次的堆棧。

0
listView1.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) { 
     Toast.makeText(cxt, "You select Item " + (position + 1), Toast.LENGTH_SHORT).show();  
    } 
}); 
0

試試這個代碼..

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, 
      long paramLong) { 
     switch(position) 
{ 
    case 0: Intent newActivity0 = new Intent(YourActivity.this, second.class);  
      startActivity(newActivity0); 
      break; 
    case 1: Intent newActivity1 = new Intent(YourActivity.this, third.class);  
      startActivity(newActivity1); 
      break; 
    case 2: Intent newActivity2 = new Intent(YourActivity.this, fourth.class);  
      startActivity(newActivity2); 
      break; 
    case 3: Intent newActivity3 = new Intent(YourActivity.this, fifth.class);  
      startActivity(newActivity3); 
      break; 
    case 4: Intent newActivity4 = new Intent(YourActivity.this, sixth.class);  
      startActivity(newActivity4); 
      break; 
}    
    } 
}); 

嘗試,讓我知道...希望這可以幫助你:-)

相關問題