I want to implement a grid Adapter that receives an ArrayList of Element2S, where Element2S is a custom object with two String (Element2S(String a, String b)
The GridAdapter should use the Element2S ArrayList to populate the Grid.
the first String of Element2S object should be the first column element, the second String of Element2S the second element
My GridAdapter is:
public class GridViewAdapterC2 extends BaseAdapter {
private ArrayList<Element2S> itemList;
private Activity activity;
public GridViewAdapterC2(Activity activity,
ArrayList<Element2S> itemList) {
super();
this.itemList = itemList;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
#Override
public Element2S getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
public TextView txtViewColumn1;
public TextView txtViewColumn2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.grid_row_2c, null);
view.txtViewColumn1 = (TextView) convertView
.findViewById(R.id.col1);
view.txtViewColumn2 = (TextView) convertView
.findViewById(R.id.col2);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
String textCoilumn1 = itemList.get(position).getA();
String textCoilumn2 = itemList.get(position).getB();
view.txtViewColumn1.setText(textCoilumn1);
view.txtViewColumn2.setText(textCoilumn2);
return convertView;
}
}
Unfortunately when in the main I try to use this adapter with
gridAdapter = new GridViewAdapterC2(this, myItemArrayList);
mygrid= (GridView) findViewById(R.id.myGridView);
mygrid.setAdapter(gridAdapter);
The result is wrong.
My gridRow layout is
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:layout_gravity="left">
<TextView
android:id="#+id/col1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left|start"
android:padding="5dp"
android:textColor="#color/light_sky"
android:textSize="12sp" />
<TextView
android:id="#+id/col2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left|start"
android:padding="5dp"
android:textColor="#color/green_2"
android:textSize="12sp" >
</TextView>
</TableRow>
</TableLayout>
The myGridView layout in the main activity is
<GridView
android:id="#+id/myGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:gravity="center_horizontal"
android:numColumns="2" >
</GridView>
I cannot figure what is wrong.
If my Elements2S arraylist is
1st element2S: (1,23a)
2nd element2S: (2,15b)
3rd element2S: (3,22c)
The Grid shows
1 2
3
But the right output is
1 23a
2 15b
3 22c
I think the ListView is better than GridView in this situation.
And I think the LinearLayout is better than TableLayout.
Your output is wrong because some region of your grid's cell is covered by others. Try to resize your cell and grid.
Related
My listview has three textview and one checkbox in horizontal order.
My listview xml code
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEXT"
android:id="#+id/textView1"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEXT"
android:id="#+id/textView2"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEXT"
android:id="#+id/textView3"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="5" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkBox1"
android:focusable="false"
android:clickable="false"
android:layout_gravity="right"
android:layout_marginRight="10dp"/>
and MainActivity.java code
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View view, int position, long id) {
Object vo = (Object)adapterView.getAdapter().getItem(position);
Log.d(TAG, String.valueOf(vo));
}
});
The log output for String.valueOf(vo) is
sn.studyroom.ListViewItem#f085546
How can I get each of the three Textview values?
If you catch the clicked object right, you must convert it to TextView, and then get your TextView's text like this:
String vo = ((TextView)adapterView.getAdapter().getItem(position)).getText().toString();
Log.d(TAG, vo);
OR
Go your adapter class and create an interface to get clicked item's values.
You can use adapter for accessing your text fields and check box. Pass your list to a adapter like this:
CustomListAdapter adapter = new CustomListAdapter (context, yourList);
yourListView.setAdapter(adapter);
Create an adapter like this.
public class CustomListAdapter extends BaseAdapter {
Context context;
List<ListObject> yourList;
public CustomListAdapter (Context context, List<ListObject> yourList) {
this.context = context;
this.yourList= yourList;
}
public void refreshAdapter(List<ListObject> yourList){
this.yourList= yourList;
notifyDataSetChanged();
}
private static class ViewHolder {
TextView textView1, textView2, textView3;
CheckBox checkBox1;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return yourList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return yourList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.your_adapter_layout, null);
holder = new ViewHolder();
holder.textView1= convertView.findViewById(R.id.textView1);
holder.textView2= convertView.findViewById(R.id.textView2);
holder.textView3= convertView.findViewById(R.id.textView3);
holder.checkBox1= convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//TODO: you can do whatever you want to do here
return convertView;
}
}
If you want to retrive list data then i want to know about to set data. Are you set model and add in adapter or taking static string arraylist? so let me know then i will suggest better way to do that.
I am creating custom grid. Each row has 1 button and 2 textviews. I am trying to assign values/text to each programmatically. I am able to set text for both the text views but, findviewbyId for button returns null.
Grid is like:
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#FFFFFF"
android:padding="5dip"
android:id="#+id/gvMYActivitySearch" />
Row layout is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnHistory"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="#drawable/button_background"
android:text="History"/>
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="35dp" />
<TextView
android:id="#+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="35dp" />
</LinearLayout>
Custom grid java file is like
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] names;
private final String[] Contact;
private final String[] ID;
public CustomGrid(Context c,String[] names,String[] Contact, String[] ID ) {
mContext = c;
this.Contact = Contact;
this.names = names;
this.ID = ID;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//grid = new View(mContext);
grid = inflater.inflate(R.layout.myactivity_singlerow, null);
} else {
grid = (View) convertView;
}
TextView tvName = (TextView) grid.findViewById(R.id.tvName);
TextView tvContact = (TextView) grid.findViewById(R.id.tvContact);
Button btnHistory = (Button) grid.findViewById(R.id.btnHistory);
tvName.setText(names[position]);
tvContact.setText(Contact[position]);
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
new MainActivity().testButton(view.getId());
}
}
);
}
return grid;
}
When I try to debug, I can see for the first time when position is '0', btnHistory is not null. But after that when position is 1, btnHistory is null.
What could possibly go wrong? When both the textviews are not null, why btnHistory is null?
Comment to improve your code:
callback somewhere outside your getView
I mean this:
public class CustomGrid extends BaseAdapter implements OnClickListener {
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setTag((Integer)view.getId());
btnHistory.setOnClickListener(this);
}
public void onClick(View v) {
new MainActivity().testButton(v.getTag());
}
}
I'm trying to create a custom gridview inside a fragment that should look something like this:
http://imgur.com/6fEFYTN
So I created a FragmentLayout that contains the image and the label:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
/>
<TextView
android:id="#+id/grid_label"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="175dp"
android:background="#drawable/label"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold" />
The adapter that manages the FragmentLayout looks like this:
public class CustomGridAdapter extends BaseAdapter{
private Context mContext;
private final String[] label;
private final int[] imgId;
public CustomGridAdapter(Context c,String[] label,int[] imgId ) {
mContext = c;
this.imgId = imgId;
this.label = label;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.image_button_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_label);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(label[position]);
imageView.setImageResource(imgId[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
The fragment grid is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/form_grid"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
/>
</LinearLayout>
I'm creating this from the onCreateView method, I'm using onclickListener so the images work as buttons and display a toast when pressed:
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout_softdrinks, container, false);
GridView grid = (GridView) rootView.findViewById(R.id.form_grid);
CustomGridAdapter adapter = new CustomGridAdapter(rootView.getContext(), label, imgId); //label and imgId are arrays containing strings and int respectively
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(inflater.getContext(), "You Clicked at " +label[+ position], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
The problem is that when I run the code, the screen is blank, so the gridview is not being populated.
Any advice on this?
First of all you will need to provide a payload in imgId array and label array.
Then your grid view won't inflate as well because you are returning 0 in your getCount method!
To inflate your layout replace in your getCount method
Return 0;
With
Return imgId.length;
The getCount method tells the base adapter how much images or strings(..data) you have in your array which you want to inflate in the grid view. If you returning 0, base adapter thinks that there are 0 data to inflate.
I have to create UI of an application as shelf which holds magazines. I am using customize grid view, with grid element consisting of thumbnail of magazine having shelf as background image.
shelf.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4" >
</GridView>
</LinearLayout>
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grid">
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:src="#drawable/book1" />
</RelativeLayout>
LibraryActivity.java
public class LibraryActivity extends Activity {
GridView grid = null;
ArrayList<Integer> image = new ArrayList<Integer>();
ImageView thumbnail;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.shelf);
image.add(R.drawable.book1);
image.add(R.drawable.book2);
image.add(R.drawable.book3);
image.add(R.drawable.book4);
System.out.println("before adapter");
grid = (GridView) findViewById(R.id.gridView1);
CustomAdapter adapter = new CustomAdapter(this, R.layout.item);
grid.setAdapter(adapter);
}
public class CustomAdapter extends BaseAdapter {
Context mContext = null;
int mitem = 0;
LayoutInflater mInflater = null;
public CustomAdapter(Context context, int item) {
this.mContext = context;
this.mitem = item;
this.mInflater = (LayoutInflater) context
.getSystemService(LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return image.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
System.out.println("****CHECKING 2**********");
convertView = mInflater.inflate(mitem, null);
System.out.println("****CHECKING 3**********");
thumbnail = (ImageView) convertView
.findViewById(R.id.imageView1);
System.out.println("****CHECKING 3**********");
}
thumbnail.setImageResource(image.get(position));
return convertView;
}
}
}
But using this code the shelf with magazine is only being displayed. I want to display empty shelf to occupy whole screen of device. How can i do that??
Is there any alternate way to implement the shelf??
There's a great project called Shelves made by Romain Guy from Google. You should check it out.
I want to add button to each row of my listview. I created an XML file called row.xml in my layout folder and added two textviews and a button in that file. But when a button is added, I am unable to click the item of listview. I'm only able to click the button. Here is row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/text11"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/text2"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="#000000"
/>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
I want to refer to textviews and button in my activity. Please help me and suggest some ideas.
I had a similar issue. The simple trick is to add android:focusable="false" to your Button.
You can use a custom adapter (extending an array adapter is fairly simple). In the getView method, set a onClickListener on your TextView, this way both your button and the other parts of the ListItem will respond to touch.
you must add focusable="false" and you can
<Button
android:id="#+id/bt_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
**android:focusable="false"** />
in your adapter
public class MyAdapter extends BaseAdapter {
private Context context;
private List< Objet > objects;
private OnClickListener listener;
public MyAdapter(Context context, List<Objet> objects,
OnClickListener listener) {
this.context = context;
this.objects = objects;
this.listener = listener;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = ((Activity) context)
.getLayoutInflater();
convertView = infalInflater.inflate(R.layout.my_line_list, null);
}
Button bt_do=(Button)convertView.findViewById(R.id.bt_do);
bt_do.setOnClickListener(listener);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
and in your activity create un adapter and implement listener of button.