请查看我的代码。 这段代码实际上是从Firebase获取值并将其附加到网格视图,尽管我发现gridView会使用重复的值进行更新,但是onclick侦听器会正确执行,仅给出唯一的值。
gridView具有重复的值,并且似乎是随机变化的。 如果有人可以通过一些示例工作代码来帮助我,那将很有帮助。有人可以帮我解决这个问题吗?
public class Materials extends Fragment {
private Button btnTEST;
Context context;
CardView cardview;
RelativeLayout.LayoutParams layoutparams;
TextView textview;
RelativeLayout relativeLayout;
GridView grid;
int l=0,fl=0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_materials, container, false);
context = v.getContext();
grid=(GridView)v.findViewById(R.id.grid);
final String[] web=new String[13];
final int[] imageId = new int[13];
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Materials_management_new/Materials/");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (fl == 0) {
for (DataSnapshot snap : dataSnapshot.getChildren()) {
web[l] =snap.getKey().toString();
l++;
fl=1;
}
//= gson.toJson(dataSnapshot);
// Toast.makeText(LoginPage.this, json, Toast.LENGTH_SHORT).show();
}
CustomGrid adapter;
adapter = new CustomGrid(getContext(), web, imageId);
grid.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
// Toast.makeText(getActivity(), "SUccesssssafmlnfka", Toast.LENGTH_SHORT).show();
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
这是我的自定义网格代码
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
答案 0 :(得分:0)
如果要按顺序排列项目列表,则可以使用数据库查询对其进行排序,还可以设置要建立索引的数据库的规则,否则整个安装将在客户端完成。
请参阅对https://firebase.google.com/docs/database/android/lists-of-data?authuser=1中的数据进行排序和过滤