单击GridView按钮,在对话框中膨胀INVISIBLE TextView

时间:2017-04-03 21:34:33

标签: java android xml gridview

我有一个带有自定义适配器的GridView,它从SQLite数据库填充GridView。

当用户点击按钮时,我想要弹出一个对话框,并显示已存在的TextView中的文本,该TextView设置为INVISIBLE。

有什么办法可以实现吗?我对android和编程很新。

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) {

}
}

MyAdapter.java

package com.example.tuss.mafia;



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) {
    ViewHolder holder;
    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;
}



}

layout_grid_item.xml - 按钮/隐藏文字视图的位置。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0004"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_player_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:text="ID" />

<TextView
    android:id="@+id/tv_player_name"
    android:textAlignment="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/holo_blue_bright"
    android:text="name" />

<TextView
    android:id="@+id/tv_player_role"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:clickable="true"
    android:visibility="invisible"
    android:text="role"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="View Role"
    android:id="@+id/btnRoleReveal"
    />

  </LinearLayout>

1 个答案:

答案 0 :(得分:0)

在OnCreateView之后的行:

gridView.setAdapter(adapter);

添加以下代码:

gridView.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {


                        Toast.makeText(
                            getApplicationContext(),
                            ((TextView) v.findViewById( R.id.tv_player_role ))
                                    .getText(), Toast.LENGTH_SHORT).show();

                }
            });

您可以创建对话框而不是创建Toast。