2012-07-25 88 views
1

大家好。我的目標是當我想點擊ListView中的特定項目時,它會打開一個我編程的新活動。例如:當我點擊「João」時,我希望它打開一個名爲Joao的類。我試過使用簡單的代碼,但它不起作用。當我從ListView中點擊João的名字時,什麼也沒有發生。如何ListView使用onClick打開一個新的活動?

這是我的代碼,我使用:

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 ("João".equals(lv_arr[position])) { 
      Intent myIntent = new Intent(Searchsort.this, Joao.class); 
      startActivity(myIntent); 
     } 
    } 

} 
+0

什麼是使用一個開關或者如果條件的問題? – 2012-07-25 06:23:19

回答

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

     Intent intent = new Intent(Searchsort.this, Joao.class);             

        startActivity(intent);   
       } 
      }   
     }); 
+0

我想我向你解釋錯了。澄清和改進我的問題。字符串的每個元素(名稱)都應該打開一個我想要的新活動。例如「Mary」應該打開「x.class」。 John打開「y.class」,Charlie打開「y.class」,João「joao.class」,等等......明白了嗎? – user1550421 2012-07-25 05:44:25

+0

使用我的代碼有什麼問題? public void onListItemClick(ListView parent,View v,int position,long id){if(「João」.equals(lv_arr [position])){ 意圖myIntent = new Intent(Searchsort.this,Joao.class); startActivity(myIntent); } – user1550421 2012-07-25 13:46:26

0

使用setOnItemClickListenerListView和處理活動開幕那裏。

0

請檢查您的Androidmanifest.xml

添加聲明,如:

<activity android:name=".CLASS_NAME"> </activity> 

您必須聲明一樣,所有的活動。

+0

我已經宣佈它。但它仍然不起作用 – user1550421 2012-07-25 05:46:43

+0

是的。我知道。我宣佈了我的活動。但它仍然不起作用!我的代碼有什麼問題? – user1550421 2012-07-25 13:57:38

0

作爲Niko建議按以下方式使用setOnItemClickListener()。

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; 
private String getItem; 

/** 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.listView1); 
    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)); 
    // Here I have set Listener for listview 
    lv1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
// Here I am getting the selected item of listview. 
      getItem = (arg0.getItemAtPosition(arg2).toString()); 

      if(getItem.equals("John")) 
      { 
       Intent i = new Intent(Searchsort.this,John.class); 
       startActivity(i); 
      } 
     } 
    }); 
    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)); 
     } 
    }); 
}  
} 
+0

嗨。我試過這個。但不起作用。我在清單文件中提到了我的活動。你能澄清一下,把這段代碼放在我的代碼中嗎?正如我之前說過的:當我從ListView中點擊某個人的名字時沒有任何事情發生:/ – user1550421 2012-07-25 13:35:49

+0

@ user1550421我已經在帖子中進行了必要的更改,請找到它並讓我知道它是否正常工作。 :) – Akshay 2012-07-25 17:40:54

+0

@ user1550421你是否嘗試過更新的code.Is它工作與否? – Akshay 2012-07-28 06:07:59

0

試試這個..

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

if(position == 1) 
{ 

     Intent myIntent = new Intent(YourActivity.this, SecondActivity.class); 
      startActivityForResult(myIntent, 0); 
} 

if(position == 2) 
{ 

     Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class); 
      startActivityForResult(myIntent, 0); 
} 
    } 
    }); 

希望這將幫助你:-)

相關問題