从活动调用ListView项窗口小部件而不调用onItemClickListener。

时间:2014-11-25 22:37:43

标签: android listview android-listview listviewitem

如果没有来自活动的onItemClickListener,我试图将listview中的项设置为可见或不可见。如何从活动中的列表视图中调用窗口小部件(复选框)? *不只是一个列表项,我想设置所有项目的可见性。

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

适配器

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

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

我得到了一个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 个答案:

答案 0 :(得分:0)

我是Android编程新手。我不确定这是否会奏效。

您可以尝试将复选框添加到Checkbox的ArrayList。

然后将可见性设置为您需要的内容。

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

这不是一个完整的解决方案,而是一个简单的方法。 添加复选框时,将所有复选框添加到列表中。 然后,您可以设置所需复选框的可见性。