Android列表视图项单击适配器

时间:2016-08-17 06:26:45

标签: android listview android-fragments

我有一个Fragment类和相应的适配器类。 My Fragment类看起来像这样 -

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        myAdapter = new MyAdapter(getActivity(), favourites);
        msgListView.setAdapter(myAdapter );
        return view;
    }

在MyAdapter中,

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 viewHolder;
        MyClass favouriteMessage =  favouriteMessageList.get(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.favorites_fragment_list, null);
            viewHolder = new ViewHolder1();
            viewHolder.favouriteMessagesList = (TextView)convertView.findViewById(R.id.favourites_list);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder1) convertView.getTag();
        }
        //Update Your Views Here
        viewHolder.favouriteMessagesList.setText(favouriteMessage.getText());

        return convertView;
    }

    public void setFavouritesData(ArrayList<FavouriteMessageWrapper> data) {
        favouriteMessageList = data;
        notifyDataSetChanged();
    }

我想在点击监听器上实现列表视图项。我在哪里实现它?如果我尝试在我的片段的onCreateView中执行它,它就是null。

5 个答案:

答案 0 :(得分:1)

试试这个,

在fragment中设置itemClickListener并按照这样做,

>>> a="D:/R_SVN/hostworkspace/middleware/projects/module/com.ofss.fc.module.ac/src/com/ofss/fc/app\ac\service\writeoffrecovery\ext\WriteoffRecoveryApplicationServiceExtExecutor.java"
>>> b=a.replace('\\','/')
>>> print b
D:/R_SVN/hostworkspace/middleware/projects/module/com.ofss.fc.module.ac/src/com/ofss/fc/appc/service/writeoffrecovery/ext/WriteoffRecoveryApplicationServiceExtExecutor.java

答案 1 :(得分:0)

不要在创建时设置adpter。在onpost方法中获取data.like http方法集适配器之后的setadapter。

答案 2 :(得分:0)

让你的片段实现OnItemSelectedListener并在

之后 msgListView.setAdapter(myAdapter );

中的

onCreateView()

msgListView.setOnItemSelectedListener(this)

答案 3 :(得分:0)

onCreateView()中进行所有初始化:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragementName, container, false);//put fragment name of your 
    msgListView=(ListView)view.findViewById(R.id.listView);
    return view;
}

在您的片段中实施OnItemClickListeneronActivityCreated()安装适配器和itemClickListener中:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    myAdapter = new MyAdapter(getActivity(), favourites);
    msgListView.setAdapter(myAdapter );
    msgListView.setOnItemClickListener(this);
}

答案 4 :(得分:0)

您可以在适配器中实现onClickListener

public View getView(int position, View convertView, ViewGroup parent) {

     viewHolder.favouriteMessagesList.setOnClickListener(new OnClickListener() {  
        public void onClick(View v)
            {
                //perform action
            }
         });

    return convertView;
}

希望它能解决你的问题