2012-03-20 202 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() 
    { 

    } 



} 

我想在Listview的底部插入一個按鈕來轉到另一個活動。有沒有可能在此列表視圖中插入一個按鈕?我需要按鈕來訪問下一個活動,這將是一個包含選項的菜單。從一個活動(數據庫)轉移到另一個活動

+0

我很好奇爲什麼你不只是把一個按鈕放在你的XML佈局,並在其上放置一個onClick()方法,它可以做你想做的事情? – raingod 2012-03-21 02:23:25

+0

這是因爲,我們不在代碼中調用r.layout.main,如果你注意到了,我已經評論了它。 – 2012-03-21 13:18:04

回答

1

如果你想添加的活動額外的按鈕,那麼你必須更換此line--

public class Database extends ListActivity 

來自這條線 -

public class Database extends Activity 

,你必須使用此行also--

setContentView(R.layout.main); 

因爲如果你要添加額外的控制那麼它neccessary設置佈局..

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); 

    } 
}); 

和替換該line--

setListAdapter(adapter); 

From--

lv.setAdapter(adapter); 

我已經解決了您的問題....無論我告訴錯誤....相應地改變了它...

+0

謝謝,讓我試試 – 2012-03-21 12:47:12

+0

感謝您的批評..什麼是您的電子郵件? – 2012-03-21 17:30:22

1

在XML:

<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) { 

    } 
}); 
相關問題