Calling the ListView items widgets from the activity without calling onItemClickListener.

86 Views Asked by At

I am trying to set the items within the listview to be visible or not visible without onItemClickListener from the activity. How could call the widget (checkbox) from the listview within the activity? *not just a single list item, I want to set the visibility in all of the items.

MainActivity

LogsArrayAdapterBreakfast logsAdapter1 = new LogsArrayAdapterBreakfast(
        this, 4, LogBreakfast.logsByDate(currentDate));
ListView listBreakfast = (ListView) findViewById(R.id.listViewBreakfast);
listBreakfast.setAdapter(logsAdapter1);
View vv = (View) listBreakfast.getParent();
CheckBox cb = (CheckBox) vv.findViewById(R.id.cbDelete);
cb.setVisibility(View.VISIBLE);

Adapter

public class LogsArrayAdapterBreakfast extends ArrayAdapter<LogBreakfast> {
    private static LogsArrayAdapterBreakfast instance;
    Context mContext;
    public static List<LogBreakfast> mLogs;
    public LogsArrayAdapterBreakfast(Context context, int textViewResourceId,
            List<LogBreakfast> logs) {
        super(context, textViewResourceId);
        mContext = context;
        mLogs = logs;
    }
    public void setLogs(List<LogBreakfast> logs) {
        mLogs = logs;
    }
    public List<LogBreakfast> getLogs() {
        return mLogs;
    }
    public void add(LogBreakfast log) {
        mLogs.add(log);
    }
    public void remove(LogBreakfast log) {
        LogsArrayAdapterBreakfast.mLogs.remove(log);
    }
    public static LogsArrayAdapterBreakfast getInstance(Context mContext) {
        if (instance == null) {
            instance = new LogsArrayAdapterBreakfast(
                    mContext.getApplicationContext(), 4, mLogs);
        }
        return instance;
    }
    /**
     * returns the number of logs that will appear in the listview
     * 
     * @return [int] number of logs
     */
    public int getCount() {
        return mLogs.size();
    }
    /**
     * returns the log found at the index of the position parameter
     * 
     * @return [Log]
     */
    public LogBreakfast getItem(int position) {

        return mLogs.get(position);
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        LogRowBreakfast view = (LogRowBreakfast) convertView;
        if (view == null) {
            view = new LogRowBreakfast(mContext);
        }
        LogBreakfast log = getItem(position);
        view.setLog(log);
        return view;
    }
    public boolean isItemChecked(int i) {
        // TODO Auto-generated method stub
        return false;
    }
}

Row

public class LogRowBreakfast extends LinearLayout {
    Context mContext;
    LogBreakfast mLog;
    public LogRowBreakfast(Context context) {
        super(context);
        mContext = context;
        setup();
    }
    public LogRowBreakfast(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setup();
    }
    private void setup() {
        LayoutInflater inflater1 = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater1.inflate(R.layout.log_row_breakfast, this);
    }
    public void setLog(LogBreakfast log) {
        mLog = log;
        TextView tvName1 = (TextView) findViewById(R.id.log_meal_name1);
        tvName1.setText(mLog.getMealName() + "");

        TextView tvCalories1 = (TextView) findViewById(R.id.log_calories1);
        tvCalories1.setText(mLog.getCalorieCount() + " Cals");
    }
}

I am getting a nullpointerexception.

11-25 17:22:17.452: W/dalvikvm(24573): threadid=1: thread exiting with uncaught exception (group=0x4178ada0)
11-25 17:22:17.452: E/AndroidRuntime(24573): FATAL EXCEPTION: main
11-25 17:22:17.452: E/AndroidRuntime(24573): Process: com.theworkoutcalculator, PID: 24573
11-25 17:22:17.452: E/AndroidRuntime(24573): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.theworkoutcalculator/com.theworkoutcalculator.Activities.CaloriesLogMainActivity}: java.lang.NullPointerException
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.os.Looper.loop(Looper.java:157)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.ActivityThread.main(ActivityThread.java:5356)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at java.lang.reflect.Method.invoke(Method.java:515)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at dalvik.system.NativeStart.main(Native Method)
11-25 17:22:17.452: E/AndroidRuntime(24573): Caused by: java.lang.NullPointerException
11-25 17:22:17.452: E/AndroidRuntime(24573):    at com.theworkoutcalculator.Activities.CaloriesLogMainActivity.onCreate(CaloriesLogMainActivity.java:673
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.Activity.performCreate(Activity.java:5426)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-25 17:22:17.452: E/AndroidRuntime(24573):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
11-25 17:22:17.452: E/AndroidRuntime(24573):    ... 11 more
1

There are 1 best solutions below

0
On

I am new to Android Programming. I am not sure this will work.

You could maybe try adding the checkbox to a ArrayList of Checkboxes.

Then setting the visibility to what you need.

ArrayList<CheckBox> checkboxlist = new ArrayList<CheckBox>();
 checkboxlist.add(cb);

This is not a complete solution but a simple approach to one. Add all the checkboxes to a list when you add a checkbox. Then you can set the visibility of what checkboxes you need.