简体   繁体   English

使ListView中的复选框可见/不可见

[英]Make CheckBox in ListView visible/invisible

Hey guys i am building an app that has a listview in its MainActivity. 大家好,我正在构建一个在MainActivity中具有列表视图的应用程序。 I have a checkbox on every list item but in the beggining i want it to be invisible and then when the user clicks a button in the actionbar the visibility of the checkbox to be visible. 我在每个列表项上都有一个复选框,但是在开始时我希望它不可见,然后当用户单击操作栏中的按钮时,该复选框的可见性将可见。 i have done this but the visibility appears only in the first item. 我已经做到了,但是可见性仅出现在第一项中。 how is it possible to make it happen to all items? 如何使它发生在所有物品上?

My code is 我的代码是

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

And this is where the list is being created: 这是创建列表的地方:

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

Any help will be much appreciated and accepted. 任何帮助将不胜感激和接受。 Thanks in advance! 提前致谢!

You will have to do it in your adapter instead of the activity class. 您将必须在适配器而不是活动类中执行此操作。 In the getView method of your SimpleAdapter , trap the click event and set a boolean variable to true. SimpleAdaptergetView方法中,捕获click事件并将一个布尔变量设置为true。 In the getView method, if the boolean variable is set, show the checkboxes. 在getView方法中,如果设置了布尔变量,则显示复选框。 If it is unset, hide the checkbox. 如果未设置,请隐藏该复选框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM