当我使用setOnItemClickListener
获取GridView
返回null
中点击的项目的ID时出现问题。我尝试这样做,因为我想当用户点击一个项目我从db检索数据然后在另一个片段中表示它,所以有一个解决方案来获取id或有另一种方法来处理它。非常感谢。
我使用适配器:
fragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import com.example.mohamed.osarelkheir.Adapters.Section_Adapter;
import com.example.mohamed.osarelkheir.First_Launch;
import com.example.mohamed.osarelkheir.Models.Section_Model;
import com.example.mohamed.osarelkheir.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Sections_Fragment extends Fragment {
private GridView Dep_grid_View; //object from GridView (list) -Session Fragment
private List<Section_Model> Dep_list; //Data Model Object
private Section_Adapter section_adapter; //Adapter Object
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private String Dep_id;
private String item;
private DocumentSnapshot lastVisible; //to print the last ....
//Empty Contractor
public Sections_Fragment() {
}
private FragmentManager fragmentManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.section_fragment, container, false);
Dep_grid_View = view.findViewById(R.id.gridSection);
Dep_list = new ArrayList<>();
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
// myList = Arrays.asList(section_models); //he create its own array list to view it as a templet data cooooool
section_adapter = new Section_Adapter(getContext(), Dep_list);
Dep_grid_View.setAdapter(section_adapter);
//First Query
Query firstQuery = firebaseFirestore.collection("Department");
firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot snapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
// Toast.makeText(getActivity(), "No", Toast.LENGTH_SHORT).show();
for (DocumentChange doc : snapshots.getDocumentChanges()) {
// Toast.makeText(getActivity(), "NoO", Toast.LENGTH_SHORT).show();
if (doc.getType() == DocumentChange.Type.ADDED) {
// Toast.makeText(getActivity(), "No00", Toast.LENGTH_SHORT).show();
Dep_id = doc.getDocument().getId();
Toast.makeText(getActivity(), "Id" + Dep_id, Toast.LENGTH_SHORT).show();
// <<<<<<<<<<<<<GET DATA from DB >>>>>>>>>>>>>>>>>>
Section_Model section_model = doc.getDocument().toObject(Section_Model.class).withId(Dep_id);
//<<<<<<<<<<<<<<<then Put it in object of BlogList >>>>>>>>>>>>>>
Dep_list.add(section_model);
section_adapter.notifyDataSetChanged();
}
}
}
});
Dep_grid_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//it's return null
item = Dep_grid_View.getItemAtPosition(i).toString();
printadminview();
}
});
return view;
}
void printadminview() {
Intent intent = new Intent(getContext(), First_Launch.class);
intent.putExtra("value", 3);
intent.putExtra("Dep_ID", item );
// Toast.makeText(getActivity(), "pos " + pos, Toast.LENGTH_SHORT).show();
getContext().startActivity(intent);
}
}
.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SearchView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/item_background_manage"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:queryHint="search"/>
</LinearLayout>
<GridView
android:id="@+id/gridSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="3dp"
android:layout_margin="8dp" />
</LinearLayout>
适配器类
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.mohamed.osarelkheir.First_Launch;
import com.example.mohamed.osarelkheir.Models.Section_Model;
import com.example.mohamed.osarelkheir.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.List;
public class Section_Adapter extends ArrayAdapter<Section_Model> { //1
private Context mContext;
private List<Section_Model> data;
public Section_Adapter(Context mContext,List<Section_Model> data) {//2
super(mContext,R.layout.item_section,data);
this.mContext = mContext;
this.data = data;
}
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(R.layout.item_section, parent, false);
holder = new ViewHolder();
holder.txtSectionName = row.findViewById(R.id.sectionName);
holder.imageView = row.findViewById(R.id.imageSection);
holder.imageIc = row.findViewById(R.id.ic_sec);
row.setTag(holder);
row.setTag(new Integer(position));
} else {
holder = (ViewHolder) row.getTag();
}
setView(holder, position);
Section_Model model = getItem(position);
String id = model.getId();
Intent intent=new Intent(mContext, First_Launch.class);
intent.putExtra("value",1);
intent.putExtra("DepartmentId", id);
mContext.startActivity(intent);
return row;
}
private void setView(ViewHolder holder, int position) {
Section_Model section_model = data.get(position);
holder.txtSectionName.setText(section_model.getSection_name());
String downlaodUriImage = section_model.getSection_image();
String DownloadUriLogo = section_model.getSection_icon();
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.logo);
Glide.with(mContext).applyDefaultRequestOptions(requestOptions).load(downlaodUriImage).into(holder.imageView);
Glide.with(mContext).load(DownloadUriLogo).apply(requestOptions.override(40,40)).into(holder.imageIc);
}
static class ViewHolder {
TextView txtSectionName;
ImageView imageView;
ImageView imageIc;
}
}
答案 0 :(得分:1)
要仅获取ID,您只需使用以下代码行。
firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String documentId = document.getId();
Log.d(TAG, documentId);
}
}
}
});
除非您需要实时获取数据,否则无需使用addSnapshotListener
。输出将是:
CIcg...
E30F...
rXY9...
如果您需要获取所点击的项目的相应ID,只需使用getItem(position)
;
根据您的评论,最简单的方法是在名为Section_Model
的{{1}}类中添加一个新属性,以便存储每个特定文档的ID。因此,每次添加新文档时,该文档都将包含id。使用id
将返回getItem(position)
类的对象。请参阅以下代码:
Section_Model
因此,Section_Model model = getItem(position);
String id = model.getId();
将返回将该文档添加到数据库时设置的对象的ID。