任何人就這一個熟悉嗎?我有一個活動類,當我從標籤中點擊列表視圖時,它會顯示內容,同時它還有一個複選框(收藏夾/書籤),用於在不同標籤上創建收藏夾列表。問題是當我點擊複選框並返回到選項卡時,listview不會更新。我需要關閉應用程序才能顯示新內容。這是活動類刷新的ListView或ViewPager從非片段活動
public class DisplayContent extends Activity {
private static final float BYTES_PER_PX = 4.0f;
HashMap<String, List<String>> Content_category;
List<String> Content_list;
ExpandableListView Exp_list;
ContentAdapter adapter;
/* Front Content*/
ImageView image;
TextView name, desc;
CheckBox cb;
Context context;
DbHerbs myDb;
TabTwo repopulate;
String getThis;
long conID;
int imgId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_content);
getActionBar().setDisplayHomeAsUpEnabled(true);
testConnection();
setViewId();
getContent();
}
private void getContent()
{
getThis = getIntent().getStringExtra(TabOne.GET_THIS);
conID = Long.parseLong(getThis);
String conFav = myDb.getFav(conID);
imgId = myDb.getImage(conID);
loadImage();
String conName = myDb.getName(conID);
/*Send to expandable list view*/
String conDesc = myDb.getDescription(conID);
String conUses = myDb.getUses(conID);
String conPartsUsed = myDb.getPartsUsed(conID);
String conApplications= myDb.getApplications(conID);
String conSideEffects= myDb.getSideEffects(conID);
String conAltName = myDb.getAltName(conID);
Content_category = getInfo(conDesc, conUses, conPartsUsed, conApplications, conSideEffects, conAltName);
Content_list = new ArrayList<String>(Content_category.keySet());
adapter = new ContentAdapter(this, Content_category, Content_list);
Exp_list.setAdapter(adapter);
if(conFav.equals("1"))
{
cb.setChecked(true);
}else if(conFav.equals("0"))
{
cb.setChecked(false);
}
closeDb();
name.setText(conName);
}
public void favList(View v)
{
myDb.open();
String fav = myDb.getFav(conID);
myDb.updateFavorite(conID, fav);
Log.d(fav, "nagbago ulit");
// Toast.makeText(this, "Database Updated" + fav, Toast.LENGTH_LONG).show();
myDb.close();
// repopulate.getFavoriteList();
}
這是我想刷新列表視圖時,我點擊回來,或選中複選框的選項卡。
public class TabTwo extends Fragment {
public final static String GET_THIS = "com.thesisdatabase._id";
DbHerbs myDb;
SimpleCursorAdapter myCursorAdapter;
View rootView;
ListView listview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_two, container, false);
testConnection();
clickListView();
getFavoriteList();
return rootView;
}
public void getFavoriteList()
{
String x = "1";
Cursor cursor = myDb.getAllFavorites(x);
populateListView(cursor);
}
private void populateListView(Cursor cursor)
{
//this.getActivity().startManagingCursor(cursor);
// this.getActivity().startManagingCursor(cursor);
String[] fromFieldNames = new String[] {DbHerbs.KEY_NAME, DbHerbs.KEY_DESCRIPTION};
int[] toViewIDs = new int[] {R.id.tvName, R.id.tvDesc};
myCursorAdapter = new SimpleCursorAdapter(this.getActivity(), R.layout.custom_listview, cursor, fromFieldNames, toViewIDs, 0);
ListView listview = (ListView) rootView.findViewById(R.id.listView2);
listview.setAdapter(myCursorAdapter);
}
private void clickListView()
{
ListView listview = (ListView) rootView.findViewById(R.id.listView2);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long dbID) {
Cursor cursor = myDb.getRow(dbID);
String sendThis = String.valueOf(dbID);
if(cursor.moveToFirst()){
/* Transfers the row id of the clicked item into the next class and then displays */
Intent intent = new Intent(getActivity(), DisplayContent.class);
intent.putExtra(GET_THIS, sendThis);
startActivity(intent);
}
cursor.close();
}
});
}
這這裏我MainActivity類別
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
TabTwo tabtwo;
private boolean doubleBackToExitPressedOnce;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "A-Z", "Favorites", "History" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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);
// mAdapter.notifyDataSetChanged();
//tabtwo.getFavoriteList();
}
@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) {
}
和持續適配器類
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new TabOne();
case 1:
return new TabTwo();
case 2:
return new TabThree();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
你可能需要更新您的適配器..通知設置變化 –