AdapterView onItemClick函数不起作用

时间:2017-02-03 12:36:33

标签: android listview android-adapterview

这是我的代码,它从FireBase获取一些数据并填充Listview 它工作得很好,它应该打开一个新的Activity,我可以看到数据的细节,但是当我点击Listview项时没有任何反应。

有什么想法吗?

以下是我的Java代码:

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);
    }
}     

以下是我的Adapter类:

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);
        }
    }
}  

1 个答案:

答案 0 :(得分:0)

通过在Adapter类中定义click方法解决了这个问题。

以下是代码:

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);
        }
    } 
}