2015-06-06 35 views
0

MainActivitty.java如何進入所選擇的活動列表視圖

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.EditText; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 

public class MainActivity extends Activity { 
     // list view 
     private ListView lv; 

     // listview adapter 
     ArrayAdapter<String> adapter; 

     // search edittext 
     EditText inputsearch; 

     // arraylist for listview 
     ArrayList<HashMap<String, String>> negaralist; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // data array yang akan ditampilkan di list 
    String negara[] = {"indonesia","malaysia","brunei","filipina", 
       "thailand","myanmar","singapura","vietnam", 
       "kamboja","laos","brazil","amerika","inggris","italia","france","kamerun", 
       "argentina","portugal","germany","spanyol","mexico","kanada","belgium"}; 
    // ambil list_view dan inputsearchnya di xml 
    lv = (ListView) findViewById(R.id.list_view); 
    inputsearch = (EditText) findViewById(R.id.inputSearch); 

    // proses menambahkan array kedalam listview 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item_filter, 
     R.id.negara_name, negara); 
    lv.setAdapter(adapter); 

    /** 
    * kode dibawah ini dipakai untuk memfilter/pencarian realtime 
    * */ 
    inputsearch.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
     MainActivity.this.adapter.getFilter().filter(s); 


    } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      // TODO Auto-generated method stub 

     }}); 

    lv.setOnItemClickListener (new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){ 
      Intent i = null; 
      if (position == 0){ 
       i = new Intent(MainActivity.this, Indonesia.class);} 
      else if(position == 1){ 
       i = new Intent(MainActivity.this, Malaysia.class);} 
else if(position == 2){ 
       i = new Intent(MainActivity.this, Brunei.class);} 
else if(position == 3){ 
       i = new Intent(MainActivity.this, Filipina.class);} 
else if(position == 4){ 
       i = new Intent(MainActivity.this, Thailand.class);} 
else if(position == 5){ 
       i = new Intent(MainActivity.this, Myanmar.class);} 
else if(position == 6){ 
       i = new Intent(MainActivity.this, Singapur.class);} 
else if(position == 7){ 
       i = new Intent(MainActivity.this, Vietnam.class);} 
     startActivity(i); 

     } 
     });} 
    public void onBackPressed() { 
     finish(); 
     System.exit(0); 
      } 

    } 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


    <EditText android:id="@+id/inputSearch" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Search Country.." 
     /> 


    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</Linear 

lis_item_filter.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" >  

    <TextView android:id="@+id/negara_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     android:textSize="16dip" 
     android:textStyle="bold"/>  

</LinearLayout> 

時,我一直在尋找一個國家印尼,英格蘭和意大利的關鍵詞「我」。當我選擇印度尼西亞,進入印度尼西亞活動,但是當我選擇inggris時爲什麼進入馬來西亞活動。 如何進入選定的活動?點擊監聽器有什麼問題?幫助我

+1

列表中沒有英文 – 3xplore

+0

@ r7v您的意思是英格蘭?另外,iyyan71,在提問時你應該更清楚。請告訴你已經努力研究解決你的問題,並避免簡單地粘貼代碼讓我們理清。 –

+0

對不起,我的意思是inggris,從字母「i」開始,不是英文。請幫助我,如何進入選擇的活動。 clicklistener有什麼問題? – iyyan71

回答

0

'位置'是來自lv對象,因此您的過濾列表,而不是您的原始字符串列表。因此,當您選擇「inggris」時,重新設置的位置是1並開始馬來西亞活動。請嘗試:

public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3){ 
LinearLayout tmpll = (LinearLayout) view; 
TextView tmptv = (TextView) tmpll.findViewById(R.id.negara_name); 
pos = negara.indexOf(tmptv.getText()); 

並將其他「位置」更改爲「pos」。

相關問題