listfragment中listview上的项目未响应

时间:2017-05-16 12:43:29

标签: android android-studio

请问我有问题。我打算在点击listview上的项目时显示一个祝酒词。但这些项目并没有显示甚至点击它们的响应。 这是代码:

package com.ejikedozie.pcsolutions;

import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v7.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.ejikedozie.pcsolutions.R;

public class OneFragment extends ListFragment implements AdapterView.OnItemClickListener {
    public OneFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.Planets, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
        switch (position){
            case 1:
                Toast.makeText(getActivity(), "Item: ", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
        }
    }
}

任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:1)

您应该实现ListView.OnItemClickListener而不是AdapterView.OnItemClickListener。

答案 1 :(得分:0)

我刚刚删除了开关盒情况,现在所有项目点击我得到了位置。  请再次检查

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(getActivity(), "Clicked item: " + position, Toast.LENGTH_SHORT).show();
}