几天来,我一直在努力用新的数据列表更新ArrayAdaptor中的ListView数据,但它无法正常工作。 我已经在堆栈溢出上搜索了很多关于此的主题,但我的问题仍然存在。 我已经尝试过listAdapter.notifyDataSetChanged();方法,但什么也没发生。 我在这里发布我的代码,请帮助我。感谢
这是MyTabActivity.java
@SuppressLint("NewApi")
public class MyTabActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_layout);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Toast.makeText(getBaseContext(), "Tab ReSelected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
// Toast.makeText(getBaseContext(), "Tab Selected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// Toast.makeText(getBaseContext(), "Tab UnSelected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(tabListener));
}
}
这是TabPagerAdapter.java
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Tab1Activity();
case 1:
return new Tab2Activity();
}
return null;
}
@Override
public int getCount() {
//No of Tabs
return 2;
}
}
这是Tab1Activity.java
public class Tab1Activity extends Fragment {
ViewPager tabPager;
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main2, container, false);
tabPager = (ViewPager) container.findViewById(R.id.pager);
List<String> dataList = prepareData();
ListView listView = (ListView) view.findViewById(R.id.list_View1);
ListRowAdaptor listAdapter = new ListRowAdaptor(inflater.getContext(), R.layout.list_row, dataList);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("newData", (ArrayList<java.lang.String>) prepareNewData());
Tab2Activity myDetailFragment = new Tab2Activity();
myDetailFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.pager, myDetailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
tabPager.setCurrentItem(1);
}});
return view;
}
public List<String> prepareData(){
List<String> list = new ArrayList<String>();
list.add("Item1");
list.add("Item2");
list.add("Item3");
list.add("Item4");
list.add("Item5");
list.add("Item6");
return list;
}
public List<String> prepareNewData(){
List<String> list = new ArrayList<String>();
list.add("New Item1");
list.add("New Item2");
list.add("New Item3");
list.add("New Item4");
list.add("New Item5");
list.add("New Item6");
return list;
}
}
这是Tab2Activity.java
public class Tab2Activity extends Fragment {
ListRowAdaptor listAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main2, container, false);
Bundle bundle = getArguments();
ListView listView = (ListView) view.findViewById(R.id.list_View1);
List<String> dataList = prepareData();
listAdapter = new ListRowAdaptor(inflater.getContext(), R.layout.list_row, dataList);
if(bundle != null){
List<String> newList = bundle.getStringArrayList("newData");
listAdapter.clear();
listAdapter.addAll(newList);
listAdapter.notifyDataSetChanged();
}
listView.setAdapter(listAdapter);
return view;
}
public List<String> prepareData(){
List<String> list = new ArrayList<String>();
list.add("Item11");
list.add("Item22");
list.add("Item33");
list.add("Item44");
list.add("Item55");
list.add("Item66");
return list;
}
}
这是activity_main2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list_View1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这是tab_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这是list_row.xml
<?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="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"`enter code here`
android:textColor="#343434"
android:textSize="15sp" />
</RelativeLayout>*
答案 0 :(得分:0)
可能错误是您设置了适配器
listView.setAdapter(listAdapter);
通知数据集已更改
listAdapter.notifyDataSetChanged();
您可以尝试在“通知”句子之前写下这些句子。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main2, container, false);
Bundle bundle = getArguments();
ListView listView = (ListView) view.findViewById(R.id.list_View1);
List<String> dataList = prepareData();
listAdapter = new ListRowAdaptor(inflater.getContext(), R.layout.list_row, dataList);
if(bundle != null){
List<String> newList = bundle.getStringArrayList("newData");
listAdapter.clear();
listAdapter.addAll(newList);
listView.setAdapter(listAdapter); // Update the adapter before notify
listAdapter.notifyDataSetChanged();
}
return view;
}
让我知道它是否有效。祝你好运。
答案 1 :(得分:0)
将您的代码更改为
if(bundle != null)
{
List<String> newList = bundle.getStringArrayList("newData");
dataList.clear();
dataList.addAll(newList);
listAdapter.notifyDataSetChanged();
}
答案 2 :(得分:0)
notifyDataSetChanged()
方法通知观察者您的适配器。
例如,如果您正在使用gridview,则可以调用invalidateViews()
方法,这将导致视图重建并重新绘制。