Android ListView设置选项模式未显示

Android ListView设置选项模式未显示,android,listview,checkbox,Android,Listview,Checkbox,我有一个ListView,它使用一个特殊的适配器组合在一起,这样我就可以用不同的颜色显示其他每个ListView。如何编写代码,以便在不必扩展ListActivity(我的活动仅扩展活动)的情况下查看清单这是我的代码 public class TrackingMe extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS

我有一个ListView,它使用一个特殊的适配器组合在一起,这样我就可以用不同的颜色显示其他每个ListView。如何编写代码,以便在不必扩展ListActivity(我的活动仅扩展活动)的情况下查看清单这是我的代码

 public class TrackingMe extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.trackingme_layout);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.custom_title_3);

    SharedPreferences prefs = getSharedPreferences("Settings", 0);
    final String id = prefs.getString("ID", "");

    final TextView myTitleText = (TextView) findViewById(R.id.tvTitle3);
    if (myTitleText != null)
        myTitleText.setText(getResources().getString(R.string.Trackingme));

    ImageButton addFriends = (ImageButton) findViewById(R.id.btnAddFriends);
    addFriends.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent addFriends = new Intent("com.cellphone.INVITEFOLLOWER");
            startActivity(addFriends);
        }
    });

    final ListView lv2 = (ListView) findViewById(R.id.trackingmelistview);
    lv2.setChoiceMode(2);
    lv2.setTextFilterEnabled(true);
    refreshList(id,lv2);

    ImageButton refresh = (ImageButton) findViewById(R.id.btnRefresh2);
    refresh.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            refreshList(id, lv2);

        }
    });
}

static final ArrayList<HashMap<String, String>> list2 = 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.
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpost = new HttpPost(
            "http://iphone-radar.com/people_i_follow");

    JSONObject holder = new JSONObject();
    try {
        holder.put("userid", id);
        // pacific time zone for now

        StringEntity se = new StringEntity(holder.toString());
        httpost.setEntity(se);
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");

        ResponseHandler responseHandler = new BasicResponseHandler();
        final String response = httpclient
                .execute(httpost, responseHandler);
        list2.removeAll(list2);


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


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

        for (int i = 0; i < tracking_users.length(); i++) {
            // for loop to get all data
            HashMap<String, String> temp = new HashMap<String, String>();
            JSONObject user = tracking_users.getJSONObject(i);
            temp.put("name", user.getString("full_name"));
            list2.add(temp);
            // upload location time

        }

        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();
    }

}

}
    //here is the special adapter so that everyother line is a different color
    public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { Color.GRAY, Color.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.setBackgroundColor(colors[colorPos]);
    return view;
}
  }
公共类TrackingMe扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE\u自定义\u标题);
setContentView(R.layout.trackingme_布局);
getWindow().setFeatureInt(Window.FEATURE\u自定义\u标题,
R.布局。自定义标题(3);
SharedReferences prefs=GetSharedReferences(“设置”,0);
final String id=prefs.getString(“id”,”);
最终文本视图myTitleText=(文本视图)findViewById(R.id.tVTTitle3);
如果(myTitleText!=null)
myTitleText.setText(getResources().getString(R.string.Trackingme));
ImageButton addFriends=(ImageButton)findViewById(R.id.btnAddFriends);
addFriends.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
Intent addFriends=newintent(“com.mobile.INVITEFOLLOWER”);
星触觉(addFriends);
}
});
最终ListView lv2=(ListView)findViewById(R.id.trackingmelistview);
lv2.setChoiceMode(2);
lv2.setTextFilterEnabled(true);
刷新列表(id,2级);
ImageButton刷新=(ImageButton)findViewById(R.id.btnRefresh2);
refresh.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
刷新列表(id,2级);
}
});
}
静态最终ArrayList list2=新ArrayList();
私有无效刷新列表(字符串id,ListView lv){
//删除列表并重建它将选择不同的响应
//字符串以获取刷新时间。
DefaultHttpClient httpclient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://iphone-radar.com/people_i_follow");
JSONObject holder=新的JSONObject();
试一试{
持有者。put(“用户id”,id);
//现在是太平洋时区
StringEntity se=新的StringEntity(holder.toString());
httpost.setEntity(se);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
ResponseHandler ResponseHandler=新BasicResponseHandler();
最终字符串响应=httpclient
.execute(httpost、responseHandler);
清单2.移除所有(清单2);
SpecialAdapter=新的SpecialAdapter(此,列表2,
R.layout.trackingme_row_layout,新字符串[]{“name”},新int[]{R.id.tvTrackingMeNames});
org.json.JSONObject obj=new org.json.JSONObject(响应);
JSONArray tracking_users=obj.getJSONArray(“d”);
对于(int i=0;i
到目前为止,它显示为一行基于我从源代码中获得的文本。但是,当我单击单个行时,它们不会高亮显示,即使我编写了lv.setChoiceMode(2),listview旁边也没有复选框。谢谢

编辑这里是我的trackingme\u row\u layout.xml

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

    <TextView 
        android:id="@+id/tvTrackingMeNames"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="31dp"              
        android:text="TextView"  
        android:textAppearance="?android:attr/textAppearanceMedium"  
        android:textColor="#000000"/>

</RelativeLayout>

和trackingme_layout.xml

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

    <ListView   
        android:id="@+id/trackingmelistview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="10dp"/>

    <ImageButton 
        android:id="@+id/btnRefresh2" 
        android:text="Refresh" 
        android:src="@android:drawable/stat_notify_sync" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true"/>

</RelativeLayout>

调用
setChoiceMode
不足以在列表行旁边显示复选框。如果对行使用基本布局,请尝试使用android.R.layout.simple\u list\u item\u多选。否则,您必须在行布局中添加一个复选框,并在适配器的
getView
方法中自己管理其开/关状态

嗯,

阿克什


提示:调用
setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE)
而不是
setChoiceMode(2)

简而言之,ListView不能很好地处理布局

解决问题的简单方法是删除outter布局并使用复选框或CheckedTextView

下面是较长的答案(马丁的答案是最好的)。

最好使用预定义的常量。
lst.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE)查看布局。跟踪我的行布局将有助于理解。请在您的问题中发布该xml布局。我发布了xml布局感谢您的帮助嗨,我用setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)替换了setChoiceMode(2),但没有任何更改。CHOICE_MODE_MULTIPLE只是一个建议,它不会改变任何东西。