嘿伙计们我正在构建一个在其MainActivity中具有列表视图的应用程序。我在每个列表项上都有一个复选框,但在开始时我希望它不可见,然后当用户单击操作栏中的按钮时,复选框的可见性将可见。我已经这样做但可见性仅出现在第一项中。怎么可能让它发生在所有物品上?
我的代码是
List<HashMap<String, String>> stocksList;
ArrayList<String> imageUrl;
Bitmap bmp;
public CheckBox dontShowAgain;
public Configuration newConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean network_connected = activeNetwork != null
&& activeNetwork.isAvailable() && activeNetwork.isConnectedOrConnecting();
if (!network_connected) {
onDetectNetworkState().show();
} else {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){
accessWebService();
registerCallClickBack();
}
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String skipMessage = settings.getString("skipMessage", "NOT checked");
if (!skipMessage.equals("checked")){
onAlertMobileData().show();
}
if(skipMessage.equals("checked")){
accessWebService();
registerCallClickBack();
}
}
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.Share:{
accessWebService();
registerCallClickBack();
return true;
}
case R.id.About:{
onAboutPressed().show();
return true;
}
case R.id.item1:{
CheckBox chk = (CheckBox)findViewById(R.id.checkBoxMainList);
chk.setVisibility(CheckBox.VISIBLE);
}
default:{
return false;
}
}
}
这就是创建列表的地方:
public void ListDrawer() {
stocksList = new ArrayList<HashMap<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
String price = jsonChildNode.optString("price");
String price1 = jsonChildNode.optString("price1");
String price2 = jsonChildNode.optString("price2");
String price3 = jsonChildNode.optString("price3");
String price4 = jsonChildNode.optString("price4");
String price5 = jsonChildNode.optString("price5");
String price6 = jsonChildNode.optString("price6");
String price7 = jsonChildNode.optString("price7");
String price8 = jsonChildNode.optString("price8");
String price9 = jsonChildNode.optString("price9");
String price10 = jsonChildNode.optString("price10");
String price11 = jsonChildNode.optString("price11");
String price12 = jsonChildNode.optString("price12");
String price13 = jsonChildNode.optString("price13");
String price14 = jsonChildNode.optString("price14");
String price15 = jsonChildNode.optString("price15");
stocksList.add(createStockList(name, price, price1, price2, price3, price4, price5, price6, price7, price8, price9, price10, price11, price12, price13, price14, price15));
}
} catch (Exception e) {
//Toast.makeText(getApplicationContext(), "Error" + e.toString(),
//Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(MainActivity.this, RefreshActivity.class);
startActivityForResult(intent1, 0);
}
String[] from = { "name", "price"};
int[] to = { R.id.stock_name, R.id.stock_price};
SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
R.layout.list_item,
from, to);
listView.setAdapter(simpleAdapter);
}
public HashMap<String, String> createStockList(String name, String price, String price1, String price2, String price3, String price4, String price5, String price6, String price7, String price8, String price9, String price10, String price11, String price12, String price13, String price14, String price15) {
HashMap<String, String> stockNameNo = new HashMap<String, String>();
stockNameNo.put("name", name);
stockNameNo.put("price", price);
stockNameNo.put("price1", price1);
stockNameNo.put("price2", price2);
stockNameNo.put("price3", price3);
stockNameNo.put("price4", price4);
stockNameNo.put("price5", price5);
stockNameNo.put("price6", price6);
stockNameNo.put("price7", price7);
stockNameNo.put("price8", price8);
stockNameNo.put("price9", price9);
stockNameNo.put("price10", price10);
stockNameNo.put("price11", price11);
stockNameNo.put("price12", price12);
stockNameNo.put("price13", price13);
stockNameNo.put("price14", price14);
stockNameNo.put("price15", price15);
return stockNameNo;
}
任何帮助都将受到高度赞赏和接受。提前谢谢!
答案 0 :(得分:1)
您必须在适配器而不是活动类中执行此操作。在getView
的{{1}}方法中,捕获click事件并将布尔变量设置为true。在getView方法中,如果设置了boolean变量,则显示复选框。如果未设置,请隐藏复选框。