选中的文本视图不检查

时间:2011-08-17 03:49:06

标签: android listview

我创建了一个自定义列表视图,该视图显示了一个名称,旁边有一个复选框。我决定不使用复选框,而是使用选中的文本视图。问题是当我点击该行时,检查不会亮起。我想知道如何解决这个问题。这是我的列表视图的布局,每个元素都有一个带有不同文本的选中文本视图。我还需要一种滚动列表的方法,取消选中哪个元素应该使其文本显示为灰色。 感谢

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

    <CheckedTextView android:text="CheckedTextView"
        android:textColor="#000000" android:id="@+id/checkedTextView1" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:checkMark="?android:attr/textCheckMark" android:paddingLeft="6dip"
        android:paddingRight="6dip"></CheckedTextView>
</LinearLayout>

编辑这是我的列表视图通过的代码

 static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

private void refreshList(String id, ListView lv) {
    // removes the list and rebuilds it will choose different response
    // string to get the refreshed times.

    try {
        HttpClient client1 = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://iphone-radar.com/accounts/"+id);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String response = client1.execute(request, responseHandler);



        list.removeAll(list);
        SpecialAdapter adapter = new SpecialAdapter(this, list,
                R.layout.trackingme_row_layout, new String[] { "name" },
                new int[] { R.id.checkedTextView1 });

        org.json.JSONObject obj = new org.json.JSONObject(response);
        JSONArray tracking_users = obj.getJSONArray("d");

        for (int i = 0; i < tracking_users.length(); i++) {

            HashMap<String, String> temp = new HashMap<String, String>();
            JSONObject user = tracking_users.getJSONObject(i);
            temp.put("name", user.getString("full_name"));
            // upload location time

            list.add(temp);

        }
        lv.setAdapter(adapter);

    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

这是它经过的特殊适配器,因此每隔一行都是白色或灰色。

 public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[]{R.drawable.row_background_grey,       R.drawable.row_background_white};

public SpecialAdapter(Context context,
        ArrayList<HashMap<String, String>> list, int resource,
        String[] from, int[] to) {
    super(context, list, resource, from, to);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    int colorPos = position % colors.length;
    view.setBackgroundResource(colors[colorPos]);
    return view;
}

}

2 个答案:

答案 0 :(得分:2)

对于CompoundButton,

performClick方法不会覆盖CheckedTextView。 所以遵循以下代码:


        final CheckedTextView checkedTextView = (CheckedTextView)findViewById(R.id.checkedTextView1);
        checkedTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                checkedTextView.toggle();
            }
        });

答案 1 :(得分:2)

根据我的要求,您希望列表中有多个CheckedTextView。

下面你会找到一种方法来解决这个问题。由于ListView重用了视图,我们需要一种方法来跟踪不同的CheckedTextView,因此使用HashMaps。另一种方法是只跟踪状态,并使用OnItemClickListener更改状态,然后更新整个列表。

public class CheckedTextViewExample extends ListActivity implements OnClickListener {   

    // store CheckTextView's
    private HashMap<Integer, CheckedTextView> mCheckedList = 
            new HashMap<Integer, CheckedTextView>();
    // store state
    private HashMap<Integer, Boolean> mIsChecked = 
            new HashMap<Integer, Boolean>();
    // the list objects
    private ArrayList<MyObject> mList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mList = new ArrayList<MyObject>();

        MyObject one = new MyObject();
        one.setName("One");
        ...
        MyObject thirteen = new MyObject();
        thirteen.setName("thirteen");
        mList.add(one);
        ...
        mList.add(thirteen);        

        setListAdapter(new MyAdapter(this, R.layout.row, mList));        
    }

    @Override
    public void onClick(View v) {
        // get the CheckedTextView
        CheckedTextView ct = mCheckedList.get(v.getTag());
        if (ct != null) {
            // change the state and colors
            ct.toggle();
            if (ct.isChecked()) {
                ct.setTextColor(Color.WHITE);
            } else {
                ct.setTextColor(Color.GRAY);
            }
            // add current state to map
            mIsChecked.put((Integer) v.getTag(), ct.isChecked());
        }       
    }

    private class MyAdapter extends ArrayAdapter<MyObject> {

        private ArrayList<MyObject> items;

        public MyAdapter(Context context, int textViewResourceId, ArrayList<MyObject> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        public View getView(final int position, View view, ViewGroup parent) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.row, null);

            CheckedTextView ct = (CheckedTextView) view.findViewById(R.id.checkedTextView);
            ct.setText(items.get(position).getName());
            if (mIsChecked.get(position) != null) {
                if (mIsChecked.get(position)) {
                    ct.setChecked(true);
                    ct.setTextColor(Color.WHITE);
                } else {
                    ct.setTextColor(Color.GRAY);
                }
            }
            // tag it
            ct.setTag(position);
            mCheckedList.put(position, ct);
            ct.setOnClickListener(CheckedTextViewExample.this);

            return view;
        }
    }

    private class MyObject {
        private String name;    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
}

我在我的blog

上发布了eclipse项目