With help of a suggestion from another answer I wrote the below code. But for some odd reason, it displays the text for the first TextView just fine and then, every other TextView below it is a blank! I tried many different ways but unable to figure it out. Someone please assist me..THANKS!
JAVA code(MainActivity.java)
ToDoListDB info = new ToDoListDB(MainActivity.this);
info.open();
String data = info.getData();
info.close();
//Split the List item string into individual strings
String delims = "[\\n]+";
String[] tokens = data.split(delims);
List<TextView> itemList = new ArrayList<TextView>(tokens.length);
for(int i = 0; i < tokens.length; i++)
{
TextView tvItem = new TextView(MainActivity.this);
tvItem.setText(tokens[i]);
tvItem.setTextColor(getResources().getColor(R.color.text_color));
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
itemList.add(tvItem);
myLayout.addView(itemList.get(i));
//myLayout.addView(tvItem);
//itemList.add(tvItem);
}
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"
android:weightSum="10" >
<EditText
android:id="#+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#DDD"
android:hint="Enter a list item"
android:textColor="#D00" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8" >
<LinearLayout
android:id="#+id/itemListLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Button" />
</LinearLayout>
add the layout_orientation in your linearlayout
<LinearLayout
android:id="#+id/itemListLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000">
Related
I am pretty new in Android.
I have some problems to create a dynamic LinearLayout programmatically.
I actually have some information in ArrayLists that I need to display. The problem is that I don't know how many items are in the ArrayList, so I need to create the same layout (LinearLayoutChild) for every item in the ArrayList.
For example, I have created this (in this case, let's say the ArrayLists have 2 items each)
ArrayList<String> alistA = new ArrayList<String>();
ArrayList<String> alistB = new ArrayList<String>();
ArrayList<String> alistC = new ArrayList<String>();
ArrayList<String> alistD = new ArrayList<String>();
ArrayList<String> alistE = new ArrayList<String>();
alistA.add(0, "TextA1");
alistA.add(1, "TextA2");
alistB.add(0, "TextB1");
alistB.add(1, "TextB2");
alistC.add(0, "TextC1");
alistC.add(1, "TextC2");
alistD.add(0, "TextD1");
alistD.add(1, "TextD2");
alistE.add(0, "TextE1");
alistE.add(1, "TextE2");
int NumberArray = 2;
for(int i = 0; i<NumberArray;i++){
// How can i do this?
}
I want to display it like :
alistA[0] -> Tv1
alistB[0] -> Tv2
and so on...
alistA[[1]] -> Tv1 (newly created)
alistB[[1]] -> Tv2 (newly created)
...
My XML file :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.murat.testlol.MainActivity"
android:id="#+id/LinearLayout1"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Et1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Btn1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="12"
android:orientation="horizontal"
android:id="#+id/LinearLayoutChild">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/Ll1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv1"
android:gravity="center"
android:text="TextView1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:id="#+id/Tv2"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/Ll2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv3"
android:gravity="center"
android:text="TextView3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/Tv4"
android:text="TextView4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/Ll3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv5"
android:gravity="center"
android:text="TextView5"/>
</LinearLayout>
</LinearLayout>
The EditText and button need to stay at the same place.
Thank you.
Sounds like a good use case for Android's ListView. From Google's documentation:
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
To go this route, update main.xml to include a ListView element -- and move the LinearLayoutChild into its own XML file.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.murat.testlol.MainActivity"
android:id="#+id/LinearLayout1"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Et1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Btn1"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
generic_linear_layout_child.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical"
android:id="#+id/linear_layout_child">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Tv1"
android:gravity="center"
android:text="TextView1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView2"
android:id="#+id/Tv2"
android:gravity="center"/>
</LinearLayout>
Then, create an adapter to bind the data in your array to the appropriate UI elements. Here's a SO post on how to create a custom adapter: Custom Adapter for List View
You need to create the linearlayouts and textviews dynamically, using routines like this:
public TextView makeTextView (String text)
{
TextView tv = new TextView (context)
tv.setText (text) ;
// customise the layout of the text here, eg...
tv.setTextColor(Color.RED) ;
return tv;
}
public LinearLayout makeHorizLayout ()
{
LinearLayout ll = new LinearLayout (context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return ll;
}
After you have loaded the schema, you find the view that you want to insert the linearLayouts into like this:
View mainView = findViewById (R.id.LinearLayout1);
You then add a linearlayout to a view like this:
LinearLayout ll = makeHorizlayout ();
mainView.add(ll);
Then you add textviews to the LinearLayout like this:
TextView tv = makeTextView ("whatever text");
ll.addView (tv);
Do the following. Get your data and fill a linearLayout container during runtime after you have retrieved / created your data. Here is an example for creating the views.
private LinearLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL)
// + other layout stuff / better inflate this from xml
// Get data and call createLayout with the data
}
createLayout(List<List<String>> lists) {
for (List<String> list : lists) {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// + other layout stuff / better inflate this from xml
for (String string : list) {
TextView textView = new AppCompatTextView(this);
textView.setText(string);
linearLayout.addView(textView);
}
container.addView(linearLayout);
}
}
I am working on an application where I have multiple linear layouts which are dynamically generated. Each linearlayout has an Id field, Name and a radioGroup containing two radio buttons for Yes and No.
I am also providing a button at bottom of the page so that once the button is clicked, All the YES radio buttons are selected.
How can i achive this as the layout is dynamically generated based on some data:
Here is my layout which is repeated dynamically:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/RollNum"
android:textSize="20dp"
android:textColor="#color/black"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="6"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Amandeep Singh and some very very long name"
android:gravity="center"
android:id="#+id/Name"
android:textSize="20dp"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center">
<RadioGroup
android:id="#+id/radioAttendance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation='horizontal'
android:gravity="center">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P"
android:id="#+id/PresentCheck"
android:textSize="10dp"
android:gravity="center"
android:textColor="#color/black"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="#+id/AbsentCheck"
android:textSize="15dp"
android:gravity="center"
android:textColor="#color/black"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:background="#ffffff" />
</LinearLayout>
</LinearLayout>
I tried following approach but didn't work:
//some code before it for button clicks and all other stuffs
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout scrollViewLinearlayout = (LinearLayout)findViewById(R.id.parent); // The layout inside scroll view
//int count=50;
Studentlist = Arrays.copyOfRange(StudentlistInterim, 1, StudentlistInterim.length);
for(int i = 0; i < Studentlist.length; i++){
String data = Studentlist[i];
String RollNum = data.split("--")[0];
String Name = data.split("--")[1];
Log.d("Name is:", Name);
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
View item = inflater.inflate(R.layout.item_layout, null, false);
layout2.addView(item);
layout2.setId(i);
TextView trollnum = (TextView) item.findViewById(R.id.RollNum);
trollnum.setText(RollNum);
TextView tname = (TextView) item.findViewById(R.id.Name);
tname.setText(Name);
RadioButton rPresent = (RadioButton)item.findViewById(R.id.PresentCheck);
RadioButton rAbsent = (RadioButton)item.findViewById(R.id.AbsentCheck);
rPresent.setId(i); //tried to set a unique Id for all YES
rAbsent.setId(Studentlist.length+i); // to make the id unique for each YES and NO entry
scrollViewLinearlayout.addView(layout2);
Log.d("Id is: ", trollnum.getText().toString());
}
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.bMarkAllPresent:
for (int i=0;i<Studentlist.length;i++){//iterate over the list for the radio buttons
RadioButton r1 = (RadioButton)findViewById(i);
RadioGroup rg = (RadioGroup)findViewById(R.id.radioAttendance);
rg.check(r1.getId()); /* get the id of radio button one by one and mark it checked*/
//Log.d("Button check id:", (r1.getId()));
}
break;
I got 'java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.RadioButton' error in the onClick() method
I am not sure if this approach will work or if this is the best approach.
Please help.
Edit-1:
After using the List way of doing it as per the answer.
RadioButton rPresent = (RadioButton)item.findViewById(R.id.PresentCheck);
RadioButton rAbsent = (RadioButton)item.findViewById(R.id.AbsentCheck);
//rPresent.setId(i);
//rAbsent.setId(Studentlist.length+i); // to make the id unique for each present and absent entry
presentRadioList.add(rPresent); //getting null pointer here
absentRadioList.add(rAbsent);
I am getting null pointer exception.
I suggest keeping two List<RadioButton>s, one for the "yes" selections and one for the "no" selections. Then you can just iterate through the appropriate list.
I am trying to map a List<List<Object>> to a RecyclerView in Android, but the result is totally messed up.
For example, I have a list of list like this (just for explaining, not my real case):
List<List<String>> list = {{a, b, c}, {d, e}, {f, g, h, i}};
the Recyclerview should display like (first row contains three subrows, second has two and the last on has four):
|----a-----|
|----b-----|
|----c-----|
|=======|
|----d-----|
|----e-----|
|=======|
|----f-----|
|----g-----|
|----h-----|
|----i-----|
but the result is not exactly like the above order. some element is duplicated and some disappears. for example:
|----d-----|
|----e-----|
|----c-----|
|=======|
|----d-----|
|----e-----|
|=======|
|----f-----|
|----a-----|
Here is a piece of my code:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
List<Event> list = eventList.get(position);
if (holder.rows < list.size()) {
holder.rowViewGroup.removeAllViews();
holder.rows = 0;
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView startView = new TextView(context);
startView.setLayoutParams(layoutParams);
Event headEvent = list.get(0);
Calendar startCal = headEvent.getStartCal();
startView.setText(String.format("%tD", startCal));
holder.rowViewGroup.addView(startView);
for (final Event event : list) {
View element = LayoutInflater.from(context).inflate(R.layout.element, null);
//start time view
TextView startTimeView = (TextView)element.findViewById(R.id.eventStartTimeInElement);
Calendar startTimeCal = event.getStartCal();
startTimeView.setText(String.format("%tl:%tM %tp", startTimeCal, startTimeCal, startTimeCal));
//end date time view
TextView endView = (TextView)element.findViewById(R.id.eventEndTimeInElement);
Calendar endCal = event.getEndCal();
endView.setText(String.format("%tD %tl:%tM %tp", endCal, endCal, endCal, endCal));
//title view
TextView title = (TextView)element.findViewById(R.id.eventTitleInElement);
title.setText(event.getTitle());
//member name
Drawable divider = context.getResources().getDrawable(R.drawable.divider);
ImageView dividerView = new ImageView(context);
dividerView.setImageDrawable(divider);
holder.rowViewGroup.addView(dividerView);
holder.rowViewGroup.addView(element);
holder.rows++;
}
}
}
Here is my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycleViewRow">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rowView"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/eventStartTimeInRow"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
and element.xml(which is contained by 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="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/eventStartTimeInElement"
android:layout_weight="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/eventEndTimeInElement"
android:gravity="end"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/eventTitleInElement"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/memberNameInElement"
android:gravity="end"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And I know this is not a really good idea to implement this, so could anybody please tell me a better way to implement this? Thanks...
I think your best bet is to flatten your list of lists into one list.
This is just for purposes of the ListView. Your adapter will act as though the lists have all been strung into one long list.
Start by creating a single-list index for all the items of your nested lists.
For example:
List<List<String>> listOfLists = ... // your original data
List<String> listForView = new ArrayList<String>();
for (List<String> innerList : listOfLists) {
for (String str : innerList) {
listForView.add(str);
}
listForView.add("---"); // e.g. marker value for divider
}
Now in your adapter's getItem() need only return listForView.get(position).
Anytime your nested list structure changes, you redo this flattening operation and call notifyDataSetChanged().
Things get a little trickier if — given the position — you have to figure out which list an item is in, but you could always index the inner lists in a similar fashion.
I want to create a game list wherein the game details like opponent name,score,etc will be displayed.I am having problem fetching the data into xml and then displaying it.please help.
I have created two xml.I have to set data into the 2nd xml and then include that 2nd xml into 1st xml.
my 1st xml is
<FrameLayout
android:id="#+id/gameListFrame"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip" >
<LinearLayout
android:id="#+id/GameListHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/scrollBackG"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>
</FrameLayout>
2nd xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gamelist_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50px"
android:gravity="left"
android:orientation="horizontal"
android:background="#FFF" >
<ImageView
android:id="#+id/gl_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout android:orientation="horizontal"
android:layout_width="225px"
android:layout_height="fill_parent"
android:gravity="left"
android:background="#000">
<LinearLayout android:orientation="vertical"
android:layout_width="175px"
android:layout_height="wrap_content"
android:gravity="top">
<TextView android:id="#+id/oppName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<TextView android:id="#+id/lastWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/diffTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My code is
gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);
for (i=0;i<gamelistVector.size();i++)
{
rowView=View.inflate(context, R.layout.gamelist_details,null );
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView); //show the total games}
You need to extend ListAdapter and override the getView() method that sets the data for each row.
See this example
You need inflater reference to inflate views.
To GetInflater reference use:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);
for (i=0;i<gamelistVector.size();i++)
{
rowView=inflater.inflate(R.layout.gamelist_details, null);
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView); //show the total games
}
thanks guys 4 answering to my query...i saw ur suggestions and figured a way out.this is wat i did nd its working...
private void doDisplay(){
this.runOnUiThread(new Thread() {
#Override
public void run() {
TextView gameCount=(TextView) findViewById(R.id.game_count);
gameCount.setText(" found "+maxGames+" games ");
for (i=0;i<gamelistVector.size();i++)
{
rowView=View.inflate(context, R.layout.gamelist_details, null);
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
TextView score=(TextView) rowView.findViewById(R.id.opplastMoveScore);
oppName.setText(" "+gamelistVector.get(i).getOppName());
lastWord.setText(" "+gamelistVector.get(i).getOppLastWord());
diffTime.setText(gamelistVector.get(i).getDiffTime()+"");
score.setText("("+gamelistVector.get(i).getLastMoveScore()+")");
ImageView oppImage = (ImageView) rowView.findViewById(R.id.gl_avatar);
oppImage.setImageBitmap(gamelistVector.get(i).getOppImage());
ImageView status = (ImageView) rowView.findViewById(R.id.gl_status);
status.setImageResource(gamelistVector.get(i).getTurnUserID());
gameListItem.addView(rowView);
}
}
});
}
I am trying to add an edit text view dynamically to a layout from the java class. However from what I have implemented nothing changes on my action. This is what I have:
public final void onCreate(final Bundle i){
int value = 0;
isEdit = false;
try
{
super.onCreate(i);
this.setContentView(R.layout.contactedit);
ContactList.soundIndicator = 0;
etPhoneNumber = new ArrayList<EditText>();
this.etPhoneNumber.add((EditText) this.findViewById(R.id.etPhoneNumberInput));
this.addNumberBtn = (Button) this.findViewById(R.id.addNumberEditText);
addNumberBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//etPhoneNumber.add(new EditText(ContactEdit.this));
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
EditText temp = new EditText(ContactEdit.this);
temp.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
etPhoneNumber.add(temp);
layout.addView(temp);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
});
}
This is the 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="wrap_content"
android:baselineAligned="true"
android:orientation="vertical"
android:id="#+id/contacteditll"
android:background="#drawable/darkimg">
<TextView
android:id="#+id/titlePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/titlePrompt"
/>
<Spinner
android:id="#+id/spContactTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/titlePrompt"
/>
<TextView
android:text="#string/namePrint"
android:id="#+id/namePrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etFirstNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView
android:text="#string/secondPrint"
android:id="#+id/secondPrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etSecondNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/numberPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etPhoneNumberInput"
android:hint="#string/returnNumber">
</EditText>
<Button android:id="#+id/addNumberEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:freezesText="true"
android:editable="false"
android:text="ADD"
/>
<include
android:id="#+id/customedittext"
layout="#layout/myedittext"/>
<TextView android:id="#+id/emailPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/emailPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etEmailAddressInput"
android:hint="#string/returnEmail">
</EditText>
<Button
android:freezesText="true"
android:editable="false"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:id="#+id/btnSaveButton"
android:layout_width="wrap_content"
android:text="#string/save"
android:layout_below="#+id/emailInput"
/>
</LinearLayout>
Whenever I click on my button the action is heard but nothing is done. The layout of the page does not change even though the new edit text is in my list of edit texts and I am adding the new edit text to the layout.
Any help would be appreciated thanks.
It sure gets added but you are not seeing it cause there is no space left. Two things to test:
Place the LinearLayout inside a ScrollView.
Use the weight parameter.
Using the weight parameter:
EditText temp = new EditText(ContactEdit.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);