gpt4 book ai didi

java - 上下文菜单显示正确的数据,但 ListView 不显示

转载 作者:行者123 更新时间:2023-12-02 04:37:12 25 4
gpt4 key购买 nike

我有一个非常奇怪的问题,过去几个小时我一直在尝试解决,但我找不到问题所在。我有一个包含名称和少量文本的 SQLite 数据库。一个 ListView ,其中填充了其中的名称,当我单击 ListView 时,我会看到与名称相关的文本。但我也有一个“添加到 Collection 夹”,当我将某些内容添加到我的 Collection 夹列表时,它会显示在另一个 Activity 上。在新的/最喜欢的 ListView 中显示的信息是正确的(名称、标题和图像),但是当我单击它时,显示的数据是错误的。

我的原始 ListView 中有 10 行,而在我的“最喜欢的” ListView 中,它的顺序与第一个 ListView 中的顺序相同。例如,如果我添加一部电影……《卡萨布兰卡》位于我原始列表中的位置 7,它会将其自身放置在我的新 ListView 中的第一个位置(位置 0),并显示正确的 Actor 、封面和作者。但是当我单击它以查看更多信息(开始新 Activity )时。它仅显示原始 ListView 中的信息以及位置 0 上的电影信息。

但是如果我单击上下文菜单来检查它。它显示了我想查看的所有正确数据。这太奇怪了,我希望有人知道问题出在哪里?我已经尝试了很多不同的组合和解决方案与数组和字符串/字符串生成器等。这是我来这里之前写的最后一个代码。

 public class Favorites extends ActionBarActivity {

DatabaseHelper dbHelper;
ListView favoritesListView;
Cursor myCursor;
String[] myStringArray = new String[3];
ListViewCursorAdapter myFavsAdapter;
String openBookCmeny = "Öppna boken";
String rmvFavsCmeny = "Ta bort från dina favoriter";
String webSiteCmeny = "Klicka här för mer information på bokens hemsida";
String showWebSiteCmeny;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);

dbHelper = DatabaseHelper.getInstance(this);
myCursor = dbHelper.visaFavoriter();
dbHelper.getWritableDatabase();
myFavsAdapter = new ListViewCursorAdapter(this, myCursor);
favoritesListView = (ListView)findViewById(R.id.favoritesListView);
favoritesListView.setAdapter(myFavsAdapter);
registerForContextMenu(favoritesListView);


favoritesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor)myFavsAdapter.getItem(view.getId());
myStringArray[1]= c.getString(4);

Intent i = new Intent(Favorites.this, VisaBoken.class);
i.putExtra(null, myStringArray);
startActivity(i);

}
});

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

Cursor c = ((Cursor) myFavsAdapter.getItem(info.position));
String cTitle = c.getString(2);

menu.setHeaderTitle(cTitle);
menu.add(0, v.getId(), 0, openBookCmeny);
menu.add(0, v.getId(), 0, rmvFavsCmeny);
menu.add(0, v.getId(), 0, webSiteCmeny);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

Cursor c = ((Cursor) myFavsAdapter.getItem(info.position));
myStringArray[0] = c.getString(0);
myStringArray[1] = c.getString(4);
showWebSiteCmeny = c.getString(6);

if (item.getTitle() == openBookCmeny){
Intent i = new Intent(Favorites.this, VisaBoken.class);
i.putExtra(null, myStringArray);
startActivity(i);
}

else if (item.getTitle() == rmvFavsCmeny){
dbHelper.removeOneFavorite(myStringArray[0]);
recreate();

}
else if (item.getTitle() == webSiteCmeny){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(showWebSiteCmeny));
startActivity(browserIntent);
}
return super.onContextItemSelected(item);
}

获取 Intent 和信息以显示它的类。

 public class VisaBoken extends ActionBarActivity implements AdapterView.OnItemClickListener {

ActionBarDrawerToggle myDrawerToggle;
DrawerLayout myDrawerLayout;
ListView myDrawerListView;
ArrayList<DrawerMenuName> myDrawerMenuName = new ArrayList<>();
DatabaseHelper dbHelper;
public static final String COL_ID = "_id";
public static String Tag = "checkIDfromDB";
TextView visaBokenTV;
String[] myStringArray = new String[2];
String[] myStringArray2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visa_boken);
visaBokenTV = (TextView)findViewById(R.id.visaBokenTV);
Bundle extras = getIntent().getExtras();

myStringArray = extras.getStringArray(null);
visaBokenTV.setText(myStringArray[1]);

myDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
dbHelper = DatabaseHelper.getInstance(this);
dbHelper.getWritableDatabase();

最佳答案

抱歉,我不完全理解您对问题的解释。但是我看到一个可疑的代码。建议,将 public void onItemClick 中的代码更改为:

Cursor c =  (Cursor)myFavsAdapter.getItem(view.getId());

致:

Cursor c =  (Cursor)myFavsAdapter.getItem( position );

通常,要使用 getItem(),您需要传递 ListView 中的位置,从值 0 开始是顶部。除非为各个 View 对象设置了 ID,否则您不能使用 view.getId(),但我认为您没有这样做,听起来太棘手了。也许您应该发布适配器 (myFavsAdapter) 代码。

让我们先尝试一下,直到我发现其他问题为止。

关于java - 上下文菜单显示正确的数据,但 ListView 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610437/

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