具有listview片段的选项卡显示为空白屏幕

时间:2014-10-16 11:49:32

标签: android listview android-fragments android-tabhost android-fragmentactivity

在打开下面的活动时,它始终显示一个blankScreen。请帮助我是android新手, 我正在从数据库填充我的列表并对该数据执行一些操作并在列表视图中显示它。当我在listview中将这些数据填充为单独的活动时,它工作正常,但是当我对片段执行相同操作时,它显示空白屏幕。

我的标签活动类

package com.example.myexpensemanager;

import com.example.myexpensemanager.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class TabActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "All", "Daily", "Weekly","Monthly" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

我的适配器类

package com.example.myexpensemanager.adapter;

import com.example.myexpensemanager.AllExpense;
import com.example.myexpensemanager.DailyExpense;
import com.example.myexpensemanager.MonthlyExpense;
import com.example.myexpensemanager.WeeklyExpense;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabsPagerAdapter extends FragmentStatePagerAdapter{

    public TabsPagerAdapter(FragmentManager supportFragmentManager) {
        // TODO Auto-generated constructor stub
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
         switch (arg0) {
            case 0:

                return  new AllExpense();
            case 1:

                return new DailyExpense();
            case 2:
                // Movies fragment activity
                return new WeeklyExpense();
            case 3:
                // Movies fragment activity
                return new MonthlyExpense();
            }

            return null;
        }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

}

我的片段类之一。

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

import cal_helper.Calendar_Helper;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;

import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class WeeklyExpense extends Fragment {
    ListView lv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_history, container, false);
lv = (ListView) rootView.findViewById(R.id.history_list);

        populatedata();
        return rootView;
    }

private void populatedata() {
    // TODO Auto-generated method stub



//      setContentView(R.layout.activity_history);
//      populatedata();
        Log.d("POint: ", "0");


        //tv = (TextView)findViewById(R.id.textView123);
        Context context = getActivity().getApplicationContext();
        Log.d("POint: ", "05");
        expensedb db = new expensedb(context);
        Log.d("POint: ", "04");
        Cursor cursor = db.getAllExpenses();
        Log.d("POint: ", "01");
        List<Expense> expenseList = new ArrayList<Expense>();
        Log.d("POint: ", "02");
        if (cursor.moveToFirst()) {
            do {
                   Expense expense = new Expense();
                expense.setId(Integer.parseInt(cursor.getString(0)));
                expense.setDateid(cursor.getString(1));
                expense.setDescription(cursor.getString(2));
                expense.setAmount(Integer.parseInt(cursor.getString(3)));
                expense.setDate(cursor.getString(4));
                expense.setDateStamp(cursor.getString(5));
                expense.setCat_id(Integer.parseInt(cursor.getString(6)));
                // Adding contact to list
                expenseList.add(expense);
            } while (cursor.moveToNext());
        }
        Log.d("POint: ", "1");
ListMultimap<Integer, Expense> myMultimap = ArrayListMultimap.create();
Map<Integer, Date> myMap = new TreeMap<Integer,Date>();
Calendar_Helper cal = new Calendar_Helper();
for (Expense x : expenseList) {
    Date date=cal.strtodate(x.getDate());
    int key =cal.weekOfYear(date) + cal.year(date)*100;
    myMultimap.put(key,x);
    myMap.put(key,date);
}
Log.d("POint: ", "2");
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
NavigableMap<Integer, Date> myMap1=((TreeMap<Integer, Date>) myMap).descendingMap();
for(Integer key : myMap1.keySet()){
    List<Expense> list = new ArrayList<Expense>();
    list = myMultimap.get(key);
    int amount = cal_amount(list);
    int week = cal.weekOfYear(myMap.get(key));
    int year = cal.year(myMap.get(key));
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("startdate", cal.startDate(week, year));
    map.put("enddate",cal.endDate(week, year));
    map.put("amount", Integer.toString(amount));
    mylist.add(map);
}
Log.d("POint: ", "4");

//now i have the all key values
SimpleAdapter adapter = new SimpleAdapter(context, mylist, R.layout.activity_week,
        new String[] {"startdate", "enddate", "amount"}, new int[] {R.id.tv_week, R.id.tv_week2, R.id.tv_Wamount});
lv.setAdapter(adapter);
    }



private int cal_amount(List<Expense> list) {
    int amount=0;
    for(Expense x: list)
    {
        amount += x.getAmount();
    }
    return amount;
}
}

1 个答案:

答案 0 :(得分:0)

你的伯爵应该是4岁。

public int getCount() {
        // TODO Auto-generated method stub
        return 4;
    }