- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 ListView,它使用特殊的适配器组合在一起,这样我就可以用不同的颜色显示每个其他 ListView 。我该如何编写代码,以便无需扩展 ListActivity(我的 Activity 仅扩展 Activity)就可以看到 list 。这是我的代码。
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;
}
}
到目前为止,它根据我从源中获得的内容显示为一行文本。但是,当我单击个别行时,它们不会突出显示,即使我写了 lv.setChoiceMode(2), ListView 旁边也没有复选框。谢谢
编辑这里是我的 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>
和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_list_item_multiple_choice
。否则,您必须在行布局中添加一个复选框并在适配器的 getView
方法中自行管理其开/关状态。
HTH,
阿克谢
附注:调用 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)
而不是 setChoiceMode(2)
。
关于android - ListView setChoiceMode 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7074888/
我有一个 ListView,它使用特殊的适配器组合在一起,这样我就可以用不同的颜色显示每个其他 ListView 。我该如何编写代码,以便无需扩展 ListActivity(我的 Activity 仅
我有一个 ListView,它使用: setChoiceMode(ListView.CHOICE_MODE_SINGLE) 因此,当用户单击列表项时,它会显示为选中状态。这工作正常并自动选择项目。 注
我正在构建一个在 Froyo 上运行并以 Ice Cream 为目标的应用。 问题出在 GridView 的 choiceMode 上。 Froyo 似乎没有实现 setChoiceMode(在 Ab
我正在使用用于我的 ListView 的自定义适配器。创建ArrayList后 WifiListAdapter adapter = new WifiListAdapter( t
我正在实现 ActionMode.Callback 和一些 ListView 东西。我的问题是,当我处于选择模式 ListView.CHOICE_MODE_MULTIPLE 并且我检查了一些项目时,我
当您将 ADT 与 Eclipse 一起使用时,并告诉 ADT 使用主/从流程创建一个新 Activity ,此方法将出现在其中一个 fragment 中: /** * Turns on activ
我是一名优秀的程序员,十分优秀!