Reputation: 62404
I'm trying to create the following screen in my android app:
Except I may need to list up to 9 or more teams. I've been looking at GridView, but I can't find anything in the GridView docs or ListAdapter docs to show that I can do that. Is this possible or am I looking in the wrong place?
Upvotes: 0
Views: 72
Reputation: 1655
Is this possible or am I looking in the wrong place?
It is possible with GridView
, View
, BaseAdapter
and LayoutInflater
. You can inflate the views of the cell (each cell is the View
here). For example:
main.xml
:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- add additional properties as needed --!>
</GridView>
player.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/grid_item_team_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/teamName"
android:layout_marginTop="10dp"
android:textSize="20dp" >
<!-- add or modify the properties as needed --!>
</TextView>
</LinearLayout>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] teamValues;
public LayoutInflater inflater;
public LinearLayout ll;
public ImageAdapter(Context context, String[] teamValues) {
this.context = context;
this.teamValues = teamValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from player.xml
gridView = inflater.inflate(R.layout.player, null);
// set value into textview
ll = (LinearLayout)gridView.findViewById(R.id.linear1);
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_team_name);
textView.setText(teamValues[position]);
String team = teamValues[position];
// add team members here
//
//
//
} else {
gridView = (View) convertView;
}
return gridView;
}
}
GridViewActivity.java
public class GridViewActivity extends Activity {
GridView gridView;
ImageAdapter mImageAdapter;
String[] teamName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
teamName = getTeamName();
mImageAdapter = new ImageAdapter(this, teamName);
gridView = (GridView)findViewById(R.id.gridView1);
registerForContextMenu(gridView);
gridView.setAdapter(mImageAdapter);
}
@Override
public void onCreateContextMenu(ContextMenu iMenu, View iView, ContextMenuInfo iMenuInfo) {
super.onCreateContextMenu(iMenu, iView, iMenuInfo);
iMenu.setHeaderTitle("TEAM NAME");
iMenu.clear();
for (int i = 0; i < nTotalTeam; i++) {
iMenu.add(Menu.NONE, i, Menu.NONE, teamName[i]);
}
}
private void getTeamName() {
//
// getTeamName and return the array of string
//
//
}
}
Upvotes: 1
Reputation: 93614
A GridView is absolutely right here. Each view in a GridView is a View- whatever view you want. And yes, you can programatically add new views to it. THe different cells don't even need to be the same type of View if you don't want them to be- its all very flexible.
Upvotes: 0