1

私は最も単純なことをすることができません:-)(私はそれが単純であるべきだと思います)

ボタンをクリックすると、リストビューでアイテムを強調表示しようとします。クリックしても何も起こりません:-(

次のコードで私が間違っていることを誰かが見ていますか、または誰かがヒントを持っていますか?

アクティビティを開始すると、リストビューが表示され、3 番目の項目も強調表示されます (int isymbolindex=2;)。しかし、onClick または onItemClick で選択を変更しようとしても、うまくいきません。デバッグ時には「public View getView」メソッドと isymbolindex=3 を経由しますが、gui (listview ) は更新されません。

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ExpandableListActivity;
    import android.content.Context;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ExpandableListView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class CityEditorActivity extends Activity {

    int isymbolindex=2;     
    Context croot=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_editor);
        final ListView lvsymbols= (ListView)findViewById(R.id.listViewdymbols);


        String[] values = new String[] { "Value1", "Value2", "Value3", "Value4"};

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }

        lvsymbols.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvsymbols.setSelector(android.R.color.darker_gray);


        Button bvon1=(Button)findViewById(R.id.vonadd1); 
        bvon1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                isymbolindex=3;
                lvsymbols.setSelection(3);
                lvsymbols.invalidate();
            }
        });




        lvsymbols.setAdapter(   new ArrayAdapter(this, R.layout.own_simple_list_item, list)
        {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final View renderer = super.getView(position, convertView, parent);
                if (position == isymbolindex)
                {
                    //TODO: set the proper selection color here:
                    renderer.setBackgroundResource(android.R.color.darker_gray);
                }else{
                    renderer.setBackgroundResource(android.R.color.white);
                }
                return renderer;
            }

        });

        lvsymbols.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long arg3) {
                isymbolindex=position;
                arg0.setSelection(isymbolindex);

                view.getFocusables(position);
                view.setSelected(true);
                lvsymbols.invalidate();
            }
        });


    }


}
4

2 に答える 2

1

You can try requestFocusFromTouch() before calling setSelection() method

or

maybe.

ListView.setItemChecked(int position, boolean checked);
于 2013-09-06T08:15:41.083 に答える
0

これも試すことができます

リストビューがあるメインレイアウトでこれを追加します

android:background="?android:attr/activatedBackgroundIndicator"

セレクターを作成する

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true">
<shape>
    <solid android:color="#ffffff" />
</shape>

あなたの活動で

listView.setSelector(R.drawable.listview_item_selection_effect);
listView.setItemChecked(3, true);


onClick of button call this setItemChecked and pass the postion of the item
于 2013-09-06T08:27:25.210 に答える