2016-05-29 5 views
1

学生の出席を取るアプリを開発していますが、 "TakeAttendance"でチェックした名前のリストを "ViewAttendance"同じカスタムリストビュー。どうすればいいですか?ここカスタムリストビュー内の選択したアイテムを同じカスタムリストビュー内の別のアクティビティに取得する方法

は私のコードです... カスタムビュー enter code here

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tv_firstname" 
    android:text="Firstname" 
    android:textSize="11dp" 
    android:ellipsize="start" 
    android:lines="3" 
    android:textColor="#000" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:paddingLeft="10dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Lastname" 
    android:lines="3" 
    android:textSize="11dp" 
    android:textColor="#000" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:paddingLeft="10dp" 
    android:id="@+id/tv_lastname" /> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tv_surname" 
    android:text="Surname" 
    android:lines="3" 
    android:textSize="11sp" 
    android:textColor="#000" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:paddingRight="80dp" 
    android:paddingLeft="10dp" /> 

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingStart="20dp" 
    android:id="@+id/checkBox" 
    android:checked="false" 
    android:focusable="false" 
    android:background="@color/white" /> 





The Listview 

enter code here

ListAdapter enter code hereパブリッククラスStudentListAdapter1はBaseAdapter { プライベートコンテキストmContextを拡張。 プライベートリストmStudentList;

//Constructor 

public StudentListAdapter1(Context mContext, List<StudentList> mStudentList) { 
    this.mContext = mContext; 
    this.mStudentList = mStudentList; 
} 

@Override 
public int getCount() { 
    return mStudentList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return mStudentList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = View.inflate(mContext, R.layout.student_take, null); 
    TextView tvReg_no = (TextView) v.findViewById(R.id.tv_reg_no); 
    TextView tvFirstname = (TextView) v.findViewById(R.id.tv_firstname); 
    TextView tvLastname = (TextView) v.findViewById(R.id.tv_lastname); 
    TextView tvSurname = (TextView) v.findViewById(R.id.tv_surname); 
    //Set text for TextView 
    tvReg_no.setText(mStudentList.get(position).getReg_no()); 
    tvFirstname.setText(mStudentList.get(position).getFirstname()); 
    tvLastname.setText(mStudentList.get(position).getLasttname()); 
    tvSurname.setText(mStudentList.get(position).getSurname()); 

    //Save product id to tag 
    v.setTag(mStudentList.get(position).getId()); 

    return v; 

} 

}

出席アクティビティ enter code here保護ボイドのonCreate(バンドルsavedInstanceState){ super.onCreate(savedInstanceState)。 setContentView(R.layout.activity_student_take100);

lvStudentlist= (ListView) findViewById(R.id.listview_studentlist); 

    mStudentList = new ArrayList<>(); 
    //Add sample data for list 
    //We can get data from DB, webservice here 
    mStudentList.add(new StudentList(1, "U11EE1001", "Bargo","S.","Mayafi")); 
    mStudentList.add(new StudentList(2, "U11EE1002", "Barnsbas","Snake.","Maciji")); 
    mStudentList.add(new StudentList(3, "U11EE1004", "Adamu","Tanko.","Sadau")); 
    mStudentList.add(new StudentList(4, "U11EE1005", "Munzali","","Cire Tallafi")); 


    //Init adapter 
    adapter = new StudentListAdapter1(getApplicationContext(), mStudentList); 
    lvStudentlist.setAdapter(adapter); 
    checkBox = (CheckBox) findViewById(R.id.checkBox); 

    lvStudentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      //Do something 
      SparseBooleanArray checked = lvStudentlist.getCheckedItemPositions(); 
      ArrayList<String> selectedItems = new ArrayList<>(); 

      for (int i = 0; i < checked.size(); i++) { 

       // Item position in adapter 

       position = checked.keyAt(i); 

       // Add names if it is checked i.e.) == TRUE! 

       if (checked.valueAt(i)) 

        selectedItems.add((String) adapter.getItem(position)); 

      } 

      String[] outputStrArr = new String[selectedItems.size()]; 

      for (int i = 0; i < selectedItems.size(); i++) { 

       outputStrArr[i] = selectedItems.get(i); 
      } 

      Intent intent = new Intent(getApplicationContext(), 

        ViewAttendanceActivity.class); 

      // Create a bundle object 

      Bundle b = new Bundle(); 

      b.putStringArray("selectedItems", outputStrArr); 

      // Add the bundle to the intent. 

      intent.putExtras(b); 

      // start the ResultActivity 
      startActivity(intent); 
     } 
    }); 


} 

}

表示出席アクティビティ enter code here保護ボイドのonCreate(バンドルsavedInstanceState){ super.onCreate(savedInstanceState)。 setContentView(R.layout.activity_view_attendance);まず

Bundle b = getIntent().getExtras(); 
    String[] resultArr = b.getStringArray("selectedItems"); 
    lvStudentlist= (ListView) findViewById(R.id.listview_studentlist); 


    adapter = new StudentListAdapter(getApplicationContext(), mStudentList); 
    lvStudentlist.setAdapter(adapter); 

} 

}

答えて

0

私はあなたが今、あなたの質問にはお答えリストビュー

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v; 
    if (convertView != null) { 
     v = convertView; 
    } else { 
     v = View.inflate(mContext, R.layout.student_take, null); 
    } 
....... 

であなたのアイテムを再利用すべきだと思う、私はあなたがlvStudentlist.setOnItemClickListener方法で配列にあなたの選択した項目を追加する必要があると思いますあなたの次のアクティビティにインテントとして余分に送信します。 String []をIntentとして余分に送ることができるので、Bundleを使用する必要はありません。

+0

私は理解していません。簡単な方法です。 –

+0

このようなものを使用してください 意図intent =新しい意図(CurrentActivity.this、NextActivity.class);intent.putExtra( "selectedItems"、outputStrArr); startActivity(インテント); –

+0

申し訳ありませんが、このアンドロイドアプリの初心者です....どのように私は、ViewAttendanceActivityで余分なものを取得し、それをリストビューに入れることができます。 –

関連する問題