在我的应用程序中,我有一个自定义监听器的列表视图。android-在活动中是正确的从自定义监听器获取位置
我想在我的活动中使用onSaveInstanceState和onRestoreInstanceState,但我需要的唯一信息是在我的类CustomListener中的方法OnItemClick。
我需要4个onItemClick参数之一的参数“position”的值,因为是唯一能够知道列表中的哪个位置被点击的项目。在onSaveInstanceState和onRestoreInstanceState两个方法中,我需要知道正确恢复活动的状态。
我的问题有两个: 1 - 是正确的(是最佳实践)创建方法获取和在CustomListener设置和活动,其中有两种方法的onSaveInstanceState和onRestoreInstanceState使用它们(我认为这是不应该做的方式这个,但我没有别人的想法) 2 - 一旦我有参数位置,我有问题在CustomListener的方法OnItemClick内设置此参数,因为如果我在纵向模式,并且我传递到横向模式,系统不会调用OnItemClick,因此方法中的参数不会修改,而且横向布局也不会被修改。
这种方法是在MainActivity:
private void populate(ListView lv){
ImageAdapter arrayAdapter;
//set list view
arrayAdapter = new ImageAdapter(this, R.layout.list_view_layout, arrayFromNumberToId, hmFromIdToName);
lv.setAdapter(arrayAdapter);
//set list listener
listener=new CustomListener(hm, hmFromIdToName, arrayFromNumberToId);
lv.setOnItemClickListener(listener);
}
这种方法是在类CustomListener:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int imageID;
String description;
String name;
imageID = arrayFromNumberToId[position];
description = hm.get(imageID);
name = hmIdToName.get(arrayFromNumberToId[position]);
extras = new Bundle();
extras.putInt("imageID", imageID);
extras.putCharSequence("description", description);
extras.putCharSequence("name", name);
if(parent.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Intent intent = new Intent(parent.getContext(), DescriptionActivity.class);
intent.putExtras(extras);
parent.getContext().startActivity(intent);
}
else{
ImageView imageViewLayout;
TextView textView;
textView = (TextView) parent.getRootView().findViewById(R.id.textDescription);
textView.setText(description);
imageViewLayout = (ImageView) parent.getRootView().findViewById(R.id.imageBig);
imageViewLayout.setImageResource(imageID);
textView = (TextView) parent.getRootView().findViewById(R.id.textName);
textView.setText(name);
textView.setTypeface(null, Typeface.BOLD);
}
}
谢谢您的回答!
非常感谢@Jason的回答! – soissy