使用onClick()为getAdadter扩展类的getView()设置文本

时间:2013-08-01 12:00:56

标签: android onclick baseadapter

我正在使用openaphid提供的其中一个类来执行翻转视图。我遇到的问题(因为我刚开始发现)是因为新视图被创建而旧的视图被废弃(在getView()内),我的变量的引用被更新,因此,有不同的ID,这导致我无法更改它们。

基本上,我所拥有的是2 TextView。其中一个TextView s(txtvwCharacter)已在XML中设置为可点击,而我要做的是:点击/触摸txtvwCharacter时,其他TextViewtxtvwComposition)在"some text"""之间切换(空字符串)。

下面是我的代码的简化版本,因为我只专注于getView()方法:

public class TravelAdapter extends BaseAdapter 
{
    private LayoutInflater inflater;

    ArrayList<String> characterArray = new ArrayList<String>();
    ArrayList<String> compositionArray = new ArrayList<String>();

    TextView txtvwCharacter, txtvwComposition;

    boolean detailsOn = false;

    public TravelAdapter(Context context) 
    {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View layout = convertView;

        if (convertView == null) 
            layout = inflater.inflate(R.layout.complex1, null);

        txtvwCharacter = UI.<TextView>findViewById(layout, R.id.txtvw_character);
        txtvwComposition = UI.<TextView>findViewById(layout, R.id.txtvw_composition);

        txtvwCharacter.setText(characterArray.get(position));
        txtvwComposition.setText("");

        txtvwCharacter.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(detailsOn == false)
                {
                    txtvwComposition.setText(compositionArray.get(position));
                    detailsOn = true;
                }
                else
                {
                    txtvwComposition.setText("");
                    detailsOn = false;
                }
            }
        });

        return layout;
    }
}

目前,使用此代码,onClick确实已注册且txtvwComposition确实已设置,但它与显示的txtvwComposition不同,因此,在视觉上,它看起来什么都没发生。

我想在这里有某种持久性,当我点击txtvwCharacter时,它知道它是一个TextView对象,而不是一个在所有对象之间实例化的对象来回翻转。 (对不起,我不知道如何更好地说出来。)

我查看了thisthis,但我不明白ViewHolder做了什么,它怎么知道“持有”,或者如何将它应用到我的身上具体代码在这里。

代码编辑:

public class TravelAdapter extends BaseAdapter 
{
    private LayoutInflater inflater;

    ArrayList<String> characterArray = new ArrayList<String>();
    ArrayList<String> compositionArray = new ArrayList<String>();

    TextView txtvwCharacter, txtvwComposition;

    boolean detailsOn = false;

    public TravelAdapter(Context context) 
    {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View layout = convertView;

        if (convertView == null) 
            layout = inflater.inflate(R.layout.complex1, null);

        txtvwCharacter = UI.<TextView>findViewById(layout, R.id.txtvw_character);
        txtvwComposition = UI.<TextView>findViewById(layout, R.id.txtvw_composition);

        if (detailsOn == false)
        {
            txtvwCharacter.setText(characterArray.get(position));
            txtvwComposition.setText("");
        }
        else
        {
            txtvwCharacter.setText(characterArray.get(position));
            txtvwComposition.setText(compositionArray.get(position));
        }

        txtvwCharacter.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                RelativeLayout root = (RelativeLayout) v.getParent();
                TextView txtvwComposition = (TextView) root.findViewById(R.id.txtvw_composition);

                if(detailsOn == false)
                {
                    txtvwComposition.setText(compositionArray.get(position));
                    detailsOn = true;
                }
                else
                {
                    txtvwComposition.setText("");
                    detailsOn = false;
                }
            }
        });

        return layout;
    }
}

0 个答案:

没有答案