2014-07-05 129 views
1

請在此應用程序中獲得幫助。我想在android應用程序中嵌入數據庫。我的表格有3列(_id,Title,Text)。應用程序的第一頁顯示列表視圖中的標題,但我想在單擊列表中的項目時在另一個活動中顯示文本。這是迄今爲止如何。 (我發現這個項目在線,並已修改它。)Android中的預填充數據庫

package com.softeq.prepopdb.activity; 

import java.util.ArrayList; 

import android.app.ListActivity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.softeq.android.prepopdb.R; 
import com.softeq.prepopdb.dbhelper.ExternalDbOpenHelper; 

public class PrepopSqliteDbActivity extends ListActivity { 
private static final String DB_NAME = "schymn.sqlite"; 
private static final String TABLE_NAME = "Hymns"; 
private static final String Hymn_ID = "_id"; 
private static final String Hymn_Title = "Title"; 
private static final String Hymn_Text = "Text"; 
    String lyrics; 

private SQLiteDatabase database; 
private ListView listView; 
private ArrayList<String> Songs; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME); 
    database = dbOpenHelper.openDataBase(); 

    fillHymns(); 
    setUpList();  
} 

private void setUpList() { 

    setListAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, Songs)); 
    listView = getListView(); 


    /*listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
          int position,long id) { 
      Toast.makeText(getApplicationContext(), 
         ((TextView) view).getText().toString(), 
         Toast.LENGTH_SHORT).show();     
     } 
    });*/ 

    listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
          int position,long id) { 

     } 
    }); 
} 


private void fillHymns() { 
    Songs = new ArrayList<String>(); 
    Cursor hymnCursor = database.query(TABLE_NAME, 
             new String[] 
             {Hymn_ID, Hymn_Title,Hymn_Text}, 
             null, null, null, null 
             , Hymn_Title); 
    hymnCursor.moveToFirst(); 
    if(!hymnCursor.isAfterLast()) { 
     do { 
      String title = hymnCursor.getString(1); 
      lyrics = hymnCursor.getString(2); 
      Songs.add(title); 
      //Songs.add(lyrics); 
     } while (hymnCursor.moveToNext()); 
    } 
    hymnCursor.close(); 
} 
} 

我試過這個,但它沒有工作。

listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
          int position,long id) { 
         Log.i("text",lyrics); 
     } 
    }); 
} 
+0

是'Log.i(「text」,lyrics)'應該創建另一個活動嗎?看看'startActivity'方法。 – m0skit0

+0

noo,它假設記錄與標題相關的文本。我知道如何通過意圖傳遞額外信息。 – olakunle

+0

你有沒有例外?你的日誌記錄什麼? – joao2fast4u

回答

0

謝謝你們,我能夠通過創建適配器並將關鍵值對中的細節存儲來解決問題。