2012-02-26 16 views
0

私はListViewに入れたデータを使ってデータベースを作成しました。プログラムによって作成されたため、このListViewを含むxmlファイルはありません。コード:ListViewを作成したデータベースを他のXMLに実装する方法ListViewを作成しましたか?

public class hornot extends Application{ 

    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_NAME = "persons_name"; 
    public static final String KEY_HOTNESS = "persons_hotness"; 
    public static final String DATABASE_NAME = "HotOrNotdb"; 
    private static final String DATABASE_TABLE = "peopleTable"; 
    private static final int DATABASE_VERSION = 1; 

    private DbHelper ourHelper; 
    private final Context ourContext; 
    private SQLiteDatabase ourDatabase; 

    private static class DbHelper extends SQLiteOpenHelper { 
    public DbHelper(Context context) { 
    super (context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    KEY_NAME + " TEXT NOT NULL, " + 
    KEY_HOTNESS + " TEXT NOT NULL);" 
    ); 

    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
    onCreate(db); 
    } 

    } 
public hornot(Context c) { 
    ourContext = c; 
    } 

public hornot open() throws SQLException{ //ez a throws kell az SQLiteExample.java try részhez 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
    } 

これは私がListViewにアイテムを入れる方法です:

ListView lv; 
    ArrayList<String> todoItems = new ArrayList<String>(); 
ArrayList<String> todoItems2 = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems)); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    final hornot info = new hornot(this); 
    info.open(); 

    Cursor c = info.getAllTitles(); 
    if (c.moveToFirst()) 
     { 
     do{ 
       todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2)); 
     }while (c.moveToNext()); 
     } 
      if (todoItems.size() > 0) 
      { 
     lv.setAdapter(new ArrayAdapter<String>(sqview.this,android.R.layout.simple_list_item_1, todoItems)); 
      } 

私は、XMLを作成し、私は、プログラム(ID listplusで別のListViewListViewを作成つけたいのですが):

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:text="Button" android:id="@+id/b1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="Button" android:id="@+id/b2" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <ListView android:id="@+id/listplus" android:layout_height="wrap_content" 
    android:layout_width="fill_parent"><;/ListView> 
</LinearLayout> 

どうすればいいですか?

+0

1.なぜあなたの 'hornot'は' Application'を拡張していますか? 2.あなたはどこで 'listplus'というidを持つレイアウトを使っていますか?(別のアクティビティですか?) – Luksprog

+0

@スルスキー1. hornot extendアプリケーションは間違いです、それはありません:) 2.はい、好きです別のアクティビティで使用することが可能です。 –

+0

可能かどうかはわかりませんが、古いリストのように、すべてのメソッドが新しいリストビューで機能するようにしたいと思います。 –

答えて

2

カスタムレイアウトのListActivityがほしいと思っています。今すぐリスト(ボタン、画像など)以外の要素を持たせたい場合は、ListActivity(デフォルトでは全画面リストのみを表示)を使用して、コンテンツビューをxmlレイアウトに設定します。コンテンツレイアウトをカスタムレイアウトに設定した場合、そのレイアウトでにはListViewのIDが"@android:id/list"であるListViewが必要なので、データのバインド先を知ることができます。だから、あなたは、あなたがそれを変更し、リストに必要なidを追加掲載レイアウトを使用する場合:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:text="Button" android:id="@+id/b1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="Button" android:id="@+id/b2" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <ListView android:id="@android:id/list" android:layout_height="wrap_content" 
    android:layout_width="fill_parent"></ListView> 
</LinearLayout> 

次に、あなただけの行を追加使用し、あなたの現在のListActivityに:

@Override 
    protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.the_name_of_above_layout); // <=this line 
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems)); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    final hornot info = new hornot(this); 
    info.open(); 

    Cursor c = info.getAllTitles(); 
    if (c.moveToFirst()) { 
     do { 
      todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2)); 
     } while (c.moveToNext()); 
    } 
    if (todoItems.size() > 0) { 
     lv.setAdapter(new ArrayAdapter<String> (sqview.this,android.R.layout.simple_list_item_1, todoItems)); 
    } 
+0

私の間違いを見つけるのに時間が掛かってしまったので、あなたはID "@android:id/list"でListViewを持っていなければならないと言っています:)もう一度あなたの助けをありがとう、私は初心者です。問題はこれです。好奇心のためにリストビューが増えれば、それらのIDは何か? android:id/listは1つのidだけです。アンドロイド:id/list2は受け入れられません。 –

+0

@ JaniBela 'ListActivity'は' id'( '@android:id/list')のリストを1つしか持たず、' ListView'だけを管理します。 'ListActivity'は1つのリストだけを扱うため、特別なIDを持つレイアウト以外に同じレイアウトの' ListView'が必要な場合は、それらのIDを与えます。 – Luksprog

関連する問題