-3
我想在同一時間列表視圖和標籤欄都和我的代碼如下:如何添加ListView和標籤欄在Android的
請檢查我的主要活動和自定義適配器,並告訴我什麼樣的變化,我需要去做。 由於列表正在工作,但標籤沒有顯示。
我已經包含了兩個XML文件。 1.tab_bar 2.final_tab_item
請讓我知道我錯了......
MainActivity.java
package com.example.tabwithlist;
import java.util.ArrayList;
import com.example.customlist.CustomAdapter;
import com.example.customlist.ListModel;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class MainActivity extends TabActivity implements OnTabChangeListener
{
ListView list;
CustomAdapter adapter;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
/** Called when the activity is first created. */
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_bar);
// setContentView(R.layout.custom_list_view);
setListData();
Resources res =getResources();
list=(ListView)findViewById(R.id.listnew);
//**************** Create Custom Adapter *********//*
adapter=new CustomAdapter(this, CustomListViewValuesArr,res);
list.setAdapter(adapter);
for (int i = 0; i < 11; i++) {
final ListModel sched = new ListModel();
//******* Firstly take data in model object ******//*
sched.setCompanyName("Company "+i);
sched.setImage("image"+i);
sched.setUrl("http:\\\\www."+i+".com");
//******** Take Model Object in ArrayList **********//*
CustomListViewValuesArr.add(sched);
}
// Get TabHost Refference
tabHost = getTabHost();
// Set TabChangeListener called when tab changed
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
//************* TAB1 ************//*
// Create Intents to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tab1.class);
spec = tabHost.newTabSpec("First").setIndicator("")
.setContent(intent);
//Add intent to tab
tabHost.addTab(spec);
//************* TAB2 ************//*
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("Second").setIndicator("")
.setContent(intent);
tabHost.addTab(spec);
/************* TAB3 ************/
intent = new Intent().setClass(this, Tab3.class);
spec = tabHost.newTabSpec("Third").setIndicator("")
.setContent(intent);
tabHost.addTab(spec);
// Set drawable images to tab
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3);
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1_over);
}
private void setListData() {
for (int i = 0; i < 11; i++) {
final ListModel sched = new ListModel();
/******* Firstly take data in model object ******/
sched.setCompanyName("Company "+i);
sched.setImage("image"+i);
sched.setUrl("http:\\\\www."+i+".com");
/******** Take Model Object in ArrayList **********/
CustomListViewValuesArr.add(sched);
}
}
@Override
public void onTabChanged(String tabId) {
/************ Called when tab changed *************/
//********* Check current selected tab and change according images *******/
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab1);
else if(i==1)
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab2);
else if(i==2)
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab3);
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==0)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab1_over);
else if(tabHost.getCurrentTab()==1)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab2_over);
else if(tabHost.getCurrentTab()==2)
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab3_over);
}
}
CustomAdapter.java(Here I am inflating my final_tab_item.xml file which displays how list view will look like.)
package com.example.customlist;
import java.util.ArrayList;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
//import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout() **********************/
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public TextView textWide;
public ImageView image;
}
/*********** Depends upon data size called for each row , Create each ListView row ***********/
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
/********** Inflate tabitem.xml file for each row (Defined below) ************/
//vi = inflater.inflate(R.layout., null);
vi=inflater.inflate(com.example.tabwithlist.R.layout.final_tab_item,null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(com.example.tabwithlist.R.id.text);
holder.text1=(TextView)vi.findViewById(com.example.tabwithlist.R.id.text1);
holder.image=(ImageView)vi.findViewById(com.example.tabwithlist.R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListModel) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(tempValues.getCompanyName());
holder.text1.setText(tempValues.getUrl());
holder.image.setImageResource(res.getIdentifier("com.example.tabwithlist:drawable/"+tempValues.getImage(),null,null));
/******** Set Item Click Listner for LayoutInflater for each row ***********/
// vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}
}
tab_bar.xml(In this file I am declaring list view and tab bar)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
</TabHost>
<ListView
android:id="@+id/listnew"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
,但我的選項卡不顯示 – 2014-09-19 12:50:59
刪除MainActivity所有的列表視圖代碼,並創造了所有的列表視圖代碼TAB1.java和創建新的XML只有列表視圖和參考TAB1.java ....但仍然顯示標籤不顯示..只有列表視圖是可見的 – 2014-09-19 13:08:01