Adding a TextView dynamically in a fragment, in a ScrollView - android
I know that there were a lot of questions like this one, but I haven't been able to find the answer I'm looking for...
I'd like to dynamically create some TextView in an already existing ScrollView. I've done something that I thought would have been okay, but the app stops when it comes to this fragment?
I can't put the whole project because the files are numerous, but here are the concerned .java and .xml:
NotesPageFragment.java
package com.a3m.Controllers.Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.a3m.Controllers.ui.Controler;
import com.a3m.Controllers.core.Task;
import com.a3m.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
public class NotesPageFragment extends Fragment {
private Controler controler;
private ScrollView mScrollView;
public static NotesPageFragment newInstance() {
return(new NotesPageFragment());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_notes_page, container, false);
this.controler = Controler.getInstance();
this.mScrollView = v.findViewById(R.id.fragment_page_notes_scrollview);
//addNotes(notes,getAllTasks());
addNotes(v, getAllTasks());
return v;
}
public ArrayList<Task> getAllTasks()
{
/*
** ici le code qui se connecte à la bdd et retourne toutes les tasks disponibles
*/
ArrayList<Task> tasks = new ArrayList<>();
ArrayList<String> n =new ArrayList<>();
n.add("detail1");
n.add("detail2");
n.add("detail3");
Task t1=new Task(0,0,null,"t1",0,0,null,0,null,null,n,null);
Task t2=new Task(0,0,null,"t2",0,0,null,0,null,null,n,null);
Task t3=new Task(0,0,null,"t3",0,0,null,0,null,null,n,null);
Task t4=new Task(0,0,null,"t4",0,0,null,0,null,null,n,null);
Task t5=new Task(0,0,null,"t5",0,0,null,0,null,null,n,null);
Task t6=new Task(0,0,null,"t6",0,0,null,0,null,null,n,null);
Task t7=new Task(0,0,null,"t7",0,0,null,0,null,null,n,null);
tasks.add(t1);
tasks.add(t2);
tasks.add(t3);
tasks.add(t4);
tasks.add(t5);
tasks.add(t6);
tasks.add(t7);
return tasks;
}
public void addNotes(View view, ArrayList<Task> tasks) {
Iterator<Task> itr_tasks = tasks.iterator();
Task task;
String taskNote;
while(itr_tasks.hasNext()) {
taskNote = "";
task = itr_tasks.next();
taskNote += task.getName();
Iterator<String> itr_notes = task.getNotes().iterator();
while(itr_notes.hasNext()) {
taskNote += "\t\t\t" + itr_notes.next() + "\t\t\t";
}
final TextView taskNotes = new TextView(getActivity());
taskNotes.setText(taskNote);
mScrollView.addView(taskNotes);
}
}
}
fragment_notes_page.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:id="#+id/fragment_page_news_rootview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDD9CF"
android:contentDescription="NotesPage"
android:gravity="center"
tools:context="com.a3m.Controllers.Fragments.NotesPageFragment">
<ScrollView
android:id="#+id/fragment_page_notes_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/nav_header_marginLeft">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="171dp"
android:src="#android:drawable/ic_menu_info_details" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="Notes"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="30sp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I'm aware that the IDs in the .xml aren't really good, but I'll improve them later...
If someone has an idea or can show me were the answer is, I'll gladly accept this help!!
ScrollView and all it's relatives that scroll the content (i.e. NestedScrollView) allow only for one child. You cannot add new views directly to ScrollView if it already has one child view.
Quote from java doc to ScrollView class:
/**
* A view group that allows the view hierarchy placed within it to be scrolled.
* Scroll view may have only one direct child placed within it.
* To add multiple views within the scroll view, make
* the direct child you add a view group, for example {#link LinearLayout}, and
* place additional views within that LinearLayout.
...
*/
And if you look inside of the class you will find that it overrides all addView methods:
To fix the issue what you need to do is to add id to your LinearLayout and insert new views into it.
I'd recommend changing it all to RecyclerView + Adapter.
Official tutorials from Google on how to use RecyclerView and Adapters
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!
Android inflating views from other layouts
I'm relatively new to android development and I'm trying to find a way to inflate a view repeatedly each time when a button is pressed, in a different location, so every inflated view has its own position: import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; public class teamCreateScreen extends Activity { #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.team_locate_layout); } public void createTeam(View view) { final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam); View teamBox = View.inflate(this, R.layout.team_box, rlTeam); final TextView teamBoxView = (TextView) findViewById(R.id.team_task_box); teamBoxView.setX(0); teamBoxView.setY(230); } } The XML code of the layout: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/rlTeam"> <Button android:layout_width="60dp" android:layout_height="60dp" android:id="#+id/teamAddBtn" android:text="+" android:textSize="30sp" android:onClick="createTeam"/> </RelativeLayout> XML code of the view that's being inflated: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="192dp" android:layout_height="120dp" android:id="#+id/team_task_box" android:text="New Team" /> </RelativeLayout> I want to use the same view to inflate multiple boxes with different coordinates in the layout. Every time I press the button to inflate the view again it inflates the box in the same coordinates so they overlap. I need to make the second box to appear to the first one's right, the third below 1st and so on, much like a grid of boxes.
Try this code and tell me whether it works. Remove the inflating of layout import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; public class teamCreateScreen extends Activity { int i=0; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.team_locate_layout); } public void createTeam(View view) { final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam); RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); TextView tv=new TextView(getApplicationContext()); if(tv.getId()>0) { relativeParams.addRule(RelativeLayout.BELOW, tv.getId()); } tv.setId(i); r1Team.addView(tv, relativeParams); i++; } } Declare int i=0; as a global variable and increment it in the createTeam() method.
How to show empty text in list fragment?
I am using Google Place API to find nearest places. I am displaying map and list on two tabs. I have made two fragments. In one i am showing map and in second one i am using list. I have used an editext box on actionbar. When i am running my app it is crashing and giving nullpointer exception at setListAdapter. It is my ListFragment file import android.support.v4.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.aquib.android.place.Place; import com.aquib.android.place.PlaceAdapter; import com.aquib.android.place.R; import java.util.List; /** * Created by dell on 2/9/2015. */ public class ListItemFragment extends ListFragment{ private PlaceAdapter adapter; List<Place> placeList; #Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_list, null); TextView emptyTextView = (TextView) view.findViewById(android.R.id.empty); emptyTextView.setTextSize(10); return view; } #Override public void onActivityCreated(Bundle savedInstanceState) { adapter = new PlaceAdapter(getActivity(), placeList); setListAdapter(adapter); } } and my fragment_list.xml file <?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="vertical"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#android:id/list" /> <TextView android:id="#id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:text="No data"/> </LinearLayout> When i will give input in edittext box and call place api and then parse json data. I have written Place class and then add place object in list. so please someone tell me that how to display empty text. app is showing exception in arrayadapter.getCount() method.
Your placeList is never initialized. Moreover, in onCreateView(), you should probably create your view like this: View view = inflater.inflate(R.layout.fragment_list, container, false);
Is it possible to dynamically add AndroidAnnotation #EViewGroups instead of using the traditional inflate directly?
I'm trying to use Android Annotations and dynamically add layout components when my createNewRow button is clicked. The app runs and displays the default rows defined in activity_main.xml and, after clicking createNewButton I see children attached to my dynamicTable in the debugger but the new children are not displayed. Here is my main activity XML: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="#+id/inflateLayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" android:paddingBottom="#dimen/activity_vertical_margin" tools:context="net.richardriley.inflate.app.MainActivity"> <Button android:id="#+id/createNewRow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/newRowButton"/> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/dynamicTable"> <TableRow> <TextView android:text="#string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> </TableLayout> </LinearLayout> So simply a linear layout with a button and then a table container. Clicking createNewRow should add a new InflatedRow. Here is the XML for the row I want to dynamically add: inflatedrow.xml <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="#+id/inflatedRowTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/inflatedRowLabel"/> <Button android:id="#+id/inflatedRowButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/defaultNewRowText" /> </merge> Using AA my subclass for a tableRow is InflatedRow.java package net.richardriley.inflate.app; import android.content.Context; import android.widget.Button; import android.widget.TableRow; import android.widget.TextView; import org.androidannotations.annotations.EViewGroup; import org.androidannotations.annotations.ViewById; /** * inflate : Created by rgr on 20/03/14. */ #EViewGroup(R.layout.inflatedrow) public class InflatedRow extends TableRow { #ViewById Button inflatedRowButton; #ViewById TextView inflatedRowTextView; public InflatedRow(Context context) { super(context); } } and finally my main activity java itself: MainActivity.java package net.richardriley.inflate.app; import android.support.v7.app.ActionBarActivity; import android.widget.LinearLayout; import android.widget.TableLayout; import org.androidannotations.annotations.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static ch.qos.logback.classic.android.BasicLogcatConfigurator.configureDefaultContext; #OptionsMenu(R.menu.main) #EActivity(R.layout.activity_main) public class MainActivity extends ActionBarActivity { static { configureDefaultContext(); log = LoggerFactory.getLogger(MainActivity.class); } #ViewById LinearLayout inflateLayout; #ViewById TableLayout dynamicTable; public MainActivity() { } protected static Logger log; #Click void createNewRow() { log.info("clicked"); InflatedRow_ inflatedRow=new InflatedRow_(this); dynamicTable.addView(inflatedRow,0); /*LayoutInflater layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(R.layout.inflatedrow,null); dynamicTable.addView(view);*/ } #OptionsItem void openSettingsSelected(){ log.info("Hello"); } } In createNewRow if I use the inflater service directly it works. What am I missing? many thanks.
Don't use the annotated class when you're inflating the view and make sure you're calling the build method that inflates it. #Click void createNewRow() { log.info("clicked"); InflatedRow inflatedRow = new InflatedRow_.build(this); dynamicTable.addView(inflatedRow,0); }
instead of 'View inflatedRow= new InflatedRow_(this);' I use 'View inflatedRow=InflatedRow_.build(this);'. This is documented for custom controls and I needed to do it for a merged control group too. So mea culpa to a degree! #Click void createNewRow() { log.info("clicked"); InflatedRow_ inflatedRow=InflatedRow_.build(this); dynamicTable.addView(inflatedRow,0); } Alternatively (and no idea if this would continue to be supported: #Click void createNewRow() { log.info("clicked"); /*View inflatedRow = InflatedRow_.build(this);*/ InflatedRow_ inflatedRow = new InflatedRow_(this); inflatedRow.onFinishInflate(); dynamicTable.addView(inflatedRow, 0); }
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.