2017-02-03 17 views
0

これは私のコードです.FireBaseからのデータがいくつか取得されていて、Listviewに値が設定されています。
完全に動作し、データの詳細を見ることができる新しいアクティビティを開くはずですが、Listviewアイテムをクリックすると何も起こりません。AdapterViewのonItemClick関数が機能しません

アイデア?この問題は、アダプタクラスのクリックメソッドに定義することで解決解決した

public class ResearchListArrayAdapter extends ArrayAdapter<Research> { 

    private Context context; 
    private List<Research> researches; 

    public ResearchListArrayAdapter(Context context, int resource, ArrayList<Research> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.researches = objects; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     Research research = researches.get(position); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View view  = inflater.inflate(R.layout.research_list_row, null); 
     setText(view, research.getProjectName(),   R.id.researchCaption,  0); 
     return view; 
    } 

    private void setText(View view, String content, int id, int length) { 
     TextView textView = (TextView) view.findViewById(id); 
     if (length == 0) { 
      textView.setText(content); 
     } else if (content == null && content.length() >= length) { 
      String contentTrim = content.substring(0, length) + "..."; 
      textView.setText(contentTrim); 
     } else { 
      textView.setText(content); 
     } 
    } 
} 

答えて

0

public class PojectsFragment extends Fragment implements AdapterView.OnItemClickListener, ChildEventListener { 

    Chronometer myChronometer; 
    private static String DATA_CHILD  = "projects"; 
    private FirebaseAuth mFirebaseAuth; 
    private DatabaseReference mDatabase; 
    private View    rootView; 
    private ListView listView; 
    private ArrayList<Research> researches = new ArrayList<>(); 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     rootView = inflater.inflate(R.layout.activity_pojects, container, false); 
     myChronometer = (Chronometer) rootView.findViewById(R.id.researchesChronometer); 
     myChronometer.start(); 
     mDatabase  = FirebaseDatabase.getInstance().getReference(); 
     mFirebaseAuth = FirebaseAuth.getInstance(); 
     FloatingActionButton fb =(FloatingActionButton) rootView.findViewById(R.id.fab_new_project); 
     fb.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getActivity(), NewResearch.class); 
       intent.putExtra("userId", mFirebaseAuth.getCurrentUser().getUid()); 
       startActivity(intent); 
      } 
     }); 

     if (mFirebaseAuth.getCurrentUser() == null) { 
      loadLogInView(); 
     } 
     init(); 
     return rootView; 
    } 



    private void init() { 
     final ArrayAdapter<Research> adapter = new ResearchListArrayAdapter(getContext(), 0, researches); 
     listView = (ListView) rootView.findViewById(R.id.projectsListView); 
     listView.setAdapter(adapter); 
     mDatabase.child(DATA_CHILD).addChildEventListener(this); 
     listView.setOnItemClickListener(this); 
    } 



    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Research research = researches.get(position); 
     Intent intent = new Intent(getActivity(), NewsFeedActivity.class); 
     intent.putExtra("research", research); 
     if (research.getCreatorId() != null && research.getCreatorId().equals(mFirebaseAuth.getCurrentUser().getUid().toString())) { 
      intent.putExtra("editable", true); 
     } 
     startActivity(intent); 
    } 

    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
     // String researchId  = dataSnapshot.getKey().toString(); 

     Research research = dataSnapshot.getValue(Research.class); 

     String creatorId = research.getCreatorId(); 
     String usertID = mFirebaseAuth.getCurrentUser().getUid(); 

     if(creatorId.equals(usertID)){ 
      researches.add(research); 
      } 
      ((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged(); 

    } 

    @Override 
    public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

    } 

    @Override 
    public void onChildRemoved(DataSnapshot dataSnapshot) { 
     // adapter.remove((String) dataSnapshot.child("title").getValue()); 
    } 

    @Override 
    public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 

    } 


    private void loadLogInView() { 
     Intent intent = new Intent(getActivity(), LoginActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     startActivity(intent); 
    } 
}  

以下は私のアダプタクラスです:

以下は私のJavaコードです。ここで

はコードです:

public class ResearchListArrayAdapter extends ArrayAdapter<Research> { 


    private Context context; 
    private List<Research> researches; 
    private FirebaseAuth mFirebaseAuth; 
    private FirebaseDatabase mDatabase; 
    private FirebaseStorage storage; 
    private static final String DEFAULT_RESEARCH_IMAGE_URL = "https://firebasestorage.googleapis.com/v0/b/bilimsel-surecler.appspot.com/o/images%2Fdefault%2Fstem.jpg?alt=media&token=b2608f1d-befe-46bd-97fa-b59544f8b210"; 


    public ResearchListArrayAdapter(Context context, int resource, ArrayList<Research> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.researches = objects; 
     mFirebaseAuth = FirebaseAuth.getInstance(); 
     mDatabase = FirebaseDatabase.getInstance(); 
     storage = FirebaseStorage.getInstance(); 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     final Research research = researches.get(position); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View view  = inflater.inflate(R.layout.research_list_row, null); 
     setText(view, research.getProjectName(), R.id.researchCaption,0); 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Activity activity = (Activity) getContext(); 
       Intent intent = new Intent(activity, NewsFeedActivity.class); 
       intent.putExtra("research", research); 
       if (research.getCreatorId() != null && research.getCreatorId().equals(mFirebaseAuth.getCurrentUser().getUid().toString())) { 
        intent.putExtra("editable", true); 
       } 

       activity.startActivity(intent); 
      } 
     }); 
     setDeleteButton(view,research); 
     setEditButton(view,research); 
     return view; 
    } 

    private void setText(View view, String content, int id, int length) { 
     TextView textView = (TextView) view.findViewById(id); 
     if (length == 0) { 
      textView.setText(content); 
     } else if (content == null && content.length() >= length) { 
      String contentTrim = content.substring(0, length) + "..."; 
      textView.setText(contentTrim); 
     } else { 
      textView.setText(content); 
     } 
    } 
} 
関連する問題