问题描述
我有一个第二活动它调用(ImageAndTextAdapter)来显示一个ListView活动(MainActivity)。
我希望能够点击每个列表项,打开一个新的活动。不过,我不知道如何正确地贯彻setOnItemClickListener功能。我已经尝试了许多不同的方式。任何帮助将AP preciated。
MainActivity:
公共类MainActivity扩展ListActivity {@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 上下文CTX = getApplicationContext();
资源解析度= ctx.getResources();的String []选项= res.getStringArray(R.array.country_names);
TypedArray图标= res.obtainTypedArray(R.array.country_icons);setListAdapter(新ImageAndTextAdapter(CTX,R.layout.main_list_item,选项图标));
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;}
}
ImageAndTextAdapter.java:
公共类ImageAndTextAdapter扩展ArrayAdapter<串GT; {私人LayoutInflater mInflater;私有String [] mStrings;
私人TypedArray mIcons;私人诠释mViewResourceId;公共ImageAndTextAdapter(上下文CTX,诠释viewResourceId,
的String []字符串,TypedArray图标){
超(CTX,viewResourceId,字符串);mInflater =(LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);mStrings =串;
mIcons =图标;mViewResourceId = viewResourceId;
}@覆盖
公众诠释的getCount(){
返回mStrings.length;
}@覆盖
公共字符串的getItem(INT位置){
返回mStrings [位置]
}@覆盖
众长getItemId(INT位置){
返回的位置;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
convertView = mInflater.inflate(mViewResourceId,NULL);ImageView的IV =(ImageView的)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(位置));TextView的电视=(TextView中)convertView.findViewById(R.id.option_text);
tv.setText(mStrings [位置]);返回convertView;
}
}
XML文件:
<资源>
<字符串数组名=country_names>
<项目>哥伦比亚< /项目>
<项目>秘鲁< /项目>
<项目>阿根廷< /项目>
< /字符串数组>
<数组名=country_icons>
<项目> @绘制/哥伦比亚< /项目>
<项目> @绘制/秘鲁< /项目>
<项目> @绘制/阿根廷< /项目>
< /阵列>
< /资源>
在你onCreate()方法,在 setListAdapter(新ImageAndTextAdapter(CTX,R.layout.main_list_item,选项图标));
,使用:
getListView()。setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,
长ID){
//处理您的请点击这里,只要你想 }
});
I have an activity (MainActivity) which calls on a 2nd Activity (ImageAndTextAdapter) to display a listview.I want to be able to click on each list item to open a new activity. However I do not know how to implement the setOnItemClickListener function correctly. I have tried many different ways. Any help will be appreciated.
MainActivity:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.country_names);
TypedArray icons = res.obtainTypedArray(R.array.country_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.main_list_item, options, icons));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;}
}
ImageAndTextAdapter.java:
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public String getItem(int position) {
return mStrings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
XML File:
<resources>
<string-array name="country_names">
<item>Columbia</item>
<item>Peru</item>
<item>Argentina</item>
</string-array>
<array name="country_icons">
<item>@drawable/Columbia</item>
<item>@drawable/Peru</item>
<item>@drawable/Argentina</item>
</array>
</resources>
In your onCreate() method, after setListAdapter(new ImageAndTextAdapter(ctx, R.layout.main_list_item, options, icons));
, use:
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Handle your click here as you want
}
});
这篇关于如何实现OnClickListener的ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!