我的MainActivity
是带有两个选项卡的TabbedActivity
,每个选项卡均由单独的Fragment
表示。第二个标签应显示ListView
。在我的MainActivity我用这个Adapter
申请我的片段:片段中的ListView不起作用
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super (fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = Fragment.instantiate(MainActivity.this, Fragment2.class.getName());
break;
case 1:
fragment = Fragment.instantiate(MainActivity.this, Fragment2.class.getName());
break;
}
return fragment;
}
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
}
return null;
}
}
这是Fragment2 class
:
public class Fragment2 extends Fragment {
protected List<Transaction> myTransactions = new ArrayList<Transaction>();
private Context mContext;
DatabaseHelper DBH = new DatabaseHelper(mContext);
String DepositInputValue, DepositInputDate, DepositInputNote;
private static final String ARG_SECTION_NUMBER = "section_number";
public AssetsOverviewList() {
}
public static Fragment2 newInstance(int sectionNumber) {
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_2, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Cursor res = DBH.getAllData();
if (res.getCount() == 0) {
//do something
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Value :" + res.getString(1) + "\n");
buffer.append("Date :" + res.getString(2) + "\n");
buffer.append("Note :" + res.getString(3) + "\n\n");
}
// Show all data
showMessage("Data", buffer.toString());
while (res.moveToNext()) {
DepositInputValue = res.getString(1);
DepositInputDate = res.getString(2);
DepositInputNote = res.getString(3);
}
}
private class LastTransactionsListAdapter extends ArrayAdapter<Transaction> {
public LastTransactionsListAdapter() {
super(MainActivity.this, R.layout.transaction_list_view, myTransactions);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.transaction_list_view, parent, false);
}
Transaction currentTransaction = myTransactions.get(position);
return itemView;
}
}
private void populateListView() {
ArrayAdapter<Transaction> adapter = new LastTransactionsListAdapter();
ListView list = (ListView) list.findViewById(last_transactions_listView);
list.setAdapter(adapter);
}
}
我得到super
在那里说之后就在LastTransactionsListAdapter
错误“MainActivity不一个封闭的类“。它下面的六条线告诉我getLayoutInflater
无法应用。如果我删除所有ListView
相关的东西,该应用程序工作得很好。我该如何解决这个问题?
你的代码有没有意义。 'ArrayAdapter'应该描述单行,但是你想要在里面处理整个'ListView'。 'ListView'它的多行列表,做成这样的[link](https://tausiq.wordpress.com/2012/12/12/android-custom-adapter-listview-with-listfragment-and-loadermanager-inside -fragmentactivity /) – grabarz121
@ grabarz121我很快会去看看 –