首页 \ 问答 \ android - 在活动中是正确的从自定义侦听器获取位置(android - in actvity is correct get position from custom listener)

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
更新时间:2021-11-06 20:11

最满意答案

使用子查询:

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;

相关问答

更多
  • 写这个的正确方法是: 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 ...
  • 使用左连接而不是内连接 内部联接需要在联接两侧的表中匹配行。 左连接(或左外连接使用其全名)选择左表中的所有行,然后匹配右行,如果右表中没有匹配行,则返回空值,就像您要求的一样。 (也有右外连接,但通过改变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语句中 ...
  • 您需要按功能和分组依据使用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 ...
  • 您可能想尝试以下操作: 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) ...
  • 在这种情况下,你绝对不应该加入约会。 例如,您可能拥有以下数据: deskcases ringcentral --------- ----------- date | whatever_d date | whatever_r ---------------------- ---------------------- today ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)