我如何才能在我的表格中查看某些数据

时间:2014-03-10 14:49:43

标签: java android listview android-listview

我有2个表:主题和备注表。我想列出选择了相同主题名称的笔记。

首先是所有主题的列表视图(View_Subj),然后点击它进入主题笔记的列表视图,添加按钮将注释插入主题 我的View_Subj.java是:

public class View_Subj extends ListActivity {


private final String SAMPLE_DB_NAME = "project";
private final String SAMPLE_TABLE_NAME = "subject";
 SQLiteDatabase sampleDB = null;
 ArrayList<String> results = new ArrayList<String>();
/** Called when the activity is first created. */

public void onListItemClick(ListView l, View view, final int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, view, position, id);       
    Intent in = new Intent (View_Subj.this,View_PPT_List.class);
    String subj = (l.getItemAtPosition(position)).toString();
    Toast.makeText(getApplicationContext(), subj, Toast.LENGTH_SHORT).show();
    in.putExtra("com.example.tinio_bolasa_project.sub1", subj);
    startActivity(in);
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 



    try {
        sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
        //name, mode MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, factory

        /*sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                SAMPLE_TABLE_NAME +
                " (LastName VARCHAR, FirstName VARCHAR," +
                " City VARCHAR, Age INT(3));");*/



        Cursor c = sampleDB.rawQuery("SELECT * FROM " +
                SAMPLE_TABLE_NAME, null);



        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String subid = 
c.getString(c.getColumnIndex("subj_ID"));
                    String subject = 
c.getString(c.getColumnIndex("subjName"));

                    results.add(subid + subject);
                }while (c.moveToNext());
            } 
        }

        this.setListAdapter(new ArrayAdapter<String>(this, 
 android.R.layout.simple_list_item_1,results));

    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        if (sampleDB != null) 
            //sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
            sampleDB.close();
    }
}
}

我的View_PPT_List.java:

public class View_PPT_List extends ListActivity {
private final String SAMPLE_DB_NAME = "project";
private final String PPT_TABLE_NAME1 = "notes";
private final String PPT_TABLE_NAME2 = "subject";
 SQLiteDatabase notesDB = null;

 ArrayList<String> results = new ArrayList<String>();

 public void onListItemClick(ListView l, View view, final int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, view, position, id);       



        Intent ins = new Intent (View_PPT_List.this,PPTActivity.class);
        String s = (l.getItemAtPosition(position)).toString();
        ins.putExtra("com.example.tinio_bolasa_project.finame", s);

       // ins.putExtra("Entries", l.getItemAtPosition(position).toString());

        Toast.makeText(getApplicationContext(),s ,Toast.LENGTH_SHORT ).show();
        startActivity(ins);
    }

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   String name = getSublabel();

    try{
         notesDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

         notesDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                PPT_TABLE_NAME1 + 
                " ( notes_ID INTEGER PRIMARY KEY AUTOINCREMENT, " + 
"subjLabel VARCHAR, " + "pptName VARCHAR, " + "pptPath VARCHAR);");

    //     int x1 = getIntent().getExtras().getInt("Entries");
         Cursor c = notesDB.rawQuery("SELECT * FROM notes WHERE subjLabel='"+name+"'",
 null);


    if (c != null ) {
        if  (c.moveToFirst()) {
            do {

                String ppt = c.getString(c.getColumnIndex("pptName"));

                results.add(ppt);
            }while (c.moveToNext());
        } 
    }

    this.setListAdapter(new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1,results));

} catch (SQLiteException se ) {
    Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
    if (notesDB != null) 
        //sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
        notesDB.close();
}



    setContentView(R.layout.activity_view__ppt__list);
    Button addppt = (Button) this.findViewById(R.id.button1);
    addppt.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent inten = new Intent (View_PPT_List.this, 
Add_PPT_Activity.class);
            startActivity(inten);
        }

    });
    }
public String getSublabel(){
    String sublabel = 
getIntent().getStringExtra("com.example.tinio_bolasa_project.finame");
    return sublabel;
}
}

我的Add_PPT_Activity:

public class Add_PPT_Activity extends ListActivity {

private final String SAMPLE_DB_NAME = "project";
private final String PPT_TABLE_NAME1 = "notes";
SQLiteDatabase notesDB = null;

View_PPT_List getsubla = new View_PPT_List();
ArrayList<String> FilesInFolder = GetFiles(Environment.getExternalStorageDirectory()
        .getPath());
public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);
    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for(int i=0; i<files.length;i++)
            MyFiles.add(files[i].getName());
    }
    return MyFiles;
}

 public void onListItemClick(ListView l, View view, final int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, view, position, id);   

String filename = FilesInFolder.get(position);
        String filepath = Environment.getExternalStorageDirectory()
                .getPath() + "/" + filename;
         String subla = getsubla.getSublabel();
notesDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
notesDB.execSQL("INSERT INTO " +
                    PPT_TABLE_NAME1 + " (subjid, "+ "pptName, " + 
"pptPath)" + 
                    " Values ('"+subla+"', '"+filename+"', 
'"+filepath+"');");

        Intent in = new Intent (Add_PPT_Activity.this,View_PPT_List.class);
        startActivity(in);
    }
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,FilesInFolder));
}
}

请帮忙,

0 个答案:

没有答案