我有一个如下的Sqlite表。
我已经用rawQuery列出了一个列表:SELECT HEADER FROM MyData GROUP BY HEADER
ArrayAdapter arrayAdapter;
ArrayList<String> listItem = new ArrayList<>();
final ListView listView = (ListView) view.findViewById(R.id.list);
SQLiteDatabase sqLiteDatabase =
SqliteDatabase.getInstance(this.getContext()).getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT HEADER FROM MyData GROUP BY
HEADER", new String[]{});
if (cursor.getCount() == 0) {
Toast.makeText(getActivity(), "No Data to show",
Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
listItem.add(cursor.getString(0));
}
arrayAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, listItem);
listView.setAdapter(arrayAdapter);
列表显示如下:
Letter
Number
Word
我的问题是,当我单击列表项Suppose(Letter
)时,SwipeViews显示了A
,
当点击Number
时,SwipeViews显示B
和
当点击Word
时,SwipeViews显示C
我想得到when I will click on Letter, SwipeViews will show only A,B,C
when I will click on Number, SwipeViews will show only 1,2,3
when I will click on Word, SwipeViews will show only Apple, Orange and Banana
这是OnClickListener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l) {
Intent intent=new Intent(getActivity(), SwipeNav.class);
intent.putExtra(ID_EXTRA, String.valueOf(l));
startActivity(intent);
}
});
这是来自寻呼机适配器
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private Context context;
public MyPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context= context;}
@Override
public Fragment getItem(int i) {
ArrayList<String> listItem = new ArrayList<String>();
SQLiteDatabase sqLiteDatabase =
SqliteDatabase.getInstance(context).getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT NAME FROM MyData", new
String[]{});
if (cursor.getCount() == 0) {
} else {
while (cursor.moveToNext()) {
listItem.add(cursor.getString(0));
}
}
Object[] mStringArray = listItem.toArray();
Fragment fragment = new AFragment();
Bundle args = new Bundle();
args.putString(AFragment.ARG_OBJECT, (String)mStringArray[i]);
fragment.setArguments(args);
return fragment;
}
在SwipeNav活动中,我使用下面的方法获取价值
if (getIntent().hasExtra(ListNavAdapter.ID_EXTRA)){
String myInt = getIntent().getStringExtra(ListNavAdapter.ID_EXTRA);
listSwipePagerAdapter = new ListSwipePagerAdapter(getSupportFragmentManager(),this);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(listSwipePagerAdapter);
答案 0 :(得分:1)
您的问题似乎是您使用的是 l ,希望它是ID。除非使用CursorAdapter,否则它将是位置(除了long以外,它与传递的第3个参数相同)。
您要传递的是单击的视图的值(字母编号或Word),然后使用该值选择相应的行。
因此,您想要的不是intent.putExtra(ID_EXTRA, String.valueOf(l));
intent.putExtra(ID_EXTRA, ((String)adapterView.getItem(i)));
或
intent.putExtra(ID_EXTRA,((TextView) view).getText().toString());
请注意列表中的值,即Header列的不同值(根据示例数据的字母,数字或单词)。
要获取所需的值,您需要在SwipeNav活动中进行查询,例如
从MyData WHERE HEADER中选择名称='the_value_from_ID_EXTRA'
,其中the_value_from_ID_EXTRA将替换为从意图附加中提取的值,并使用传递给SwipeNav活动的键ID_EXTRA。
注意。以上代码为原则代码,尚未经过测试或运行,因此可能会有一些错误。