gpt4 book ai didi

android - 从非 fragment Activity 刷新 ListView 或 ViewPager

转载 作者:行者123 更新时间:2023-11-29 17:31:33 25 4
gpt4 key购买 nike

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

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

这是我想要在单击返回或选中复选框时刷新 ListView 的选项卡。

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

这是我的主要 Activity 类

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

最佳答案

您在 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();

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

关于android - 从非 fragment Activity 刷新 ListView 或 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32630467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com