只有第一个项目是可点击的ListView onItemClickListener

时间:2017-11-09 22:55:35

标签: android listview

我想创建一个包含其中一些项目的ListView,在特定项目上单击,我想运行需要该项目索引的代码。但是,只有第一个项目是可点击的并且正在被识别,我想知道我的问题是否在我的CustomAdapter中,但是因为我之前从未做过类似的事情,所以我对此没有任何线索。感谢您的帮助。

这是我的CustomAdapter:

public class CustomAdapterGroups extends ArrayAdapter<ModelGroups>{

private final Context context;
private final ArrayList<ModelGroups> modelsArrayList;

public CustomAdapterGroups(Context context, ArrayList<ModelGroups> modelsArrayList)
{
    super(context, R.layout.target_item, modelsArrayList);

    this.context = context;
    this.modelsArrayList = modelsArrayList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    // Inflater creation
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // get rowView from inflater
    View rowView = null;
    if (!modelsArrayList.get(position).isGroupHeader())
    {
        rowView = inflater.inflate(R.layout.target_item, parent, false);

        // get icon, title & counter views from the rotView
        ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon_idea);
        TextView titleView = (TextView) rowView.findViewById(R.id.item_title_group);
        Button colorView = (Button) rowView.findViewById(R.id.item_color_group);
        //TextView counterView = (TextView) rowView.findViewById(R.id.item_counter);

        // set text for textview
        imgView.setImageResource(modelsArrayList.get(position).getIcon());
        titleView.setText(modelsArrayList.get(position).getTitle());
        colorView.setBackgroundColor(modelsArrayList.get(position).getColor());
        //counterView.setText(modelsArrayList.get(position).getCounter());

    }
    else
    {
        rowView = inflater.inflate(R.layout.group_header_item, parent, false);
        TextView titleView = (TextView) rowView.findViewById(R.id.item_header);
        titleView.setText(modelsArrayList.get(position).getTitle());
    }

    return rowView;
}

}

在我的片段(我正在使用选项卡式布局)中包含ListView我创建了一个onItemClickListener,但除了一个Log.e(&#34;&#34;,位置)之外,这个是空的;

我只是想知道我为了获得所有点击项目的位置而无法实现的目标。

编辑:@Brian,这是我的片段,我根据我得到的对象列表在generateData中创建ListView项目。

public class dashboardtab_main extends Fragment implements View.OnClickListener{

ImageButton create_group;
Switch change_mode;
ListView list_groups;
List<Group> userGroups;
PacketBuilder pb = new PacketBuilder();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_dashboardtab_main, container, false);

    userGroups = new ArrayList<Group>();
    userGroups = pb.getUserGroups();

    create_group = (ImageButton) rootView.findViewById(R.id.btn_createGroup);
    change_mode = (Switch) rootView.findViewById(R.id.switch_mode);
    list_groups = (ListView) rootView.findViewById(R.id.lv_groups);

    create_group.setOnClickListener(this);
    change_mode.setOnClickListener(this);
    //list_groups.setOnItemClickListener(this);

    CustomAdapterGroups adapter = new CustomAdapterGroups(getActivity(), generateData());

    list_groups.setAdapter(adapter);

    list_groups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String item = list_groups.getItemAtPosition(position).toString();
            Log.e("Dashboard", item);
        }
    });

    return rootView;



}


private ArrayList<ModelGroups> generateData()
{
    ArrayList<ModelGroups> modelGroupes = new ArrayList<ModelGroups>();
    modelGroupes.add(new ModelGroups("Groups"));

    //for (:) {}
    for(int i=0; i < userGroups.size(); i++) {
        modelGroupes.add(new ModelGroups(R.drawable.avatar_circle_blue_120dp, userGroups.get(i).getName(), Color.parseColor(userGroups.get(i).getColor())));
    }
    return modelGroupes;
}

0 个答案:

没有答案
相关问题