从非片段活动

时间:2015-09-17 12:36:30

标签: android android-fragments android-listview

有人熟悉这个吗?我有一个活动类,当我从选项卡中单击列表视图时显示内容,它还有一个复选框(收藏夹/书签),可在不同的选项卡上创建收藏夹列表。问题是,当我单击复选框并返回选项卡时,列表视图不会更新。我需要关闭它的应用程序才能显示新内容。这是活动类

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

这是我的主要活动课

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

4 个答案:

答案 0 :(得分:1)

调用适配器类的notifyDatasetChanged()方法,以表示ListView的数据已更改。当用户返回列表所在的选项卡时,您可以在onPageSelected()中执行此操作。

答案 1 :(得分: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();     

您需要浏览代码并清理它们。

答案 2 :(得分:1)

将您的设置逻辑移至onResume

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

并且始终关闭光标

答案 3 :(得分:0)

在标签之间滑动时,活动会被杀死。所以试试吧。

preg_match

其中n是TabLayout中的标签数量。