2013-12-19 14 views
1

我在应用程序的第一页搜索栏,当我写来搜索栏中打开搜索辅助(求助列表视图),我想,当点击打开的列表视图的项目,去应用当我点击列表视图的项目时,我该如何去其他活动?

的其他活动

感谢帮助

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 


public class MainActivity extends Activity { 

    List<HashMap<String,String>> adp; 
    // Array of strings storing country names 
    String[] countries = new String[] { 
      "India", 
      "Pakistan", 
      "Sri Lanka", 
      "China", 
      "Bangladesh", 
      "Nepal", 
      "Afghanistan", 
      "North Korea", 
      "South Korea", 
      "Japan" 
    }; 

// Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[]{ 
       R.drawable.india, 
       R.drawable.pakistan, 
       R.drawable.srilanka, 
       R.drawable.china, 
       R.drawable.bangladesh, 
       R.drawable.nepal, 
       R.drawable.afghanistan, 
       R.drawable.nkorea, 
       R.drawable.skorea, 
       R.drawable.japan 
    }; 

    // Array of strings to store currencies 
    String[] currency = new String[]{ 
     "Indian Rupee", 
     "Pakistani Rupee", 
     "Sri Lankan Rupee", 
     "Renminbi", 
     "Bangladeshi Taka", 
     "Nepalese Rupee", 
     "Afghani", 
     "North Korean Won", 
     "South Korean Won", 
     "Japanese Yen" 
    }; 




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


     // Each row in the list stores country name, currency and flag 
     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

     for(int i=0;i<10;i++){ 
       HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", countries[i]); 
      hm.put("flag", Integer.toString(flags[i])); 
      hm.put("cur", currency[i]); 
      aList.add(hm); 
     } 

     // Keys used in Hashmap 
     String[] from = { "flag","txt"}; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag,R.id.txt}; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.autocomplete_layout, from, to); 

     // Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file 
     CustomAutoCompleteTextView autoComplete = (CustomAutoCompleteTextView) findViewById(R.id.autocomplete); 

     /** Defining an itemclick event listener for the autocompletetextview */ 
     OnItemClickListener itemClickListener = new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { 


      } 
     }; 

     /** Setting the itemclick event listener */ 
     autoComplete.setOnItemClickListener(itemClickListener); 

     /** Setting the adapter to the listView */ 
     autoComplete.setAdapter(adapter); 

    } 

    /** A callback method, which is executed when this activity is about to be killed 
    * This is used to save the current state of the activity 
    * (eg : Configuration changes : portrait -> landscape) 
    */ 
    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;  
     outState.putString("currency", tvCurrency.getText().toString()); 
     super.onSaveInstanceState(outState); 
    } 

    /** A callback method, which is executed when the activity is recreated 
    * (eg : Configuration changes : portrait -> landscape) 
    */ 
    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;  
     tvCurrency.setText(savedInstanceState.getString("currency")); 
     super.onRestoreInstanceState(savedInstanceState); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

回答

2

首先声明在AndroidManifest将推出的活动。

<application 
...   
    <activity 
      android:name="com.hello.world.MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.hello.world.MyNextActivity"/> 
... 
</application> 

现在做这在您的项目点击收听

/** Defining an itemclick event listener for the autocompletetextview */ 
    OnItemClickListener itemClickListener = new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { 
      Intent intent = new Intent(MainActivity.this, MyNextActivity.class); 
      //Get the value of the item you clicked 
      String itemClicked = countries[position]; 
      intent.putExtra("country", itemClicked); 
      startActivity(intent); 

     } 
}; 
在你的下一个活动

现在检索您的onCreate

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.download_pads_layout); 

    String country = getIntent().getStringExtra("country"); 

} 
+0

喜switch语句和答案 – user3101008

+0

坦克,但我有几个活动的,我想,当点击每个项目去不同的活动 – user3101008

+0

给我回答,请 – user3101008

1

值在你当前活动

countries.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 

      if (position == 0) { 
       Intent india = new Intent(MainActivity.this, newActivityIndia.class); 
       startActivity(india); 
      } 
      else if (position == 1) { 
       Intent pakistan = new Intent(MainActivity.this, newActivityPakistan.class); 
       startActivity(pakistan); 
      } 
      else if (position == 2) { 
       Intent shriLanka = new Intent(MainActivity.this, newActivityShriLanka.class); 
       startActivity(shriLanka); 
      } 
      else if (position == 3) { 
       Intent china= new Intent(MainActivity.this, newActivityChina.class); 
       startActivity(china); 
      } 
      else if (position == 4) { 
       Intent bangladesh= new Intent(MainActivity.this, newActivityBangladesh.class); 
       startActivity(bangladesh); 
      } 
      else if (position == 5) { 
       Intent nepal= new Intent(MainActivity.this, newActivityNepal.class); 
       startActivity(nepal); 
      } 
      else if (position == 6) { 
       Intent afghanistan = new Intent(MainActivity.this, newActivityAfghanistan.class); 
       startActivity(afghanistan); 
      } 
      else if (position == 7) { 
       Intent northKorea= new Intent(MainActivity.this, newActivityNorthKorea.class); 
       startActivity(northKorea); 
      } 
      else if (position == 8) { 
       Intent southKorea= new Intent(MainActivity.this, newActivitySouthKorea.class); 
       startActivity(southKorea); 
      } 
      else if (position == 9) { 
       Intent japan= new Intent(MainActivity.this, newActivityJapan.class); 
       startActivity(japan); 
      } 


     } 
}; 
+0

添加一些解释并回答这个答案如何帮助OP解决当前问题 –

0
你可以使用

例如

adapter= new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayCountry); 
    listView.setAdapter(adapter); 

    getResources().getStringArray(R.array.array_country); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

      switch (position) { 
       case 0: 
        startActivity(new Intent(MainActivity.this, ActivityAustralia.class));break; 

       case 1: 
        startActivity(new Intent(MainActivity.this, ActivityJapan.class));break; 
       case 2: 
        startActivity(new Intent(MainActivity.this, ActivityChina.class));break; 

      } 
     } 
    }); 

    listView.setAdapter(adapter); 

} 
相关问题