网格项按钮不起作用

时间:2017-04-05 19:57:29

标签: java android

我有一个GridView和一个名为MyAdapter的适配器。我在每个网格中的按钮没有显示应该执行的对话框( MyAdapter.java )。这是我的代码,我不确定我在这里缺少什么:

MyAdapter.java

public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;
Button btnRoleReveal;

public MyAdapter(Context context, ArrayList<Players> playerList){
    this.context = context;
    this.playerList = playerList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public int getCount() {
    return playerList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = inflater.inflate(layout_grid_item, null);

    final Button button = (Button) convertView.findViewById(R.id.btnRoleReveal);
    TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
    TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
    final TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context)
                    .setTitle("Test")
                    .setMessage("Test");
        }
    });


    Players p = new Players();
    p = playerList.get(position);
    idTextView.setText("ID: " + String.valueOf(p.getId()));
    nameTextView.setText("Name: " + String.valueOf(p.getName()));
    roleTextView.setText("Role: " + String.valueOf(p.getRole()));

    return convertView;
}


}

RolesFragment.java

public class RolesFragment extends Fragment implements View.OnClickListener {

GridView gridView;
ArrayList<Players> playersList;
MyAdapter adapter;
Button role;

public RolesFragment() {

}

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

    View view = inflater.inflate(R.layout.fragment_viewroles, container, false);

    gridView = (GridView) view.findViewById(R.id.gv_players);
    role = (Button) view.findViewById(R.id.btnRoleReveal);

    DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
    playersList = new ArrayList<Players>();

    playersList = databaseHelper.getPlayers();
    adapter = new MyAdapter(getActivity(), playersList);
    gridView.setAdapter(adapter);
    return view;
}



@Override
public void onClick(View v) {

}
}

1 个答案:

答案 0 :(得分:0)

你错过了对话框上的show()调用:

new AlertDialog.Builder(context)
                    .setTitle("Test")
                    .setMessage("Test")
                    .show();