我的自定义ListView在Fragment中不起作用

时间:2016-07-26 12:23:48

标签: listview fragment

我复制了在Activity中运行良好的代码和类(我将我的活动改为fragment to use tab)。但在片段中,它不起作用。没有应用程序粉碎,但任何列表都不会出现在我的显示器上。 我已经检查了我的数据库,它正常工作。但我的清单上没有显示。请帮我。下面是我的代码。

ListFragment.java

public class ListFragment extends android.support.v4.app.Fragment {

DBHelper mydb;
SQLiteDatabase db;

Intent retent;
private int amount;
private String date;
private String time;
private String use;
private ListView useList;
private ArrayList<history> histories;
private historyAdapter adapter;
private String checker;
private TextView upperBalance;

private int position=4;
private int onClickPosition;
private int itemcount;
private int amt=0;

public boolean bottomViewActivate = false;
public boolean monthlyActivate = true;

private int bottomViewId =0;
private TextView Arvitor;
private FrameLayout frameArvitor;
private ArrayList<history> historiesBackup;

private int CurrentYearMonthInt=0;
private int CurrentAddBalance=0;
public Calendar c = Calendar.getInstance();
public SimpleDateFormat intform = new SimpleDateFormat("yyMM");
public SimpleDateFormat yearform = new SimpleDateFormat("yyyy");
public SimpleDateFormat monthform = new SimpleDateFormat("MM");
public SimpleDateFormat yearf = new SimpleDateFormat("yy");
int year = Integer.parseInt(yearform.format(c.getTime()));
int month = Integer.parseInt(monthform.format(c.getTime()));

public ListFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_page2, container,    false);
    year = Integer.parseInt(yearform.format(c.getTime()));
    month = Integer.parseInt(monthform.format(c.getTime()));
    CurrentYearMonthInt=Integer.parseInt(intform.format(c.getTime()));
    mydb = new DBHelper(getActivity());

    upperBalance = (TextView)rootView.findViewById(R.id.ListUpView);
    if(mydb.numberOfRows()!=0) {
        upperBalance.setText(" Current Balance : "+mydb.getRecentBalance()+"won");
    }else {
        upperBalance.setText(" Balance : 0won");
    }

    useList=(ListView) rootView.findViewById(R.id.listView);
    histories = new ArrayList<history>();
    adapter = new historyAdapter(getActivity(), R.layout.listsublayout, histories);
    useList.setAdapter(adapter);
    getActivity().registerForContextMenu(useList);
    histories=mydb.getAllUseList(bottomViewId,bottomViewActivate,monthlyActivate,CurrentYearMonthInt);
    itemcount=mydb.numberOfRows();
    adapter.notifyDataSetChanged();

    return rootView;
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

但我无法理解为什么这个解决方案有效。 无论如何,我提出我是如何解决这个问题的对象。

我改变了

历史= mydb.getAllUseList(bottomViewId,bottomViewActivate,monthlyActivate,CurrentYearMonthInt);

histories.addAll(mydb.getAllUseList(bottomViewId,bottomViewActivate,monthlyActivate,CurrentYearMonthInt));

任何知道此解决方案工作原因的人都会回复我的问题。