2012-03-20 22 views
0
import android.app.Activity; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 

public class Database extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     DatabaseHelper dbh = new DatabaseHelper(this); 
     Cursor myCursor = dbh.getReadableDatabase() 
         .rawQuery("SELECT _id, " + DatabaseHelper.NAME + 
            ", " + DatabaseHelper.VALUE + 
            " FROM " + DatabaseHelper.TABLE, null); 
     String[] dataFrom = {DatabaseHelper.NAME, DatabaseHelper.VALUE}; 
     int[] dataTo = {R.id.name, R.id.value}; 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
       R.layout.row, myCursor, dataFrom, dataTo); 

     setListAdapter(adapter); 

     /*TextView tv = new TextView(this); 
     tv.setText("Hello, Android"); 
     setContentView(tv);*/ 



    } 

    public void onListItemClick() 
    { 

    } 



} 

リストビューの下部にボタンを挿入して別のアクティビティに移動しようとしています。このリストビューにボタンを挿入することは可能ですか?オプションを含むメニューになる次のアクティビティにアクセスするには、ボタンが必要です。 XMLで1つのアクティビティ(データベース)から別のアクティビティへの転送

+0

なぜ私はあなたがXMLレイアウトにボタンを置くだけではなく、あなたが望むことをするonClick()メソッドをそれに配置しますか? – raingod

+0

これはコード内でr.layout.mainを呼び出さないためです。私が気付いた場合は、コメントアウトしています。 –

答えて

1

あなたが活動に余分のボタンを追加したい場合、あなたはこれが

public class Database extends ListActivity 
をline--交換する必要がこの行の

--

public class Database extends Activity 

と、あなたは余分なコントロールを追加したい場合は、そのneccessaryは、レイアウトを設定しているので

setContentView(R.layout.main); 

also--このラインを使用する必要があります。..

単にAfterthatあなたmain.xml--で

<Button android:id="@+id/button" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:gravity="center" 
     android:text="Next Activity" /> 

<ListView android:id="@android:id/list" android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 

活動での使用ではこのalso--

ListView lv=(ListView)findViewById(R.id.list); 
lv.setClickable(true); 
を追加

//ボタン

Button button1 = (Button)findViewById(R.id.button1); 
button1.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
Intent intent=new Intent(this,NextActivity.class); 
    startActivity(intent); 

    } 
}); 

を設定し、これは私が話したwhereever私は....あなたの問題を解決した

setListAdapter(adapter); 

フロム・

lv.setAdapter(adapter); 

をline--置き換えるために間違って....それに応じてそれを変更しました...

+0

ありがとう、私はそれを試してみよう –

+0

たくさんのことをありがとう..何ですか? –

1

:活動の

<ListView android:id="@android:id/list" android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:dividerHeight="1dip" 
       android:cacheColorHint="#00000000" /> 

<Button android:id="@+id/button1" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:gravity="center" 
      android:text="Next" /> 

Button button1 = (Button)findViewById(R.id.button1); 
button1.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 

    } 
}); 
関連する問題