2015-09-17 125 views
1

任何人就這一個熟悉嗎?我有一個活動類,當我從標籤中點擊列表視圖時,它會顯示內容,同時它還有一個複選框(收藏夾/書籤),用於在不同標籤上創建收藏夾列表。問題是當我點擊複選框並返回到選項卡時,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; 
} 
+0

你可能需要更新您的適配器..通知設置變化 –

回答

1

您在getfavoritelist創建光標並把它傳遞到populateviewlist,但不要將其關閉。

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); 
} 

同樣在這裏,你是不是總是關閉你的光標。這取決於if語句的評估。

@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();  

你需要拖動你的代碼並清理它們。

+0

所以我需要關閉onitemclick事件的所有這些?並且必須避免傳遞它? –

+0

你需要學習如何保持你的代碼。這是學習C非常寶貴的地方。如果你打開某些東西,你必須關閉它。所以你需要看看你的程序流程,並確保它無論結果如何都關閉。 –

+0

@RenzRazon它很難在評論中解釋這一點。但我喜歡分開我的功能。我所有的遊標/查詢都是在一個自定義的數據庫管理器類中完成的。您需要了解模塊化和程序流程。 –

1

打電話給你的適配器類的notifyDatasetChanged()方法,發出信號,對於ListView的數據已經改變。當用戶回到列表所在的選項卡時,您可以在onPageSelected()中執行此操作。

+0

嘗試,即,問題是光標()沒有關閉的錯誤。 –

+0

@RenzRazon你不關閉你的光標 –

+0

我很好奇,當你填充一個ListView,我不能關閉嗎?和notifydatasetchanged()時,我不會在tabspager適配器 \t公衆詮釋getItemPosition(Object對象){ \t回POSITION_NONE宣稱這不起作用; \t} –

1

打動你設置的邏輯來的onResume

@Override 
public void onResume(){ 
super.onResume(); 
testConnection(); 
    clickListView(); 
    getFavoriteList(); 
} 

而且還始終貼近光標

0

在標籤之間刷卡時,活動就會被殺死。所以試試這個。

yourViewPager.setOffscreenPageLimit(n-1); 

其中n是您在TabLayout中擁有的選項卡數量。