我正在尝试制作一个时间表,用户必须选择他们的主题,他们的主题将通过单击按钮(查看您的时间表)显示在另一个活动(ViewTimetable)中。
用户选择了他或她的主题后,用户将看到在同一活动中选择的内容(ChooseSubject),当他们点击"查看您的时间表"时,主题将列在ViewTimetable中.java根据"星期一,星期二,星期三,星期四,星期五和星期四的日子。
如果一天中没有主题,例如星期一,"星期一和#34;将不会显示。
但我不知道如何将数据从数据库检索到ListView中 请帮我! :(
ChooseSubject.java(我如何获得用户的选择)
public class ChooseSubject extends ActionBarActivity {
ArrayList<String> subjects = new ArrayList<String>();
SQLiteDatabase db;
String[] dbSubject = {
"INSERT INTO `subjects` VALUES (1,'TPRG343','Introduction to Android Programming')",
"INSERT INTO `subjects` VALUES (2,'TNWK343(L)','Network Security(Lectural)')",
"INSERT INTO `subjects` VALUES (3,'TNWK224(L)','System Administration')",
"INSERT INTO `subjects` VALUES (4,'TWEB334','Advanced Web Design')",
"INSERT INTO `subjects` VALUES (5,'TWEB224','Web Programming')",
"INSERT INTO `subjects` VALUES (6,'TNWK343(T)','Network Security(Tutorial)')",
"INSERT INTO `subjects` VALUES (7,'TMMD233','Multimedia Presentation')",
"INSERT INTO `subjects` VALUES (8,'TMMD113','Computer Graphics Editing')"
};
String[] dbTimetable = {
"INSERT INTO `timetable` VALUES (1,'Monday','8.30am - 11.30am','KKL','B106')",
"INSERT INTO `timetable` VALUES (2,'Monday','12.30pm - 2.30pm','GBS','B103/B302')",
"INSERT INTO `timetable` VALUES (3,'Monday','3.00pm - 6.00pm','GBS','B103/B302')",
"INSERT INTO `timetable` VALUES (3,'Friday','4.30pm - 6.00pm','GBS','B103/B302')",
"INSERT INTO `timetable` VALUES (4,'Tuesday','12.00pm - 1.30pm','KKL','B106')",
"INSERT INTO `timetable` VALUES (4,'Thrusday','10.00am - 1.00pm','KKL','B101')",
"INSERT INTO `timetable` VALUES (5,'Tuesday','2.00pm - 4.30pm','YKC','B101')",
"INSERT INTO `timetable` VALUES (5,'Friday','12.00pm - 2.30pm','YKC','B101')",
"INSERT INTO `timetable` VALUES (6,'Tuesday','4.30pm - 6.00pm','GBS','B103/B207')",
"INSERT INTO `timetable` VALUES (7,'Wednesday','9.00am - 12.00pm','YKC','B101')",
"INSERT INTO `timetable` VALUES (8,'Thrusday','1.30pm - 4.30pm','Widura','B101')"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_subject);
db = openOrCreateDatabase("subjects", MODE_PRIVATE, null);
db.execSQL("Create table if not exists subjects (id integer primary key, subCode text, subName text)");
Cursor cursor = db.rawQuery("select * from subjects", null);
//Access for the first time,
if (cursor.getCount() == 0) {
for (int i = 0; i < dbSubject.length; i++) {
db.execSQL(dbSubject[i]);
}
}
db.execSQL("Create table if not exists timetable (id integer, day text, time text, lecturer text, class text)");
Cursor ttCursor = db.rawQuery("select * from timetable", null);
//Access for the first time,
if (ttCursor.getCount() == 0) {
for (int i = 0; i < dbTimetable.length; i++) {
db.execSQL(dbTimetable[i]);
}
}
String sId, sCode, sName;
String output = "";
Cursor spCursor = db.rawQuery("select * from subjects", null);
Toast.makeText(this, Integer.toString(spCursor.getCount()), Toast.LENGTH_SHORT).show();
if (spCursor.getCount() > 0) {
while (spCursor.moveToNext()) {
//output = spCursor.getString(spCursor.getColumnIndex("subCode")) + " \n" + spCursor.getString(spCursor.getColumnIndex("subName"));
output = spCursor.getString(spCursor.getColumnIndex("subName"));
subjects.add(output);
}
//
}
final LinearLayout rl = (LinearLayout) findViewById(R.id.rl);
Button btn = (Button) findViewById(R.id.btn);
Button btnTt = (Button)findViewById(R.id.seeTt);
final TextView tv = (TextView) findViewById(R.id.tv);
btnTt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ChooseSubject.this,ViewTimetable.class);
startActivity(intent);
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Build an AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(ChooseSubject.this);
final boolean[] checkedSubjects = new boolean[subjects.size()];
for(int i = 0 ; i < subjects.size(); i++){
checkedSubjects[i] = false;
}
// Convert the array to list
//final List<String> subjects = new ArrayList<String>();
String[] subjectsOutput = new String[subjects.size()];
subjectsOutput = subjects.toArray(subjectsOutput);
final ArrayList<String> selectedItem = new ArrayList<String>();
//final List<String> subjectList = Arrays.asList(subjects);
builder.setMultiChoiceItems(subjectsOutput, checkedSubjects, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// Update the current focused item's checked status
checkedSubjects[which] = isChecked;
if(isChecked){
selectedItem.add(String.valueOf(which));
db.execSQL("Create table if not exists UserSubject (subName text)");
String query = "insert into UserSubject values ('"+ selectedItem + "');";
db.execSQL(query);
}
else {
selectedItem.remove(String.valueOf(which));
}
// Get the current focused item
String currentItem = subjects.get(which);
// Notify the current action
Toast.makeText(getApplicationContext(),
currentItem + " " , Toast.LENGTH_SHORT).show();
}
});
// Specify the dialog is not cancelable
builder.setCancelable(false);
// Set a title for alert dialog
builder.setTitle("Choose your subjects");
// Set the positive/yes button click listener
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click positive button
tv.setText("Your subjects are \n");
for (int i = 0; i < checkedSubjects.length; i++) {
boolean checked = checkedSubjects[i];
if (checked) {
tv.setText(tv.getText() + "\n" + subjects.get(i) + "\n");
}
}
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click the neutral button
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_choose_subject, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ChooseSubject.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose your subjects"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn"
android:textSize="20dp"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See Your Timetable"
android:id="@+id/seeTt"
android:layout_gravity="right" />
</LinearLayout>
ViewTabletable.java
import android.app.ActivityGroup;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import java.util.ArrayList;
public class ViewTimetable extends ActionBarActivity {
SQLiteDatabase db;
String id, subCode, subName, day, time, lecturer, myClass;
String outputTime, outputMon, outputTue, outputWed, outputThur, outputFri;
LinearLayout layout,box;
TextView tvTime,tvDetail;
ArrayList<String> MondaySubject = new ArrayList<String>();
ArrayList<String> TuesdaySubject = new ArrayList<String>();
ArrayList<String> WednesdaySubject = new ArrayList<String>();
ArrayList<String> ThrusdaySubject = new ArrayList<String>();
ArrayList<String> FridaySubject = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_timetable);
layout = (LinearLayout)findViewById(R.id.liLayout);
db = openOrCreateDatabase("subjects", MODE_PRIVATE, null);
Cursor cursor = db.rawQuery(
"select s.id, subCode, s.subName, day, time, lecturer, class" +
" from subjects s, timetable t, UserSubject us " +
"where s.id = t.id and us.subName = s.subName",null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
box = new LinearLayout(this);
box.setOrientation(LinearLayout.VERTICAL);
//box.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//layoutParams.setMargins(35, 10, 35, 10);
tvTime = new TextView(this);
tvTime.setGravity(Gravity.RIGHT);
tvDetail = new TextView(this);
//box.addView(tvDay);
box.addView(tvTime);
box.addView(tvDetail);
box.setLayoutParams(layoutParams);
layout.addView(box);
id = cursor.getString(cursor.getColumnIndex("id"));
subCode = cursor.getString(cursor.getColumnIndex("subCode"));
subName = cursor.getString(cursor.getColumnIndex("subName"));
day = cursor.getString(cursor.getColumnIndex("day"));
time = cursor.getString(cursor.getColumnIndex("time"));
lecturer = cursor.getString(cursor.getColumnIndex("lecturer"));
myClass = cursor.getString(cursor.getColumnIndex("class"));
if(day.equals("Monday")){
outputMon = cursor.getString(cursor.getColumnIndex("subCode")) + "\n" +
cursor.getString(cursor.getColumnIndex("subName")) + "\n" +
cursor.getString(cursor.getColumnIndex("class")) + "\n" +
cursor.getString(cursor.getColumnIndex("lecturer")) ;
outputTime = cursor.getString(cursor.getColumnIndex("time"));
tvTime.setText(outputTime);
tvDetail.setText(outputMon);
}
if(day.equals("Tuesday")){
outputTue = cursor.getString(cursor.getColumnIndex("subCode")) + "\n" +
cursor.getString(cursor.getColumnIndex("subName")) + "\n" +
cursor.getString(cursor.getColumnIndex("class")) + "\n" +
cursor.getString(cursor.getColumnIndex("lecturer")) ;
outputTime = cursor.getString(cursor.getColumnIndex("time"));
tvTime.setText(outputTime);
tvDetail.setText(outputTue);
}
if(day.equals("Wednesday")){
outputWed = cursor.getString(cursor.getColumnIndex("subCode")) + "\n" +
cursor.getString(cursor.getColumnIndex("subName")) + "\n" +
cursor.getString(cursor.getColumnIndex("class")) + "\n" +
cursor.getString(cursor.getColumnIndex("lecturer")) ;
outputTime = cursor.getString(cursor.getColumnIndex("time"));
tvTime.setText(outputTime);
tvDetail.setText(outputWed);
}
if(day.equals("Thursday")){
outputThur = cursor.getString(cursor.getColumnIndex("subCode")) + "\n" +
cursor.getString(cursor.getColumnIndex("subName")) + "\n" +
cursor.getString(cursor.getColumnIndex("class")) + "\n" +
cursor.getString(cursor.getColumnIndex("lecturer")) ;
outputTime = cursor.getString(cursor.getColumnIndex("time"));
tvTime.setText(outputTime);
tvDetail.setText(outputThur);
}
if(day.equals("Friday")){
outputFri = cursor.getString(cursor.getColumnIndex("subCode")) + "\n" +
cursor.getString(cursor.getColumnIndex("subName")) + "\n" +
cursor.getString(cursor.getColumnIndex("class")) + "\n" +
cursor.getString(cursor.getColumnIndex("lecturer")) ;
outputTime = cursor.getString(cursor.getColumnIndex("time"));
tvTime.setText(outputTime);
tvDetail.setText(outputFri);
}
}
}
}
}
activity_view_timetable.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/liLayout"
xmlns:android="http://schemas.android.com/apk/res/android">
</LinearLayout>