我在android app中有一个搜索框,通过搜索展示一个ListView,我想在对应的选择项点击之后进入该app的其他activity中,应该怎么做? 我的代码如下:
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"
};
// 代表图片的int指针集合,图片存储在 /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);
// 遍历map获得每一个名称,标示
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
String[] from = { "flag","txt"};
// listview_layout 中使用的IDS
int[] to = { R.id.flag,R.id.txt};
// 怎对每一项创建一个 adapter
// 使用 R.layout.listview_layout 定义每一个
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.autocomplete_layout, from, to);
// 通过 activity_main.xml 配置获得 CustomAutoCompleteTextView 视图
CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) findViewById(R.id.autocomplete);
/** 为 autocompletetextview 定义一个点击项目时间监听*/
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<!--?--> arg0, View arg1, int position, long id) {
}
};
/** 设置事件*/
autoComplete.setOnItemClickListener(itemClickListener);
/** 设置 adapter */
autoComplete.setAdapter(adapter);
}
/**
回调方法,在activity 被kill掉的时候执行
主要是用来保存当前activity 的状态
*/
@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
回调方法,在activity 被重建的时候执行
*/
@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;
}
}</string,string></string,></hashmap<string,string></hashmap<string,string></hashmap<string,string>
处理方法
第一步:在 AndroidManifest 中声明你的activity被调用
<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);
}
};
第三步:在你的activity通过onCreate方法接受值
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download_pads_layout);
String country = getIntent().getStringExtra("country");
}