Android GridMenu layout - images not showing up - android

I am creating a simple app for my own use. I want to have a simple menu using GridLayout, using arrays and ArrayAdapter.
Here is my java code:
package com.jlo.love;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class Love extends Activity implements OnItemClickListener{
/** Called when the activity is first created. */
GridView menu;
private String[] menu_text = {
"Location",
"Rotate",
"Gallery",
"View",
"My Places",
"Search"};
Integer[] menu_icon = {
android.R.drawable.ic_menu_mylocation,
android.R.drawable.ic_menu_rotate,
android.R.drawable.ic_menu_gallery,
android.R.drawable.ic_menu_view,
android.R.drawable.ic_menu_myplaces,
android.R.drawable.ic_menu_search};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
menu = (GridView)findViewById(R.id.Menu);
menu.setOnItemClickListener(Love.this);
menu.setAdapter(new MenuItem(Love.this, R.layout.menu_item, menu_text));
}
public class MenuItem extends ArrayAdapter<String> {
public MenuItem(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.menu_item, parent, false);
TextView tv=(TextView)row.findViewById(R.id.text);
ImageView iv = (ImageView)row.findViewById(R.id.Menu);
tv.setText(menu_text[position]);
iv.setImageResource(menu_icon[position]);
return row;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}//end class
these are my XMLs
menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:maxLines="2"
android:textColor="#000"
android:drawablePadding="5dp"
android:minLines="2"
android:background="#null">
</TextView>
My second XML - menu.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="25dp"
android:horizontalSpacing="10dp"
android:columnWidth="70dp"
android:background="#ffffff"
android:stretchMode="columnWidth"
android:gravity="center_horizontal|top"
android:scrollbars="none"
android:paddingTop="10dp">
</GridView>
I have the 6 icons, on all the 3 drawable folders.
When i launch it on the simulator, this is what i have-
the text shows up, but no images icon show up.
Screenshot
Could anyone point me in the right direction..

If I understood everything correctly you wan some icons to appear with the text that is showing in the grid view. But I noticed that your menu item xml does not have an image view, you should be getting a NULLPOINTER EXCEPTION, have you posted the correct xml/java ?

Related

Howto Add Integers to a Listview With Custom Adapter

i have one list view with two text views inside it, one edit text that is in the same activity but not in the list view and two buttons one to add to the list view and the other to delete from it.
how to add integers to the first text view, the sum of all integers to the second one, and to be from a custom adapter.
thank you.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="#+id/edit_ten"
android:hint="Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_add"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Add"/>
<Button
android:id="#+id/btn_delete"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Undo"/>
</LinearLayout>
<ListView
android:id="#+id/list_sinhvien"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
item_layout.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:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_ten"
android:textSize="20dp"
android:text="Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text_sdt"
android:textSize="20dp"
android:text="Total"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.addanddelete;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView listSinhvien;
EditText editTen;
Button btnThem , btnSua;
ArrayList<Sinhvien> arraySinhvien;
CustomAdapter myadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anhxa();
arraySinhvien = new ArrayList<Sinhvien>();
myadapter = new CustomAdapter(this , R.layout.item_layout,arraySinhvien);
listSinhvien.setAdapter(myadapter);
btnSua.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int count = myadapter.getCount();
myadapter.remove(myadapter.getItem(count -1));
myadapter.notifyDataSetChanged();
return;}});
}
private void anhxa(){
listSinhvien = (ListView)findViewById(R.id.list_sinhvien);
editTen = (EditText)findViewById(R.id.edit_ten);
btnThem = (Button)findViewById(R.id.btn_add);
btnSua = (Button)findViewById(R.id.btn_undo);
btnThem.setOnClickListener(this);
btnSua.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_add:
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
String ten = editTen.getText().toString();
String sdt = editTen.getText().toString();
Sinhvien temp = new Sinhvien(R.mipmap.ic_launcher,ten , sdt);
arraySinhvien.add(temp);
myadapter.notifyDataSetChanged();
break;
}
}
}
CustomAdapter.java
package com.example.addanddelete;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter {
Activity activity;
int layout;
ArrayList<Sinhvien> arrSinhVien;
public CustomAdapter(#NonNull Activity activity, int layout, #NonNull ArrayList<Sinhvien> arrSinhVien) {
super(activity, layout, arrSinhVien);
this.activity = activity;
this.layout = layout;
this.arrSinhVien = arrSinhVien;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
convertView = layoutInflater.inflate(layout, null);
TextView ten = (TextView) convertView.findViewById(R.id.text_score);
TextView sdt = (TextView) convertView.findViewById(R.id.text_total);
ten.setText(arrSinhVien.get(position).getTenSinhvien());
sdt.setText(arrSinhVien.get(position).getSdtSinhvien());
return convertView;
}
}
Sinhvien.java
package com.example.addanddelete;
public class Sinhvien {
String tenSinhvien;
String sdtSinhvien;
public Sinhvien(String iclauncher,String ten, String sdt) {
}
public Sinhvien(int iclauncher,String tenSinhvien, String sdtSinhvien) {
this.tenSinhvien = tenSinhvien;
this.sdtSinhvien = sdtSinhvien;
}
public String getTenSinhvien() {
return tenSinhvien;
}
public void setTenSinhvien(String tenSinhvien) {
this.tenSinhvien = tenSinhvien;
}
public String getSdtSinhvien() {
return sdtSinhvien;
}
public void setSdtSinhvien(String sdtSinhvien) {
this.sdtSinhvien = sdtSinhvien;
}
}
The general behaviour/implementation that you've outlined in your question is very well documented. That said, I'd suggest considering utilising a RecyclerView with associated custom item view and adapter, as opposed to a ListView. There are a few reasons why I'd suggest this.
I did a little searching and found several examples that cover the general idea of your implementation. This example does a great job of illustrating how to achieve what you're seeking. The article begins by outlining some reasons to work with a RecyclerView over a ListView or GridView, then proceeds to give an in-depth run-down on how to implement a RecyclerView with custom adapter (and associated item view and item class).
At a glance, your implementation would require:
An Activity containing your RecyclerView, two Buttons (used for adding and deleting elements from the RecyclerView) and an EditText for taking user input.
A custom item View representing individual items of your RecyclerView list. This would contain the two TextView views (one for displaying the integer and the other for displaying the sum of all integers).
A custom item model Class to represent the data model for the above custom item View. This would hold an integer value and likely some logic for displaying the sum.
A custom RecyclerView adapter (which ties all of the above together). This will need to handle the task of binding data from your dataset (that grows and shrinks based on user input) to instances of your custom items that are to appear in the RecyclerView list. This adapter could also be used by your add and delete item buttons to modify the elements in the RecyclerView list.
The above is outlined in far greater depth in the link I provided earlier.
I sincerely hope that helps!

How to parse an integer and it gives a list of courses in android studio?

am developing an android application. My problem statement is as follows:
1) I need to have a ListView of courses available at some colleges.
2) I the points for each of them(e.g medicine:30,maths:24,English:14,French:28,etc...)
3) I need to parse the points(integer) to corresponding courses and lower courses than that.
Full sample:
Enter your points:(say integer 26 )(edittext)
After the person added his point he gets a list of courses he is likely to apply at the college like that:
You are eligible to apply for these courses:
maths
english(to be listed in as a listview)
P.s am using android studio.
can someone help me programmatically? thanks
mainactivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] courses = new String[]{
"Mathematics",
"Chemical Engineering",
"Civil Engineering",
"Agriculture",
"Law and Management",
"Sociology",
"Information and Communication Technologies",
"Computer Science",
"Information Systems",
"Software Engineering",
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event2icon);
EditText editText = null;
editText=(EditText)findViewById(R.id.edit_text);
final String point=editText.getText().toString();
final int point_integer=Integer.parseInt(point);
final ListView courselist = (ListView)findViewById(R.id.courseNames);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, courselist, false);
courselist.addHeaderView(header, null, false);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, courses);
courselist.setAdapter(adapter);
courselist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
if((Integer.parseInt(String.valueOf(point_integer))<=10))
{
}
}
});
}
}
event2icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_text"
android:text="Enter your points:"/>
<ListView
android:id="#+id/courseNames"
android:layout_width="match_parent"
android:layout_below="#+id/edit_text"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:dividerHeight="15.0sp"
>
</ListView>
</LinearLayout>
listviewheader.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
you need to parse points into integer and then by pass that integer to query to get list of eligible courses ;get point_integer from below code and use it into your query for database or arraylist and set aadapter to listview for eligible courses list related to that point
String point=edittext.getText().toString.trim();
int point_integer=Integer.parseInt(point);

Android GridView not updating

I have a GridView arranged in a 3x4 matrix, each item in the GridView is a FrameLayout containing an ImageView and a Textview. Initially the TextViews are set to display the letter "X". When clicked on, the letter is changed to "Y".
It works on all the GridView items except the first one. If I click on the first one (top left) then nothing happens, no matter how long I wait. If I then click anywhere else on the Android emulator screen (even outside the GridView itself), at that point the top left GridView item changes to "Y".
I have a gut feeling it is something to do with the fact that "getView" in "ImageAdapter" is being called several times with "position=0" before the GridView is rendered, the sequence is as follows:
0
0
1
2
3
4
5
6
7
8
9
10
11
0
Now maybe that's just how Android works, I don't know, but it seems strange. If anyone can help me out I'd appreciate it so much.
UPDATE: it appears the problem is related to "MyView=convertView" in getView(), if I simply remove this and always create a new View (as if convertView is null always) then the problem no longer exists. Now I don't think this solution will be an issue as I don't use scrolling and the number of Views created should be 12 (i.e. a 3x4 GridView), but if anyone can explain the correct solution to the problem it would help a lot, thanks.
My main activity (and only activity):
package com.xxxxxx.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
init_gridview();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void init_gridview()
{
setContentView(R.layout.game);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(3);
gridview.setColumnWidth(32);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
grid_click(position,v);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return(3*4);
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View MyView = convertView;
if (convertView == null)
{
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.grid_item, null);
}
else
{
MyView=convertView;
}
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText("X");
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
iv.setImageResource(R.drawable.tile);
MyView.setLayoutParams(new GridView.LayoutParams(32,32));
return(MyView);
}
}
private void grid_click(int position, View v)
{
View clicked_view=v;
TextView tv2 = (TextView)clicked_view.findViewById(R.id.grid_item_text);
tv2.setText("Y");
}
}
The main layout, called game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center"
android:background="#android:color/white">
<GridView
android:id="#+id/gridview"
android:background="#android:color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="106dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="none"
android:gravity="center"
android:listSelector="#android:color/transparent"
/>
</LinearLayout>
Finally the FrameLayout for each item in the GridView, called grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/grid_item_image"
android:src="#drawable/tile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="contentDescription"
/>
<TextView
android:id="#+id/grid_item_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textStyle="bold"
android:textSize="17sp"
android:shadowColor="#555555"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="1.5"
/>
</FrameLayout>
I think the problem is with your grid_click function it assumes that the view being clicked is the whole cell (FrameLayout) when it could possibly be just the image or text view itself.

Item clic with ListView Android don't work correctly

I can not get the correct screen open.
I have the following code, and I also added the corresponding class in the manifest.
Please, if you can give me some idea.
Thank you.
listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
ListAdapter adp = (ListAdapter)parent.getAdapter();
//lista.get(position);
Seccion s = lista.get(Integer.parseInt(adp.getItem(position).toString()));
if (s.getTitle().contains("Estudiar")) {
//sel.setText("Seleccionado el del if"+ s.getTitle());
Intent i = new Intent(LiverpoolguideActivity.this,tipoListado.class);
startActivity(i);
}
}
});
The code tipoListado.class :
package jorgechu.com.liverpoolguide;
import java.util.ArrayList;
import jorgechu.com.liverpoolguide.Secciones.Seccion;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class tipoListado extends Activity {
public class LiverpoolguideActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listadotipo);
}
}
}
And the code screen xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textViewListadoTipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tipo:"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ListView
android:id="#+id/listViewType"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Focusable views inside a ListView item will disable the ability to select ListView items. Applying android:focusable="false" to the TextView will allow OnItemClick to work again.

Android - Retrieving a string from a specific element of a gridview

I'm pretty new to Android and I have been playing about with GridView. I've got stuck trying to retrieve Strings from individual elements of the GridView so that I can save state based on the changes a user has made.
I've mocked up a stripped down version of what I'm trying to do for this question:
The view of each element in the grid is made up of a TextView and an EditText defined by the following xml file; grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/single_item_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id = "#+id/label"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:inputType="text">
</TextView>
<EditText
android:id = "#+id/item_value"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
android:inputType="text">
</EditText>
</LinearLayout>
My main.xml, consisting of a button and a gridview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/editbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRetrieveButtonClick"
android:text="#string/get" android:clickable="true"/>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="250dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:paddingTop="50dp">
</GridView>
</LinearLayout>
My Activity Class:
package com.jdev.simplegrid;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.Toast;
public class SimpleGridActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
//Some hard-coded sample data -each person has a birth-name and a nickname
ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("David","Dave"));
people.add(new Person("Bruce","Batman"));
people.add(new Person("John","J"));
gridview.setAdapter(new SimpleAdapter(this, people));
}
public void onRetrieveButtonClick(View view)
{
Toast.makeText(getBaseContext(), "Get the String value of EditText at position 1 of the gridview e.g Batman", Toast.LENGTH_LONG).show();
//Get the String value of EditText at position 1 of the gridview. e.g Batman
}
}
And finally my Adapter for the view:
package com.jdev.simplegrid;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class SimpleAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Person> personList;
public SimpleAdapter(Context c, ArrayList<Person> people) {
mContext = c;
personList = people;
}
public int getCount() {
return personList.size();
}
public Person getItem(int position) {
return personList.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.grid_item, parent, false);
TextView birthnameLabel=(TextView)view.findViewById(R.id.birthname);
birthnameLabel.setText(personList.get(position).getBirthname());
EditText nicknameText=(EditText)view.findViewById(R.id.nickname);
nicknameText.setText(personList.get(position).getNickname());
return view;
}
}
So my question is, how do I retrieve the a value from the gridview on the click of a button. Say the value "Batman" in EditText at position 1 of the gridview.
I feel like I'm really missing something here!
gridview.getAdapter().getItem(1).getNickname();
Is that what you're looking for? assuming your Person object has a getter method for the nickname of course. - Sorry, obviously I could've seen that in your custom adapter.
If your the idea is the user can change the nickname in the EditText, you'll probably want to add a TextChangedListener (TextWatcher) to each of them and after editting update the nickname on the Person object associated with that position in the grid; e.g. with the help of a setNickname(String) method.
The easiest way to think about this is probably in terms of 'models' and 'views'. The TextViews and EditTexts are the views, whereas the Person objects in the adapter are the models. If you want to make any changes to the data, modify the underlying models with some logic and simply have the views update/refresh after that.

Categories

Resources