我的架构的一部分如图所示:
联结表会映射哪些项目属于哪个类别,“特色”列将该项目标记为该类别中的特色。
我需要做的是获取特定类别中所有项目的列表,包括联结表中的“featured”属性,并将该数据传递给gridview。
我已经为private RecyclerView recyclerView;
public static final String PREF_FILE_NAME="testpref";
public static final String KEY_USER_LEARNED_DRAWER="user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private View containerView;
private MichaelAdapter adapter;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
public NavigationDrawerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreference(getActivity(),KEY_USER_LEARNED_DRAWER,"false"));
if (savedInstanceState!=null){
mFromSavedInstanceState=true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout=inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView=(RecyclerView)layout.findViewById(R.id.drawerList);
adapter = new MichaelAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<information> getData() {
List<information> data = new ArrayList<>();
int[] icons = {R.drawable.ic_podcasts, R.drawable.ic_magazine};
String[] titles = {"Podcast", "Magazine"};
for (int i = 0; i< titles.length && i< icons.length; i++) ;
{
information current=new information();
current.iconId=icons[i];
current.title=titles[i];
data.add(current);
}
return data;
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
containerView=getActivity().findViewById(fragmentId);
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.draw_open, R.string.draw_close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!mUserLearnedDrawer){
mUserLearnedDrawer=true;
saveToPreference(getActivity(),KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
if (!mUserLearnedDrawer&& !mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static void saveToPreference(Context context,String preferenceName,String preferenceValue){
SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(preferenceName,preferenceValue);
editor.apply();
}
public static String readFromPreference(Context context,String preferenceName,String defaultvalue){
SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
return sharedPreferences.getString(preferenceName,defaultvalue);
}
表创建了一个模型并尝试实现了提议的解决方案here但是如何访问并将数据传递到gridview中?
类别模型
category_item
类别搜索模型
public function getCategoryItems()
{
return $this->hasMany(CategoryItem::className(), ['category_id' => 'id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])->viaTable('{{%category_item}}', ['category_id' => 'id']);
}
项目模型
public function search($params)
{
$query = Category::find()->with('items')->with('categoryItems');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'type' => $this->type,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
控制器
public function getItemCategories()
{
return $this->hasMany(CategoryItem::className(), ['item_id' => 'id']);
}
public function getCategories()
{
return $this->hasMany(Category::className(), ['id' => 'category_id'])->viaTable('{{%category_item}}', ['item_id' => 'id']);
}
查看
public function actionCategory($id = null)
{
if($id == null) {
$dataProvider = new ActiveDataProvider([
'query' => Category::find()->all(),
]);
return $this->render('category', [
'dataProvider' => $dataProvider,
]);
} else {
$category = Category::findOne($id);
if($category) {
$searchModel = new CategorySearch();
$itemsDataProvider = new ActiveDataProvider([
'query' => $searchModel->search(['CategorySearch'=>['id'=>$id]]),
]);
return $this->render('editCategory', [
'itemsDataProvider' => $itemsDataProvider
'model' => $category,
]);
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
通常,视图文件是试图找到解决方案的混合搭配。 TagFeaturedItem模型来自视图的先前版本,请参阅here
答案 0 :(得分:0)
在我看来,您的项目中可能存在一些我不会探索的设计缺陷;我将讨论如何将相关数据导入gridview的问题。
您可以在gridview中创建自定义列,然后获取相关数据,您可以将这样的内容添加到gridview列数组中:
[
'label'=>'featured',
'format'=>'raw',
'value'=>function($data)
{
$categories = $data->categories;
$content = "";
foreach($categories as $category)
$content .= $category->title."<br/>";
return $content;
}
],
请注意,$data
表示数据提供程序返回的模型,在上面的示例中是项目模型。