本文介绍了ListView控件setChoiceMode没有显示出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个放在一起使用一个特殊的适配器,这样我可以显示不同的颜色每隔ListView的一个ListView。我怎样写我的code,这样我能看到一个清单,而无需扩展ListActivity(我的活动只延伸活动)这是我的code。

 公共类TrackingMe延伸活动{

@覆盖
保护无效的onCreate(包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);

    共享preferences preFS = getShared preferences(设置,0);
    最后字符串ID = prefs.getString(ID,);

    最后的TextView myTitleText =(TextView中)findViewById(R.id.tvTitle3);
    如果(myTitleText!= NULL)
        myTitleText.setText(getResources()的getString(R.string.Trackingme));

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

        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            意图addFriends =新的意向书(com.cellphone.INVITEFOLLOWER);
            startActivity(addFriends);
        }
    });

    最终的ListView LV2 =(ListView控件)findViewById(R.id.trackingmelistview);
    lv2.setChoiceMode(2);
    lv2.setTextFilterEnabled(真正的);
    refreshList(ID,LV2);

    的ImageButton刷新=(的ImageButton)findViewById(R.id.btnRefresh2);
    refresh.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根

            refreshList(ID,LV2);

        }
    });
}

静态最后的ArrayList< HashMap的<字符串,字符串>> list2中=新的ArrayList< HashMap的<字符串,字符串>>();

私人无效refreshList(字符型,ListView控件LV){
    //删除列表并重建它会选择不同的反应
    //字符串来获得更新的时间。
    DefaultHttpClient的HttpClient =新DefaultHttpClient();
    HttpPost httpost =新HttpPost(
            http://iphone-radar.com/people_i_follow);

    JSONObject的持有人=新的JSONObject();
    尝试 {
        holder.put(用户id,身份证);
        现在//太平洋时区

        StringEntity本身=新StringEntity(holder.toString());
        httpost.setEntity(SE);
        httpost.setHeader(接受,应用/ JSON);
        httpost.setHeader(内容型,应用/ JSON);

        ResponseHandler的ResponseHandler的=新BasicResponseHandler();
        最后弦乐响应=的HttpClient
                .execute(httpost,ResponseHandler所);
        list2.removeAll(list2中);


        SpecialAdapter适配器=新SpecialAdapter(这一点,list2中,
                R.layout.trackingme_row_layout,新的String [] {姓名},新的INT [] {R.id.tvTrackingMeNames});


        org.json.JSONObject OBJ =新org.json.JSONObject(响应);
        JSONArray tracking_users = obj.getJSONArray(D);

        的for(int i = 0; I< tracking_users.length();我++){
            // for循环获取所有数据
            HashMap的<字符串,字符串>临时=新的HashMap<字符串,字符串>();
            的JSONObject用户= tracking_users.getJSONObject(ⅰ);
            temp.put(姓名,user.getString(FULL_NAME));
            list2.add(临时);
            //上传位置时

        }

        lv.setAdapter(适配器);
    }赶上(JSONException E1){
        // TODO自动生成的catch块
        e1.printStackTrace();
    }赶上(UnsupportedEncodingException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(ClientProtocolException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

}

}
    //这里是特殊的适配器,以便everyother线是不同的颜色
    公共类SpecialAdapter扩展SimpleAdapter {
私人INT []颜色=新INT [] {Color.GRAY,Color.WHITE};

公共SpecialAdapter(上下文的背景下,
        ArrayList的< HashMap的<字符串,字符串>>列表,INT资源,
        的String []从,INT []键){
    超(背景下,列表,资源,从,到);

}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    查看查看= super.getView(位置,convertView,父母);
    INT colorPos =位置%colors.length;
    view.setBackgroundColor(颜色[colorPos]);
    返回查看;
}
  }
 

到目前为止,它显示为文本的依据是什么,我从我的源得到的线。然而,当我点击单个行,他们不突出,即使我写lv.setChoiceMode(2),有旁边的列表视图没有复选框。谢谢

编辑这里是我的trackingme_row_layout.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的TextView
        机器人:ID =@ + ID / tvTrackingMeNames
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真
        机器人:layout_marginLeft =31dp
        机器人:文本=TextView的
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=#000000/>

< / RelativeLayout的>
 

和trackingme_layout.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的ListView
        机器人:ID =@ + ID / trackingmelistview
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:paddingTop =10dp/>

    <的ImageButton
        机器人:ID =@ + ID / btnRefresh2
        机器人:文本=刷新
        机器人:SRC =@机器人:可绘制/ stat_notify_sync
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentLeft =真/>

< / RelativeLayout的>
 

解决方案

调用 setChoiceMode 是不够的,以显示你的列表行旁边的复选框。如果您使用的是基本布局行,试试 android.R.layout.simple_list_item_multiple_choice 。否则,你将有一个复选框添加到您的排布置和放大器;管理其ON / OFF状态在自己的适配器 getView 方法。

心连心,

阿克沙伊

PS:拨打 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)而不是 setChoiceMode(2)

I have a ListView that is put together using a special adapter so that I can show every other listview in a different color. How do I write my code so that I am able to see a checklist without having to extend ListActivity (my activity only extends Activity) Here is my code.

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

So far it shows up as a line of text based on what I get from my source. However, when I click on individual lines, they don't highlight and even though I wrote lv.setChoiceMode(2), there was no checkbox next to the listview. Thanks

EDIT here is my trackingme_row_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>

and 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>
解决方案

Calling setChoiceMode is not enough to display checkboxes beside your list rows. If you are using a basic layout for the rows, try android.R.layout.simple_list_item_multiple_choice. Else, you will have to add a checkbox to your row layout & manage its on/off state yourself in the adapter's getView method.

HTH,

Akshay

P.S: call setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) instead of setChoiceMode(2) .

这篇关于ListView控件setChoiceMode没有显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 22:42