Listview 如何使用if-in-click-listener进行不同的活动?
我在我的应用程序的第一页有搜索栏,当我写搜索栏打开搜索辅助(帮助列表视图)时,我想单击列表视图的每个项目转到不同的活动 问题:我不知道在该列表视图中使用Listview 如何使用if-in-click-listener进行不同的活动?,listview,android-intent,android-listview,android,Listview,Android Intent,Android Listview,Android,我在我的应用程序的第一页有搜索栏,当我写搜索栏打开搜索辅助(帮助列表视图)时,我想单击列表视图的每个项目转到不同的活动 问题:我不知道在该列表视图中使用if来转到其他活动。我测试了这段代码,但在firstif中只进行了第一个活动 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.Intent
if
来转到其他活动。我测试了这段代码,但在firstif
中只进行了第一个活动
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
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 {
ArrayList<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<?> parent, View view, int position, long id) {
if(parent.getAdapter().getItem(position).equals("India")){
Intent intent = new Intent(MainActivity.this, TestPage.class);
startActivity(intent);
} else if(parent.getAdapter().getItem(position).equals("Pakistan")){
Intent intent = new Intent(MainActivity.this, TestPage2.class);
startActivity(intent);
}
}
};
/** 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;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.simpledapter;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
ArrayList adp;
//存储国家名称的字符串数组
字符串[]国家/地区=新字符串[]{
“印度”,
“巴基斯坦”,
“斯里兰卡”,
“中国”,
“孟加拉国”,
“尼泊尔”,
“阿富汗”,
“朝鲜”,
“韩国”,
“日本”
};
//整数数组指向存储在/res/drawable ldpi中的图像/
int[]标志=新的int[]{
R.drawable.印度,
R.drawable.巴基斯坦,
R.drawable.srilanka,
R.drawable.china,
R.drawable.孟加拉国,
R.drawable.尼泊尔,
R.drawable.阿富汗,
R.drawable.nkorea,
R.drawable.skorea,
日本
};
//存储货币的字符串数组
字符串[]货币=新字符串[]{
“印度卢比”,
“巴基斯坦卢比”,
“斯里兰卡卢比”,
“人民币”,
“孟加拉国塔卡”,
“尼泊尔卢比”,
“阿富汗人”,
“朝鲜赢了”,
“韩元”,
“日元”
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//列表中的每一行存储国家名称、货币和国旗
列表列表=新的ArrayList();
对于listview中的(int i=0;i,当您使用自定义适配器时,我可以看到在hashmap中,您添加了三个内容,因此您应该使用listview的onimclick
侦听器中的contains
方法
你的代码是一个文件。
将equals
替换为contains
自定义listview在您使用时将不支持onitemclicklistner。如果您在baseadapter中为每行调用setonclicklistner会更好。这将非常有效。请尝试一下,如果需要更多帮助,请告诉我。谢谢如果您单击Paki的listitem,会发生什么stan?您应该首先检查getItem()
的返回类型。它是否返回您认为它返回的项目?如何使用getItem()在我的代码中,您的确切意思是什么,对不起,我是eclipsetested的新手,但不起作用。在搜索项目后,单击“项目”跳转应用程序首先调试您的应用程序,或者将整个项目粘贴到某个位置。我将查看它