Question

Hey guys i am building an app that has a listview in its 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!

Was it helpful?

Solution

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. In the getView method, if the boolean variable is set, show the checkboxes. If it is unset, hide the checkbox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top