将列表视图的点击值传递给具有共享首选项的其他活动

时间:2019-06-09 05:12:11

标签: java android listview sharedpreferences

我有三个活动。

我想将点击数据从第一个活动传递到第三个活动。

第一(点击)>第二(点击)>第三

我的问题是:在SwipeNavAdapter

String data1 = sharedPreferences.getString(ListNavAdapter.ID_EXTRA, "")未显示ListNavAdapter中的列表,但显示了CourseAdapter中的列表。我想从ListNavAdapter中的SwipeNavAdapter中单击列表项。

第一:ListNavAdapter

SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(this.getContext()).getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT CHAPTER FROM BOOK GROUP BY CHAPTER order by _id", new String[]{});

 if (cursor.getCount() == 0) {
 //
 } else {
     while (cursor.moveToNext()) {
         listItem.add(cursor.getString(0));
     }
     arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listItem);
     listView.setAdapter(arrayAdapter);
 }
listView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
         String achal=(String)adapterView.getItemAtPosition(i);
         SharedPreferences.Editor editor = sharedPreferences.edit();
         editor.putString(ID_EXTRA, achal);
         editor.apply();
         Intent intent=new Intent(getActivity(), Course.class);
         intent.putExtra(ID_EXTRA, String.valueOf(l));
         startActivity(intent);
     }
 });

第二个:CourseAdapter

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
String  data = sharedPreferences.getString(ListNavAdapter.ID_EXTRA, "") ;
SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(this.getContext()).getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT TEXT_NO FROM BOOK WHERE CHAPTER='" + data + "' ORDER BY _id", new String[]{});

if (cursor.getCount() == 0) {
   //
} else {
    while (cursor.moveToNext()) {
        listItem.add(cursor.getString(0));
    }
    arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listItem);
    listView.setAdapter(arrayAdapter);
}
listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
            String achal1=(String)adapterView.getItemAtPosition(i);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(ID_EXTRAA, achal1);
            editor.apply();
            Intent intent=new Intent(getActivity(), SwipeNav.class);
            intent.putExtra(ID_EXTRAA, String.valueOf(l));
            startActivity(intent);
        }
    });

第三:SwipeNavAdapter

@Override
public Fragment getItem(int i) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String  data1 = sharedPreferences.getString(ListNavAdapter.ID_EXTRA, "") ;
    System.out.println(data1);
    ArrayList<String> listItem1 = new ArrayList<String>();
    Cursor cursor1 = sqLiteDatabase.rawQuery("SELECT TEXT FROM BOOK WHERE CHAPTER='" + data1 + "'", new String[]{});
    if (cursor1.getCount() == 0) {
        ///
    } else {
        while (cursor1.moveToNext()) {
            listItem1.add(cursor1.getString(0));
        }
    }
    Object[] mStringArray1 = listItem1.toArray();
    Fragment fragment = new AFragment();
    Bundle args = new Bundle();
    assert mStringArray1 != null;
    args.putString(AFragment.ARG_OBJECT1, (String)mStringArray1[i]);
    fragment.setArguments(args);
    return fragment;
}


@Override
public int getCount() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String  data1 = sharedPreferences.getString(ListNavAdapter.ID_EXTRA, "") ;
    SQLiteDatabase sqLiteDatabase = SqliteDatabase.getInstance(context).getWritableDatabase();
    ArrayList<String> listItem = new ArrayList<>();
    Cursor cursor = sqLiteDatabase.rawQuery("SELECT TEXT FROM BOOK WHERE CHAPTER='" + data1 + "'", new String[]{});
    while (cursor.moveToNext()) {
        listItem.add(cursor.getString(cursor.getColumnIndex("TEXT")));
    }
    return listItem.size();
}

1 个答案:

答案 0 :(得分:1)

  

我的问题是:在SwipeNavAdapter中

     

字符串数据1 = sharedPreferences.getString(ListNavAdapter.ID_EXTRA,   “”)不显示ListNavAdapter中的列表,但显示中的列表   CourseAdapter。我想从ListNavAdapter获得单击的列表项   在SwipeNavAdapter中。

我假设您正在使用SharedPreferencesCourseAdapter中使用的相同键覆盖ListNavAdapter中的值。确保ID_EXTRA中的ListNavAdapter键和ID_EXTRAA中的CourseAdapter键。

如果将值添加到sharedPreference的同一键下,则该值将被覆盖。