坚持使用ListViews和Adapters

时间:2014-07-15 01:07:56

标签: java android android-layout listview checkbox

好吧,我试图做一个占用几个字段的屏幕然后,它将信息发送到我的服务器。代码工作,我可以进行插入,但现在,我正在尝试使用我的应用程序的类组的复选框选择器来完成屏幕。

问题是,我在不同的课程中有不同数量的小组,所以我必须动态。

我工作的班级就是这个。意见之间包含的代码是失败的地方

 public class NuevaTarea extends Activity {

    EditText nombretarea;
    DatePicker fechainicio, fechafin;
    ExpandableListView listagrupos;
    Button crear;
    TimePicker horainicio, horafin;
    ProgressDialog pDialog;
    Httppostaux post;
    CheckBox op1, op2, op3, op4, op5;
    JSONArray Grupos;
    ArrayList<PostData> data;
    PostAdapter adapter;
    ListView lista;






    @SuppressWarnings("null")
    public void onCreate(Bundle savedInstanceState) {  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insertatarea);


        nombretarea = (EditText) findViewById(R.id.editText1);
        fechainicio = (DatePicker) findViewById(R.id.datePicker1);
        fechafin = (DatePicker) findViewById(R.id.datePicker2);
        listagrupos = (ExpandableListView) findViewById(R.id.expandableListView1);
        crear = (Button) findViewById(R.id.buttoncrear);
        horainicio = (TimePicker) findViewById(R.id.timePicker1);
        horafin = (TimePicker) findViewById(R.id.timePicker2);
        op1 = (CheckBox) findViewById(R.id.checkBox1);
        op2 = (CheckBox) findViewById(R.id.checkBox2);
        op3 = (CheckBox) findViewById(R.id.checkBox3);
        op4 = (CheckBox) findViewById(R.id.checkBox4);
        op5 = (CheckBox) findViewById(R.id.checkBox5);

        //////////////// Grupos


        Log.d("Grupos", "Previo Inicio");
        if ( savedInstanceState == null) {
            Log.d("Grupos", "SavedInstance == NULL");
            Log.d("Todos los Grupos", "ARRAY GRUPOS = " + Constantes.cargainfogrupos);
            //new asynccargagrupos().execute();
            Log.d("Todos los Grupos", "ARRAY GRUPOS = " + Constantes.cargainfogrupos);
            data = new ArrayList<PostData>();
            if ( Constantes.cargainfogrupos != null) {
                Log.d("Grupos", "Grupos != NULL");
                Log.d("Grupos", "Dentro del WHILE");
                    String tex;
                    try {
                        Log.d("Grupos", "Dentro del Try");
                        for ( int i = 0; i < Constantes.cargainfogrupos.length(); i++){
                            Log.d("Grupos", "Dentro del For");
                            JSONObject jsonObject = Constantes.cargainfogrupos.getJSONObject(i);
                            tex = jsonObject.getString("nombregrupo");
                            data.add(new PostData(tex, false));
                            Log.d("Grupos", "Primera Vista a data" + data);
                        }
                        Log.d("Grupos", "Version de Data que paso a Adapter" + data);
                        adapter = new PostAdapter(NuevaTarea.this, data);

                    }
                    catch (Exception e){

                    }

            }

        }else {
            Log.d("Grupos", "ELSE");
            data = savedInstanceState.getParcelableArrayList("saveddata");
            adapter = new PostAdapter(NuevaTarea.this, data);       
        }
        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setAdapter(adapter);

        /////////////////// GRUPOS

我的XML文件是

 <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nombre Tarea" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" >
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tipo de Tarea" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Examen Práctico Parcial" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Examen Práctico Final" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Examen Teórico Parcial" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exament Teórico Final" />

    <CheckBox
        android:id="@+id/checkBox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Entregable" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fecha de Inicio" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/FechaFinalizacion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TimePicker
        android:id="@+id/timePicker2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


   <DatePicker
        android:id="@+id/datePicker2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
   <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Grupo" />

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ExpandableListView>

    <!--  CHECKBOXs --> 

    <RelativeLayout 
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#999999"
    android:padding="2dp" >

    <CheckBox
        android:id="@+id/leido"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:focusable="false" />

    <TextView
        android:id="@+id/tvTitulo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/leido"
        android:text="Titulo del post"
        android:textAppearance="?android:attr/textAppearanceMedium" />

     <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>



    </RelativeLayout>



    <!--  CHECKBOXs --> 
    <Button
        android:id="@+id/buttoncrear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Crea Tarea" />



        </LinearLayout>


    </ScrollView>



</LinearLayout>  

我使用了两个外部类,PostData和PostAdapter

public class PostData implements Parcelable{

  private String titulo;
  private boolean leido;

  public PostData(String equip, boolean checked) {

      this.titulo = equip;
      this.leido = checked;

   }

   public PostData(Parcel in){
       this.titulo= in.readString();
       this.leido = in.readInt() == 1 ? true:false;
    }


   public void setChecked(boolean value) {
     this.leido = value;
 }

   public boolean getChecked() {
       return leido;
   }

   public String getEquipos() {
        return titulo;
  }

   public void setEquipos(String equipos) {
        this.titulo = equipos;
  }

   @Override
   public int describeContents() {
     return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
     dest.writeString(getEquipos());
     dest.writeInt(getChecked() ? 1 : 0);
    }

   public static final Parcelable.Creator<PostData> CREATOR = new Parcelable.Creator<PostData>() {
     public PostData createFromParcel(Parcel in) {
           return new PostData(in);
        }

       public PostData[] newArray(int size) {
          return new PostData[size];
      }
   };

这是我的PostAdapter类

public class PostAdapter extends BaseAdapter {

static class ViewHolder
 {
      TextView tvFecha_pub;
      TextView tvtitulo_post;
      CheckBox cb;
    }

   private static final String TAG = "CustomAdapter";
  private static int convertViewCounter = 0;

  private ArrayList<PostData> data;
   private LayoutInflater inflater = null;

 public PostAdapter(Context c, ArrayList<PostData> d)
    {
       Log.v(TAG, "Constructing CustomAdapter");

       this.data = d;
      inflater = LayoutInflater.from(c);
  }

   @Override
   public int getCount()
   {
       Log.v(TAG, "in getCount()");
        return data.size();
 }

   @Override
   public Object getItem(int position)
 {
       Log.v(TAG, "in getItem() for position " + position);
        return data.get(position);
  }

   @Override
   public long getItemId(int position)
 {
       Log.v(TAG, "in getItemId() for position " + position);
      return position;
    }

   @Override
   public int getViewTypeCount()
   {
       Log.v(TAG, "in getViewTypeCount()");
        return 1;
   }

   @Override
   public int getItemViewType(int position)
    {
       Log.v(TAG, "in getItemViewType() for position " + position);
        return 0;
   }

   @Override
   public void notifyDataSetChanged()
  {
       super.notifyDataSetChanged();
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {

       ViewHolder holder;

      Log.v(TAG, "in getView for position " + position + ", convertView is "
              + ((convertView == null) ? "null" : "being recycled"));

     if (convertView == null)
        {
           convertView = inflater.inflate(R.layout.activity_insertatarea, null);

          convertViewCounter++;
            Log.v(TAG, convertViewCounter + " convertViews have been created");

            holder = new ViewHolder();


            holder.tvtitulo_post = (TextView) convertView
                   .findViewById(R.id.tvTitulo);
            holder.cb = (CheckBox) convertView.findViewById(R.id.leido);
            holder.cb.setOnClickListener(checkListener);

            convertView.setTag(holder);

     } else
          holder = (ViewHolder) convertView.getTag();

     // Para porde hacer click en el checkbox
        PostData d = (PostData) getItem(position);
      holder.cb.setTag(d);
        // Setting all values in listview

      holder.tvtitulo_post.setText(data.get(position).getEquipos());
      holder.cb.setChecked(data.get(position).getChecked());

      return convertView;
 }

   public void setCheck(int position)
  {
       PostData d = data.get(position);

        d.setChecked(!d.getChecked());
      notifyDataSetChanged();
 }

   public void checkAll(boolean state)
 {
       for (int i = 0; i < data.size(); i++)
           data.get(i).setChecked(state);
  }

   public void cancelSelectedPost()
    {

       int i = 0;
      while (i < getCount())
      {
           if (data.get(i).getChecked())
           {
               data.remove(data.indexOf(data.get(i)));
         } else
              i++;
        }
       notifyDataSetChanged();

 }

   public boolean haveSomethingSelected()
  {
       for (int i = 0; i < data.size(); i++)
           if (data.get(i).getChecked())
               return true;
        return false;
   }

   /**
  * Este método es para poder seleccionar una fila directamente con el
    * checkbox en lugar de tener que pulsar en la liste en sí
   */
 private OnClickListener checkListener = new OnClickListener()
   {

       @Override
       public void onClick(View v)
     {
           PostData d = (PostData) v.getTag();
         d.setChecked(!d.getChecked());
      }
   };

}

我坚持使用此错误日志

 07-14 20:34:52.972: E/AndroidRuntime(5961): FATAL EXCEPTION: main
    07-14 20:34:52.972: E/AndroidRuntime(5961): java.lang.ClassCastException:    android.widget.LinearLayout cannot be cast to android.widget.CalendarView$WeekView
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.CalendarView.onScroll(CalendarView.java:1183)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.CalendarView.access$900(CalendarView.java:76)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.CalendarView$2.onScroll(CalendarView.java:1075)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.ListView.layoutChildren(ListView.java:1762)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.AbsListView.onLayout(AbsListView.java:2012)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.ScrollView.onLayout(ScrollView.java:1459)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.View.layout(View.java:14289)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.Choreographer.doFrame(Choreographer.java:532)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.os.Handler.handleCallback(Handler.java:730)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.os.Looper.loop(Looper.java:137)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at java.lang.reflect.Method.invoke(Method.java:525)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-14 20:34:52.972: E/AndroidRuntime(5961):     at dalvik.system.NativeStart.main(Native Method)
07-14 20:39:55.014: E/AndroidRuntime(5993): FATAL EXCEPTION: main
07-14 20:39:55.014: E/AndroidRuntime(5993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.PFC.pablo/com.PFC.pablo.Cursos}: java.lang.NullPointerException
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.os.Looper.loop(Looper.java:137)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at java.lang.reflect.Method.invoke(Method.java:525)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at dalvik.system.NativeStart.main(Native Method)
07-14 20:39:55.014: E/AndroidRuntime(5993): Caused by: java.lang.NullPointerException
07-14 20:39:55.014: E/AndroidRuntime(5993):     at com.PFC.pablo.Cursos.agregarFilasTabla(Cursos.java:113)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at com.PFC.pablo.Cursos.onCreate(Cursos.java:74)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.Activity.performCreate(Activity.java:5133)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-14 20:39:55.014: E/AndroidRuntime(5993):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-14 20:39:55.014: E/AndroidRuntime(5993):     ... 11 more

当我尝试加载Activity时,一切正常,甚至可以做得很好NuevaTarea.java循环获取数据,但那就是破坏了。具体来说,来到这里并从那里通过

    Log.d("Grupos", "Version de Data que paso a Adapter" + data);
                    adapter = new PostAdapter(NuevaTarea.this, data);

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

从堆栈跟踪看来,你正在向某个CalendarView投射一个LinearLayout是一个名为Cursos的类。

你可能有类似

的行
LinearLayout mLayout = (LinearLayout) findViewById(R.id.my_view);

但是具有id R.id.my_view的视图实际上是日历视图。