0

onClickの問題が発生しないように機能するカスタムArrayListを作成しました。私は当初、ユーザーが行をクリックしたときに、ArrayListで選択した行を赤で強調表示していました。ただし、ArrayAdapterクラスの外側でそのクリックの位置も必要でした。

これを行うことができる唯一の方法は、onClickListenerを追加することですが、これが問題の始まりでした。これにより、ユーザーがクリックする位置を取得するという私の問題は解決しましたが、行の強調表示が上書きされるようになりました。

したがって、基本的に私の問題は、選択した行の両方を強調表示し、位置をArrayAdapterクラスの外部の変数に渡すことを許可することです。以下のコードを参照してください。

アレイアダプタクラス:

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>

リストビュー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>
4

3 に答える 3

0

これを試して...

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
{    
    onClick(...)  
    {    
       convertView.parent().setSelection(position); <---
    }
}
于 2012-11-30T08:46:06.223 に答える
0

ListActvityには、セルのクリックを管理する独自の方法があります。

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

このメソッドは、の行をクリックすると呼び出されますListView。ご覧のとおり、2番目のパラメータはposition必要なものです

于 2012-11-30T08:47:48.520 に答える
0

カスタムlistViewsと配列アダプタでも同様の問題が発生しました。

私にとってはandroid:listSelector正しく機能しなかったので、android:Background代わりに、選択した状態や強調表示した状態などを定義した場所を使用しました。最後に、特定の視覚効果を実現するには、両方の組み合わせを使用する必要がありました。おそらく最良の解決策ではありませんが、完全に機能します。

于 2012-11-30T08:53:18.760 に答える