自ArrayAdapter的onClick位置和放大器;排亮点 [英] Custom ArrayAdapter onClick position & row highlight

查看:306
本文介绍了自ArrayAdapter的onClick位置和放大器;排亮点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了工作,因为它应该禁止使用onClick的一个问题,一个自定义的ArrayList。我orginally过该ArrayList突出所选行中的红,当用户在该行点击。不过,我也需要一个ArrayAdapter类以外的点击的位置。

似乎我所能做到这一点的唯一方法是通过添加onClickListener,然而这是问题的开始,虽然这解决了我让用户点击位置的问题,现在,该行突出似乎得到覆盖。

所以基本上我的问题是允许被突出了选中的行和位置将被传递到一个ArrayAdapter类之外的变量,请参阅下面我的code:

数组适配器类:

 公共类店铺扩展ListActivity
{
    私有静态类MyAdapter扩展ArrayAdapter<串GT;
    {
        私人的ArrayList<串GT;数据1,数据2,数据3,数据4;        公共MyAdapter(上下文的背景下,INT资源,诠释textViewResourceId,ArrayList的<弦乐>数据1,ArrayList的<弦乐> DATA2,ArrayList的<弦乐>数据3,ArrayList的<弦乐>数据4)
        {
            超级(上下文,资源textViewResourceId,DATA1);
            this.data1 = DATA1;
            this.data2 = DATA2;
            this.data3 =数据3;
            this.data4 = DATA4;
        }        @覆盖
        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
        {
            视图V = super.getView(位置,convertView,父母);
            TextView的T1 =(TextView中)v.findViewById(R.id.data1);
            t1.setText(data1.get(位置));
            TextView的T2 =(TextView中)v.findViewById(R.id.data2);
            t2.setText(data2.get(位置));
            TextView的T3 =(TextView中)v.findViewById(R.id.data3);
            t3.setText(data3.get(位置));
            TextView的T4 =(TextView中)v.findViewById(R.id.data4);
            t4.setText(data4.get(位置));
            spnCharacters.setVisibility(View.GONE);            迭代器<整数GT;迭代= unavailableItem.iterator();
            而(iterator.hasNext())
            {
                INT NotAvailable = iterator.next();
                如果(位置== NotAvailable)
                {
                    v.setBackgroundColor(Color.rgb(170,170,170));
                }
            }
            如果(unavailableItem.size()== 1)
            {
                如果(位置== unavailableItem.get(0))
                {
                    v.setBackgroundColor(Color.rgb(170,170,170));
                }
            }
            如果(位置== 0)
            {
                v.setBackgroundResource(android.R.drawable.title_bar);
            }            v.setOnClickListener(新OnClickListener()
            {
                @覆盖
                公共无效的onClick(视图v)
                {
                    currentPositon =位置;
                    如果(data2.get(currentPositon).equals(0))
                    {
                        btnSell.setEnabled(假);
                    }
                    其他
                    {
                        btnSell.setEnabled(真);
                    }
                }
            });            返回伏;
        }
    }    私人无效NeedPosition()
    {
        INT位置= //需要ArrayAdapter这里确切位置
    }
}

ArrayAdapter XML:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>    <的TextView
        机器人:ID =@ + ID /数据1
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =5DP
        机器人:layout_weight =1.5
        机器人:重力=CENTER_HORIZONTAL
        机器人:文字颜色=#FFFFFF/>    <的TextView
        机器人:ID =@ + ID /数据2
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =5DP
        机器人:layout_weight =0.5
        机器人:重力=CENTER_HORIZONTAL
        机器人:文字颜色=#FFFFFF/>    <的TextView
        机器人:ID =@ + ID /数据3
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =5DP
        机器人:layout_weight =0.5
        机器人:重力=CENTER_HORIZONTAL
        机器人:文字颜色=#FFFFFF/>    <的TextView
        机器人:ID =@ + ID /数据4
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_margin =5DP
        机器人:layout_weight =0.5
        机器人:重力=CENTER_HORIZONTAL
        机器人:文字颜色=#FFFFFF/>< / LinearLayout中>

列表查看XML:

 < ListView控件
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / linearButtons
    机器人:layout_above =@ + ID / linerBuyBtns
    机器人:layout_marginTop =5DP
    机器人:layout_marginBottom =5DP
    机器人:listSelector =@绘制/ list_selector>
< /&的ListView GT;


解决方案

ListActvity有它自己的方式来管理小区点击。

 保护无效onListItemClick(ListView中升,视图V,INT位置,长的ID)

当你点击的ListView的行此方法将被调用。正如你所看到的第二放慢参数是位置需要

I have created a custom ArrayList which is working as it should barring an issue with the onClick. I orginally had the ArrayList highlight the selected row in red when the user clicked on the row. However, I also required the position of that click outside of the ArrayAdapter class.

The only way I could seem to do this is by adding an onClickListener, however this is were the issues began, although this solved my issue of getting the position the user click on, the row highlighting now seems to get overridden.

So basically my issue is allowing both selected rows to be highlighted and position to be passed to a variable outside of the ArrayAdapter class, please see my code below:

Array Adapter Class:

public class Shop extends ListActivity
{
    private static class MyAdapter extends ArrayAdapter<String> 
    {
        private ArrayList<String> data1, data2, data3, data4;

        public MyAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> data1, ArrayList<String>  data2, ArrayList<String>  data3, ArrayList<String>  data4) 
        {
            super(context, resource, textViewResourceId, data1);
            this.data1 = data1;
            this.data2 = data2;
            this.data3 = data3;
            this.data4 = data4;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 
        {
            View v = super.getView(position, convertView, parent);
            TextView t1 = (TextView) v.findViewById(R.id.data1);
            t1.setText(data1.get(position));
            TextView t2 = (TextView) v.findViewById(R.id.data2);
            t2.setText(data2.get(position));
            TextView t3 = (TextView) v.findViewById(R.id.data3);
            t3.setText(data3.get(position));
            TextView t4 = (TextView) v.findViewById(R.id.data4);
            t4.setText(data4.get(position));
            spnCharacters.setVisibility(View.GONE);

            Iterator<Integer> iterator = unavailableItem.iterator();
            while (iterator.hasNext())
            {
                int NotAvailable = iterator.next();
                if(position == NotAvailable)
                {
                    v.setBackgroundColor(Color.rgb(170, 170, 170));
                }
            }
            if(unavailableItem.size() == 1)
            {
                if(position == unavailableItem.get(0))
                {
                    v.setBackgroundColor(Color.rgb(170, 170, 170));
                }
            }
            if (position == 0) 
            {
                v.setBackgroundResource(android.R.drawable.title_bar);
            }

            v.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    currentPositon = position;
                    if(data2.get(currentPositon).equals("0"))
                    {
                        btnSell.setEnabled(false);
                    }
                    else
                    {
                        btnSell.setEnabled(true);
                    }
                }
            });

            return v;
        }
    }

    private void NeedPosition()
    {
        int position = // need ArrayAdapter exact position here
    }
}   

ArrayAdapter XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/data1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1.5"
        android:gravity="center_horizontal"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/data2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/data3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/data4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:textColor="#FFFFFF" />

</LinearLayout>

List View XML:

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearButtons"
    android:layout_above="@+id/linerBuyBtns" 
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:listSelector="@drawable/list_selector">
</ListView>

解决方案

ListActvity has its own way to manage cell click.

protected void onListItemClick (ListView l, View v, int position, long id)

This method will be called when you click on an row of the ListView. As you can see the second paramter is the position you need

这篇关于自ArrayAdapter的onClick位置和放大器;排亮点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆