我有一个GridView,它是从我的SQLite数据库中的项目填充的。问题是,当单击GridView上的某个项目时,我无法弄清楚如何显示对话框。我正在使用自定义适配器,我的代码如下(我对android很新,任何帮助/提示将不胜感激):
RolesFragment.java
public class RolesFragment extends Fragment implements View.OnClickListener {
GridView gridView;
ArrayList<Players> playersList;
MyAdapter adapter;
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);
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
playersList = new ArrayList<Players>();
playersList = databaseHelper.getPlayers();
adapter = new MyAdapter(getActivity(), playersList);
gridView.setAdapter(adapter);
addListenerToGrid();
return view;
}
public void addListenerToGrid() {
gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
int action = me.getActionMasked();
String TAG = "Pressed";
float currentXPosition = me.getX();
float currentYPosition = me.getY();
int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
if (action == MotionEvent.ACTION_UP) {
Log.d(TAG, "-");
}
return true;
}
});
}
@Override
public void onClick(View v) {
}
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;
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(R.layout.layout_grid_item, null);
TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);
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;
}
}
DatabaseHelper.java - 相关代码 -
public ArrayList<Players> getPlayers(){
String query = "SELECT * FROM " + TABLE_NAME;
ArrayList<Players> players = new ArrayList<Players>();
SQLiteDatabase database = getReadableDatabase();
Cursor c = database.rawQuery(query, null);
if (c != null){
while (c.moveToNext()) {
int id = c.getInt(c.getColumnIndex(COL_ID));
String name = c.getString(c.getColumnIndex(COL_NAME));
String role = c.getString(c.getColumnIndex(COL_ROLE));
Players player = new Players();
player.setId(id);
player.setName(name);
player.setRole(role);
Log.v("DBHelper:", "Name:, " + name);
Log.v("DBHelper:", "ID:" + id);
Log.v("DBHelper:", "Role:" + role);
players.add(player);
}
}
return players;
}
这个想法是,当一个人点击显示他们的ID和名字的GridView时......它会显示玩家的相关“角色”。
答案 0 :(得分:0)
将此添加到您的片段中:
public class RolesFragment extends Fragment implements OnClickCallback {
MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
adapter = new MyAdapter(getActivity(), playersList,this);
...
return view;
}
@Override
public void onItemClick(int position){
//create a dialog here based on the item selected
//to get the item selected you can do adapter.getItem(position);
}
}
界面本身:
public interface OnClickCallback {
void onItemClick(int position);
}
然后在适配器上:
public class MyAdapter extends BaseAdapter {
Context context;
OnClickCallback callback;
...
public MyAdapter(Context context, ArrayList<Players> playerList,OnClickCallback callback){
this.context = context;
this.callback = callback;
this.playerList = playerList;
inflater = (LayoutInflater) c
Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.layout_grid_item, null);
...
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callback.OnItemClick(getAdapterPosition());
}
};
return convertView;
}
}