android - 在活动中是正确的从自定义侦听器获取位置(android - in actvity is correct get position from custom listener)
在我的应用程序中,我有一个自定义侦听器的列表视图。
我想在我的活动中使用onSaveInstanceState和onRestoreInstanceState,但我需要的唯一信息是在我的类CustomListener中的方法OnItemClick中。
我需要参数“position”的值,它是4个onItemClick参数中的一个,因为是唯一能够知道列表中单击项目的位置的唯一方法。 在onSaveInstanceState和onRestoreInstanceState两个方法中,我需要知道正确恢复活动的状态。
我的问题是两个:1 - 是正确的(最好的做法)创建方法获取和设置在CustomListener中,并在Activity中使用它们,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); } }
感谢您的回答!
In my app i have a listview with a custom listener.
I would like to use onSaveInstanceState and onRestoreInstanceState in my activity, but the only information that i need is in method OnItemClick inside my class CustomListener.
I need the value of parameter "position", one of 4 onItemClick's parameters, because is the only way to know in what position of list was clicked item. And in the two mthods onSaveInstanceState and onRestoreInstanceState i need to know to restore correctly the activity's state.
My questions are two: 1 - is correct(is best practise) create methods get and set in CustomListener and use them in Activity, where are the two methods onSaveInstanceState and onRestoreInstanceState (I think is not the right way to do this, but I don't have others ideas) 2 - once i have the parameter position, i have the problem to set this parameter inside CustomListener's method OnItemClick, because if i am in portrait mode and i pass to landscape mode, the system doesn't call OnItemClick and so the parameter inside the method will not modify, and also the landscape layout won't be modify.
this method is in 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); }
This method is in class 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); } }
THANKS FOR YOUR ANSWERS!!!
原文:https://stackoverflow.com/questions/43352515
最满意答案
使用子查询:
SELECT rh.group_area, COUNT(*) FROM (SELECT rh.id_subscriber, rh.bill_month, rh.bill_year, (SELECT tbl_gen_info.gen_data_03 FROM tbl_subscriber LEFT JOIN tbl_gen_info ON tbl_subscriber.bill_area_code = tbl_gen_info.gen_data_01 WHERE rh.id_subscriber = tbl_subscriber.id_subscriber ) as group_area FROM tbl_reading_head rh WHERE rh.id_soa_head IS NULL AND rh.read_status <> 'Beginning' AND rh.rec_status = 'active' ) rh GROUP BY rh.group_area;
Use a subquery:
SELECT rh.group_area, COUNT(*) FROM (SELECT rh.id_subscriber, rh.bill_month, rh.bill_year, (SELECT tbl_gen_info.gen_data_03 FROM tbl_subscriber LEFT JOIN tbl_gen_info ON tbl_subscriber.bill_area_code = tbl_gen_info.gen_data_01 WHERE rh.id_subscriber = tbl_subscriber.id_subscriber ) as group_area FROM tbl_reading_head rh WHERE rh.id_soa_head IS NULL AND rh.read_status <> 'Beginning' AND rh.rec_status = 'active' ) rh GROUP BY rh.group_area;
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
写这个的正确方法是: SELECT d.*, COUNT(ud.DiscussionID) as BookmarkCount FROM Discussion d LEFT JOIN UserDiscussion ud ON ud.DiscussionID = d.DiscussionID AND ud.Bookmarked = true GROUP BY d.DiscussionId; 变化是: 在第二个表的字段上使用COUNT() 。 这将计算匹配数。 在这种情况下, ...
-
使用子查询: SELECT rh.group_area, COUNT(*) FROM (SELECT rh.id_subscriber, rh.bill_month, rh.bill_year, (SELECT tbl_gen_info.gen_data_03 FROM tbl_subscriber LEFT JOIN tbl_gen_info ON tbl_subscribe ...
-
MySQL加入查询帮助(MySQL join query help)[2020-01-17]
使用左连接而不是内连接 内部联接需要在联接两侧的表中匹配行。 左连接(或左外连接使用其全名)选择左表中的所有行,然后匹配右行,如果右表中没有匹配行,则返回空值,就像您要求的一样。 (也有右外连接,但通过改变ON子句中的条件可以实现相同的效果) Use a left join instead of an inner An inner join requires matching rows from the table on both sides of the join. A left join (or lef ... -
不确定mysql但sql server允许你使用'select statment>'更新'table'set'asignation'[,'asignation']就像这样 UPDATE vendors ven1 SET ven1.parent_name = ven2.vendor_name FROM vendors ven2 其中ven1.parent_id = ven2.ID not sure about mysql but sql server allows you to use update 'ta ...
-
SELECT ap.formId, ap.entryID, preferences.userEmail FROM ( SELECT formId, COUNT(entryID) entryID FROM approvalDetails GROUP BY formId ) ap INNER JOIN preferences ON ap.formId = preferences.formId GROUP BY ap.formId, ...
-
SELECT catname= CASE name WHEN '' THEN (select name from Category where lang=$default_lang limit 1) ELSE name END FROM Category where id=$current_lang 解释:这里Query在select statment中使用case语句,其中外部查询使用条件“id = $ current_lang” 所以在select语句中 ...
-
MySQL查询计数和加入(MySQL Query Count and Join)[2022-06-09]
您需要按功能和分组依据使用count()组来获得所需的输出。 我还重写了连接以使用内部连接和显式连接语法。 SELECT shipments.file_number, file_creation_date, city_name, count(container_sequence) as number_of_containers FROM cities INNER JOIN countries ON cities.country_code = countries.country_code INNER JOI ... -
MySQL查询加入和计数查询(MySQL Query Join and Count Query)[2023-07-03]
您可能想尝试以下操作: SELECT i.id, i.industry, count(l.id) as count FROM industries i LEFT JOIN ( SELECT l.industry, l.id FROM links l JOIN companies c ON (l.company = c.id AND c.active = 1) ... -
MySQL计数IF和JOIN(MySQL Count IF and JOIN)[2021-11-14]
在这种情况下,你绝对不应该加入约会。 例如,您可能拥有以下数据: deskcases ringcentral --------- ----------- date | whatever_d date | whatever_r ---------------------- ---------------------- today ...