» 首頁 » 討論區 » Android程式設計 »TabActivity

TabActivity

發表人: 訪客
發表時間: 2011-11-08 22:37:55
你好,請問在editText輸入文字串(activity為main)後
要怎麼樣讓相對應的圖片A、B、C
出現在tabA、tabB、tabC?(各有一個imageView)
tabhost的部分我已經寫好了
(有三個tabspec:A、B、C)
發表人: 訪客
發表時間: 2011-11-09 16:04:01
請問一下,bundle傳的值
可以傳給三個activity嗎?
發表人: 訪客
發表時間: 2011-11-09 19:20:34
你好,我要怎麼讓layout的backgroud隨著資料庫的資料(圖)變動?
請問程式碼該怎麼寫呢?
發表人: 訪客
發表時間: 2011-11-09 22:41:19
你好,
我tabactivity程式碼是這樣

[sea:javaCode]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
database = openDatabase();

Resources res = getResources();//object to get Drawable
TabHost tabHost = getTabHost();//The activity TabHost
TabHost.TabSpec spec;//TabSpec for each tab
Intent intent1,intent2,intent3;//Intent for each tab

//取得Intent中的Bundle物件
intent = this.getIntent();
bunde = intent.getExtras();

//取得Bundle物件中的資料

String input = bunde.getString("name");


Cursor c = null;

if (input == null == false) {
c = database.query(true, TABLE_FILENAME, new String[]{"_id\n","name\n", "response\n","photo\n","office\n","lab\n"},
"name=" + "\"" + input + "\"",null, null, null, null, null);
}

if (c == null)
return;

if (c.getCount() == 0)
{
Toast.makeText(tabtest.this, "沒有資料", Toast.LENGTH_LONG)
.show();
}

else
{
c.moveToFirst();
byte[] photo = c.getBlob(3);
byte[] office = c.getBlob(4);
byte[] lab = c.getBlob(5);

//Create an Intent to launch an Activity for the tab(to be reused)
intent1 = new Intent().setClass(this, Income.class);
//new一個Bundle物件,並將要傳遞的資料傳入
Bundle bundle1 = new Bundle();
bundle1.putByteArray("photo", photo);
intent1.putExtras(bundle1);

//初始化每一個標籤的TabSpec,並且將它加入TabHost
spec = tabHost.newTabSpec("Income").setIndicator("Income",
res.getDrawable(R.drawable.tab_icon)).setContent(intent1);
tabHost.addTab(spec);

intent2 = new Intent().setClass(this, Outcome.class);
Bundle bundle2 = new Bundle();
bundle2.putByteArray("office", office);
intent2.putExtras(bundle2);

spec = tabHost.newTabSpec("Outcome").setIndicator("Outcome",
res.getDrawable(R.drawable.tab_icon)).setContent(intent2);
tabHost.addTab(spec);


intent3= new Intent().setClass(this, Total.class);
Bundle bundle3 = new Bundle();
bundle3.putByteArray("lab", lab);
intent3.putExtras(bundle3);

spec = tabHost.newTabSpec("Total").setIndicator("Total",
res.getDrawable(R.drawable.tab_icon)).setContent(intent3);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}
[/sea]

我是三個bundle分別傳到不同的class
然後在三個class出現不同的圖片
但是在切換的部分有錯誤
不知道是哪裡有問題呢?
發表人: Seachaos
積分: 2432
發表時間: 2011-11-10 13:23:14
Hi:
諸問有錯誤訊息嗎?
你有試過先不要用圖片嗎?
先用String來看看各Activity是否可以成功收到String
發表人: 訪客
發表時間: 2011-11-10 13:40:20
你好,有試過是成功的,但是三個tab都是同個圖片
後改成放三個不同的圖片就不行

若文字串沒輸入文字直接按確認
他會跳到其他頁面,然後顯示沒有資料

若文字串有輸入文字案確認後
會顯示應用程式為正常終止

不知道是
[sea:javaCode]
c.moveToFirst();
byte[] photo = c.getBlob(3);
byte[] office = c.getBlob(4);
byte[] lab = c.getBlob(5);
[/sea]
的問題嗎?

還是下面
[sea:javaCode]
//Create an Intent to launch an Activity for the tab(to be reused)
intent1 = new Intent().setClass(this, Income.class);
//new一個Bundle物件,並將要傳遞的資料傳入
Bundle bundle1 = new Bundle();
bundle1.putByteArray("photo", photo);
intent1.putExtras(bundle1);

//初始化每一個標籤的TabSpec,並且將它加入TabHost
spec = tabHost.newTabSpec("Income").setIndicator("Income",
res.getDrawable(R.drawable.tab_icon)).setContent(intent1);
tabHost.addTab(spec);

intent2 = new Intent().setClass(this, Outcome.class);
Bundle bundle2 = new Bundle();
bundle2.putByteArray("office", office);
intent2.putExtras(bundle2);

spec = tabHost.newTabSpec("Outcome").setIndicator("Outcome",
res.getDrawable(R.drawable.tab_icon)).setContent(intent2);
tabHost.addTab(spec);


intent3= new Intent().setClass(this, Total.class);
Bundle bundle3 = new Bundle();
bundle3.putByteArray("lab", lab);
intent3.putExtras(bundle3);

spec = tabHost.newTabSpec("Total").setIndicator("Total",
res.getDrawable(R.drawable.tab_icon)).setContent(intent3);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}
[/sea]
這裡的問題?
發表人: Seachaos
積分: 2432
發表時間: 2011-11-11 03:17:06
你好:
看來有可能是
[sea:javaCode]
c.moveToFirst();
byte[] photo = c.getBlob(3);
byte[] office = c.getBlob(4);
byte[] lab = c.getBlob(5);
[/sea]

你的資料結構是這三個圖片在同一個row嗎 (3個column) ?
還是他是三個row (三筆資料)

(你上面的Column看來是_id,name,response ,所以我想比較有可能是這個地方有問題)
發表人: 訪客
發表時間: 2011-11-11 11:47:31
你好,大部分執行都可以
但是有個問題是
若在文字串輸入"A",則可以在三個tab出現不同的圖
若輸入"B",則在一與三的tab可以出現不同的圖,但自第2tab會說沒有資料
也就是有些文字串,在tab2可以出現,有些不行
我有檢查過資料庫也沒有問題
不知道哪裡有問題呢?

順便問一下,若要讓圖顯示在三個layout的background
程式碼要怎麼寫呢?
發表人: Seachaos
積分: 2432
發表時間: 2011-11-11 14:41:09
你Tab的Activity可以方便貼出來看一下嗎?
會不會是在取得Bundle的Data時出了問題?
發表人: 訪客
發表時間: 2011-11-12 16:18:45
你好,這是tabactivity的code
[sea:javaCode]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

database = openDatabase();

Resources res = getResources();//object to get Drawable
TabHost tabHost = getTabHost();//The activity TabHost
TabHost.TabSpec spec;//TabSpec for each tab
Intent intent1,intent2,intent3;//Intent for each tab

//取得Intent中的Bundle物件
bunde = new Bundle();
bunde = getIntent().getExtras();

//取得Bundle物件中的資料

String input = "";
input = bunde.getString("tname");

//new一個Bundle物件,並將要傳遞的資料傳入
bundle1 = new Bundle();
bundle1.putString("tname", input);

bundle2 = new Bundle();
bundle2.putString("tname", input);

bundle3 = new Bundle();
bundle3.putString("tname", input);

intent1 = new Intent();
intent1.putExtras(bundle1);
intent1.setClass(this, Income.class);


//初始化每一個標籤的TabSpec,並且將它加入TabHost
spec = tabHost.newTabSpec("Income").setIndicator("課表",
res.getDrawable(R.drawable.tab_icon)).setContent(intent1);
tabHost.addTab(spec);


intent2 = new Intent();
intent2.putExtras(bundle2);
intent2.setClass(this, Outcome.class);
spec = tabHost.newTabSpec("Outcome").setIndicator("辦公室",
res.getDrawable(R.drawable.tab_icon)).setContent(intent2);
tabHost.addTab(spec);


intent3 = new Intent();
intent3.putExtras(bundle3);
intent3.setClass(this, Total.class);
spec = tabHost.newTabSpec("Total").setIndicator("實驗室",
res.getDrawable(R.drawable.tab_icon)).setContent(intent3);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}
[/sea]
發表人: Seachaos
積分: 2432
發表時間: 2011-11-15 14:18:29
Hi, 我沒有看到你取得ByteArray的地方
也許是這個部份有問題?
發表人: 訪客
發表時間: 2011-11-15 21:31:05
你好,我把ByteArray寫在三個不同的程式
也就是寫在tabhost裡的三個tab
以下為其中之一,剩下兩個程式碼都不多,只有要放的圖片不同
這個是有時可以出現圖,有時無法出現的圖的程式碼
其他兩個圖(兩個不同的程式碼)都可以出現

[sea:javaCode]
public class Outcome extends Activity {
/** Called when the activity is first created. */
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/dictionary";
public static final String PATH = "/dictionary";
private final String DATABASE_FILENAME = "manage_office.db";
private final String TABLE_FILENAME = "office_data";
private SQLiteDatabase database;
private ImageView mImage;
Bundle bunde;
Intent intent;

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

database = openDatabase();
mImage = (ImageView)findViewById(R.id.imgB);

//取得Intent中的Bundle物件
intent = new Intent();
intent = this.getIntent();
bunde = new Bundle();
bunde = intent.getExtras();

String input = "";
input = bunde.getString("tname");
//取得Bundle物件中的資料


Cursor c = null;

if (input == null == false) {
c = database.query(true, TABLE_FILENAME, new String[]{"_id","name","office"},
"name=" + "\"" + input + "\"",null, null, null, null, null);
}

if (c == null)
return;

if (c.getCount() == 0)
{
Toast.makeText(Outcome.this, "沒有資料", Toast.LENGTH_LONG)
.show();
}

else
{ c.moveToFirst();
byte[] photo = c.getBlob(c.getColumnIndex("office"));
ByteArrayInputStream bais = new ByteArrayInputStream(photo);
mImage.setImageDrawable(Drawable.createFromStream(bais, "photo"));

}
}
[/sea]
發表人: Seachaos
積分: 2432
發表時間: 2011-11-15 23:36:15
Hi
你上面就有把Bytes放到Bundle內了
有試過用bundle.getByteArray來取得圖片嗎?
不一定要每個Activity都從SQLite內取資料

另外你也可以用BitmapFactory.decodeByteArray取代Drawable.createFromStream 看看
(BitmapFactory出來是是Bitmap, 不是Drawable)