I have a TableLayout in fragment with following code:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/projectsTableLayout"
android:orientation="vertical"
android:layout_margin="10dp">
</TableLayout>
This is the AddMore button which is below the TableLayout tag:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/add_more_projects_btn"
android:text="#string/addmore"
android:textColor="#color/colorAccent"
android:layout_below="#+id/projectsTableLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
Also I have created TableRow layout in separate xml file with following code:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/project_name__ET"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:hint="#string/projectnametxt"
android:inputType="text"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<LinearLayout
android:id="#+id/yearProjectTypeLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="05dp"
android:orientation="horizontal"
android:weightSum="1">
<Spinner
android:id="#+id/yearSpinner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:drawSelectorOnTop="false"
android:spinnerMode="dropdown" />
<Spinner
android:id="#+id/projectTypeSpinner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:drawSelectorOnTop="false"
android:spinnerMode="dropdown" />
</LinearLayout>
</LinearLayout>
</TableRow>
What I want to implement is when the application loads I will be dynamically add rows 5 times.
I want to add 5 more rows on the Add More button click. Also I have to send all the values to the webserver when the user moves ahead in the application.
I am confused how should provide the ID to the components like(EditText, Spinners) in the Rows of table and retrieve it when the user moves to next screen.
I want to code in such a way so it can be optimised and easy to retrieve all the data for as many rows added to TableLayout on the AddMore Button click.
How to retrieve values and set ids for Row Components.
EDIT ::
Modification of code as suggested by #Rohit Heera
I have written following code:
projectTableLayout = (TableLayout) rootView.findViewById(R.id.projectsTableLayout);
projectTableLayout.setStretchAllColumns(true);
yearAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, Constants.YEAR_ARRAY);
projectTypeAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, Constants.PROJECT_TYPE_ARRAY);
projectListData = new ArrayList<ProjectData>();
for(int i = 0; i < 5 ; i++)
{
tableRowStartCount = i;
TableRow projectItemRow = new TableRow(getActivity().getApplicationContext());
LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(getActivity().getApplicationContext().LAYOUT_INFLATER_SERVICE);
projectItemRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT, 1.0f));
View projectRowView = inflator.inflate(R.layout.project_row_item,projectItemRow,false);
projectNameET = (EditText) projectRowView.findViewById(R.id.project_name__ET);
yearSpinner = (Spinner) projectRowView.findViewById(R.id.yearSpinner);
yearSpinner.setAdapter(yearAdapter);
projectTypeSpinner = (Spinner) projectRowView.findViewById(R.id.projectTypeSpinner);
projectTypeSpinner.setAdapter(projectTypeAdapter);
projectItemRow.addView(projectRowView);
ProjectData projectObj = new ProjectData();
projectListData.add(projectObj);
projectTableLayout.addView(projectItemRow,tableRowStartCount);
}
Can you please let me know how can i implement the eventlistener in the above loop?
Use this code to add rows in table
/**
* this method add the dynamic table row and display the list .
*/
public void displayList(List<Data> datalist) {
int i = 0;
TableLayout projectsTableLayout = (TableLayout) findViewById(R.id.projectsTableLayout);
projectsTableLayout.removeAllViews();
for (i = 0; i <= datalist.size(); i++) {
TableRow singleTableRow = new TableRow(this);
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.addattendeelistdata, null);
RelativeLayout parentRelativeLayout = (RelativeLayout) view
.findViewById(R.id.comleteRL);
EditText project_name__ET = (EditText) view
.findViewById(R.id.project_name__ET);
Spinner yearSpinner = (Spinner) view
.findViewById(R.id.yearSpinner);
Spinner projectTypeSpinner = (Spinner) view
.findViewById(R.id.projectTypeSpinner);
singleTableRow.addView(view);
/** Add row to TableLayout. */
projectsTableLayout.addView(singleTableRow, i);
}
}
And for get Reference for each object you need to Create seperate class .
If you tell me on which event you can move to next class so i can help you better.
Thanks
Like Edit Text you need to create separte class for spinner item listeners and store the value and main list automatically get all the new values.
/**
* this method add the dynamic table row and display the list .
*/
List<ProjectData> mainList=new List<ProjectData>();
// calling function
displayList(mainList);
public void displayList(List<ProjectData> datalist) {
int i = 0;
TableLayout projectsTableLayout = (TableLayout) findViewById(R.id.projectsTableLayout);
projectsTableLayout.removeAllViews();
for (Iterator<ProjectData> it :datalist.hasNext();) {
TableRow singleTableRow = new TableRow(this);
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.addattendeelistdata, null);
RelativeLayout parentRelativeLayout = (RelativeLayout) view
.findViewById(R.id.comleteRL);
EditText project_name__ET = (EditText) view
.findViewById(R.id.project_name__ET);
Spinner yearSpinner = (Spinner) view
.findViewById(R.id.yearSpinner);
Spinner projectTypeSpinner = (Spinner) view
.findViewById(R.id.projectTypeSpinner);
project_name__ET.addTextChangedListener(new ListenEditText(it.next()) );
singleTableRow.addView(view);
/** Add row to TableLayout. */
projectsTableLayout.addView(singleTableRow, i);
}
}
class ListenEditText implements TextWatcher
{
ProjectData data;
ListenEditText(ProjectData data)
{
this.data=data;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
data.project_name=s.toString();
}}
ModelClass;
class ProjectData
{
public String project_name;
public String year;
public String projectType;
}
Related
Hi a bit new to java/android programming, I am trying to populate a listview from a database. The problem is the database output for each record may have varying results. To give you an idea I have the following layout in my xml for the listview;
<?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"
android:background="#a28d7a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/coreHeader"
android:textColor="#ffffff"
android:padding="4dp" />
<LinearLayout
android:id="#+id/coreLinear"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/coreStatsLeft"
android:textColor="#ffffff" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView"
android:textColor="#ffffff"
android:textAlignment="textEnd"
android:gravity="right" />
</LinearLayout>
</LinearLayout>
The first TextView is the header of sorts, bigger font, etc.. in this area it will hold two fields from the database, the NAME and the LEVEL e.g; SPIDER-MAN (Lvl 5).
The two textfields below it will hold the stats, this area can have more than one for example;
Name Value
------------------------------------
Armor 10%
Speed 5%
... and so on
Each record can be different, some may not have speed, some may not have armor... Maybe this is too difficult to do via ListView, but I need it to be searchable and I do not know what other options I have available for this.
My database fields are as follows:
Id, Name, Level, Stats
an example of the populated db would read;
1, Spider-Man, 5, 3|10%;5|5%;6|3%
I created a example to show how to add variable data to each row in a listview.
This is the Activity implementation:
public class MainActivity extends AppCompatActivity
{
private DynamicAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Simulating variable data per row. Array Adapter instead Cursor Adapter for simplicity
final DataRow[] data =
{
new DataRow("Spider Man", new String[] {"3|10%", "5|25%"}),
new DataRow("Spider Man Brother", new String[] {"3|20%"}),
new DataRow("Other Spider Man", new String[] {"3|22%", "1|12%", "4|7%"})
};
// Assign data to adapter
adapter = new DynamicAdapter(this, data);
// Getting reference to ListView
ListView listViewData = (ListView) findViewById(R.id.listViewData);
// Setting adapter to listView
listViewData.setAdapter(adapter);
}
}
This is how the Adapter will put the data for each row:
public class DynamicAdapter extends ArrayAdapter<DataRow>
{
private final Context context;
private final DataRow[] data;
public DynamicAdapter(Context context, DataRow[] data)
{
super(context, R.layout.row_layout, data);
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_layout, parent, false);
// Get a listview reference
LinearLayout linearLayoutRow = (LinearLayout) rowView.findViewById(R.id.coreLinear);
if(position < data.length)
{
// Put text on header
TextView textHeader = (TextView) rowView.findViewById(R.id.coreHeader);
// Set the header for this row
textHeader.setText(data[position].GetHeader());
// Creating a vertical linear layout to put the data row
LinearLayout linearLayoutData = new LinearLayout(context);
linearLayoutData.setOrientation(LinearLayout.VERTICAL);
// Get column amount for this row.
int columnAmount = data[position].GetColumnsSize();
for(int i = 0; i < columnAmount; i++)
{
// Creating dynamically a TextView for this column.
TextView dynamicData = new TextView(context);
dynamicData.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
// Adding data to TextView
dynamicData.setText(data[position].GetDataAt(i));
// Adding TextView to linearLayout
linearLayoutData.addView(dynamicData);
}
// Adding LinearLayout to LinearLayout on XML
linearLayoutRow.addView(linearLayoutData);
}
return rowView;
}
}
This is the full demo on Github: https://github.com/deinier/DynamicDataOnListView
I hope this help.
This is Relative Layout view which is created dynamically like this :
...
So my question is how to get the values from those dynamic elements and perform some calculation to show result on textview which is also dynamically created.
Thanks in advance guys!!
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numberofPlayersTolayout = (Integer) Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
}
}
}
for (int i = 0; i < maalContainer.getChildCount(); i++) {
View view = maalContainer.getChildAt(i);
EditText ed_item = (EditText) view
.findViewById(R.id.edittext1);
EditText ed_value = (EditText) view
.findViewById(R.id.edittext2);
EditText ed_value1 = (EditText) view
.findViewById(R.id.edittext3);
ed_item.getText().toString().trim();
ed_value.getText().toString().trim();
ed_value1.getText().toString().trim();
}
No need to assign/get ID of any View but the best way is to iterate through the child views of LinearLayout:
int childcount = myLinearLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = myLinearLayout.getChildAt(i);
// do whatever you would want to do with this View
}
A few things here:
You shouldn't be using the Application Context to get the LayoutInflater. You should get that from the Activity Context, otherwise your themes and styles will not be respected.
You shouldn't inflate with null as the second parameter (except in special instances where you don't know the View's parent). Just put the android:layout_width="match_parent" and android:layout_height="wrap_content" attributes in player_entry_item.xml and they will be respected upon inflation.
Store the EditText objects upon inflation as a private array.
With that said, my suggested revision:
private EditText[] mEditTextPlayers;
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = LayoutInflater.from(view.getContext());
mEditTextPlayers = new EditText[numPlayers];
for (int i = 0; i < numPlayers; i++) {
//Pass the parent as the second parameter to retain layout attributes
mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
maalContainer.addView(dynamicEntryView);
}
}
public void goButtonClicked(View view) {
int numberofPlayersTolayout = Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
maalContainer.removeAllViews();
maalText = new EditText[numberofPlayersTolayout];
points = new EditText[numberofPlayersTolayout];
result = new TextView[numberofPlayersTolayout];
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
playerName.setText("Player :" + (i + 1));
maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}
You can do it by using getId() and setId() methods of the View.
dynamicEntryView.setId(i);
and access the view as below by getting the
dynamicEntryView.getId(i);
<?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/player_name_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:text="Player name"
android:textColor="#202020"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/player_item_edittext_point"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Points"
android:inputType="number"
android:textColor="#202020" />
<EditText
android:id="#+id/player_item_edittext_maal"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Maal"
android:inputType="number"
android:textColor="#202020" />
<TextView
android:id="#+id/player_item_textview_result"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00$"
android:textSize="22sp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
Only one id is used. It will loop through according to number of players. Hope it will help you. #user2731584
I trying to write code to highlight the selected value of the list with "Next" button at the bottom of the layout. But for some reason, after every list item, "next" button also shows up. Can someone please help me resolve this problem?
Here is the layout file:
<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:orientation="vertical"
android:id="#+id/questionLayout"
>
<TextView android:id="#+id/txtExample"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000000"
android:background="#FF0000"
/>
<ListView
android:id="#+id/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id = "#+id/next"
android:text="Next"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
/>
<Button
android:id = "#+id/submit"
android:text="Submit"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_weight="50"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
Java Code:
public class updateList extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
int correct_answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<String>();
list.add("Choice One");
list.add("Choice Two");
list.add("Choice Three");
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
ListView listview = (ListView) findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
}
Thanks in advance
SSP
Selected Adaptor class:
public class SelectedAdapter extends ArrayAdapter{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.activity_main, null);
}
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
}else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/*
// to use something other than .toString()
MyClass myobj = (MyClass)this.getItem(position);
label.setText(myobj.myReturnsString());
*/
return(v);
}
}
change your listview in xml as like this
<ListView
android:id="#+id/listExample"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"//===== set maximum heighthere
android:layout_marginBottom="50dp"// === give some space at bottom so that buttons will appear
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
But for some reason, after every list item, "next" button also shows up.
The ListView's row layout is determined by the layout you inflate in getView() or pass to your Adapter's super class if you haven't overridden getView(). Double check this layout and remove the unwanted code.
Addition
The layout for your ListView's items only needs to be one TextView since you only want to display a phrase in each. However you are currently passing your entire main layout, this creates the Buttons, an unused ListView, and everthing else in every row...
Instead use android.R.layout.simple_list_item_1 in getView(), of course you'll need to change the id you pass to findViewById() as well:
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(android.R.layout.simple_list_item_1, null);
}
// get text view
TextView label = (TextView)v.findViewById(android.R.id.text1);
Please watch Android's Romain Guy discuss writing an efficient adapter to speed things up.
I'm trying to implement a custom SimpleCursorAdapter in order to switch out layouts in a ListView but I'm getting very random results while scrolling.
My issue here is that when I scroll up and down, the ListView seemingly by random, mix up the layouts. For instance, a row can have the listview_item_row layout at first but when scrolling in and out of the screen it can be replaced by listview_item_reply_row and back again.
I can't say I've really understood how newView works. I have successfully been able to use bindView to determine if I'm to hide an image in the layout or not but new View is veiled in darkness to me about its implementation and why the list scrolling behaves the way it does.
My goal is to have a list with x amount of items. Depending on if the item is a reply or a new message I want to load a specific layout on that row. Depending on if the row has an image or not I want to show/hide the imageview in the row layout.
What I have omitted in the code are the imports and row layouts. I'm trying to implement this by using Fragments and SimpleCursorAdapter in the v4 support package. The row layouts for the ListFragment are visibly different but contain the same widgets.
The ListView layout:
<RelativeLayout 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/text_feed_header_random"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:padding="4dp"
android:text="Allmänt"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFF" />
<!--
The frame layout is here since we will be showing either
the empty view or the list view.
-->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/text_feed_header_random"
android:layout_above="#+id/footer" >
<!--
Here is the list. Since we are using a ListActivity, we
have to call it "#android:id/list" so ListActivity will
find it
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
<!-- Here is the view to show if the list is emtpy -->
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items."
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
<LinearLayout
android:id="#+id/footer"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/button_random_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gör ett inlägg!" />
<Button
android:id="#+id/button_random_refresh"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Refresh list!" />
</LinearLayout>
</RelativeLayout>
A condensed Fragment using the layout above:
public class RandomFragment extends ListFragment implements LOG {
private DatabaseHelper mDbHelper;
private KarenfeedCursorAdapter mAdapter;
private Cursor mCursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "RANDOMFRAGMENT START!");
mDbHelper = new DatabaseHelper(getActivity());
mDbHelper.open();
mDbHelper.setTable(Posts.TABLE_RANDOM_POSTS);
//TODO: Replace SimpleCursorAdapter with a FragmentList instead...
mCursor = mDbHelper.getAllPostsSortedCursor();
String[] columns = { Posts.COLUMN_ID, Posts.COLUMN_CREATED, Posts.COLUMN_USER, Posts.COLUMN_COMMENT };
int[] to = { R.id.imageItemPhoto, R.id.textItemDate, R.id.textItemUser, R.id.textItemComment };
int flags = 0;
mAdapter = new FeedCursorAdapter(getActivity(), R.layout.listview_item_row, mCursor, columns, to, flags);
this.setListAdapter(mAdapter);
initFeedList(); // This call in the end executes mCursor = mDbHelper.getAllPostsSorted(); mAdapter.changeCursor(mCursor); mAdapter.notifyDataSetChanged();
}
}
The SimpleCursorAdapter that the ListFragment connects to:
public class FeedCursorAdapter extends SimpleCursorAdapter implements LOG {
private Context mContext;
private int mLayout;
public FeedCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
// TODO Auto-generated constructor stub
mContext = context;
mLayout = layout;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
Log.d(TAG, "id: " +id+ " parentId: " +parentId);
int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
if(id == parentId) {
view = inflater.inflate(R.layout.listview_item_row, parent, false);
} else {
view = inflater.inflate(R.layout.listview_item_reply_row, parent, false);
}
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "bindView()");
int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
String date = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_CREATED));
String user = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_USER));
String comment = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_COMMENT));
TextView dateView = (TextView) view.findViewById(R.id.textItemDate);
TextView userView = (TextView) view.findViewById(R.id.textItemUser);
TextView commentView = (TextView) view.findViewById(R.id.textItemComment);
ImageView imageView = (ImageView) view.findViewById(R.id.imageItemPhoto);
dateView.setText(date);
userView.setText(user);
commentView.setText(comment);
if(hasImage == 0) {
imageView.setVisibility(ImageView.GONE);
} else {
String bitmapPath = Environment.getExternalStorageDirectory().getPath() + "/feed/" + id + "_thumb.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imageView.setImageDrawable(bitmapDrawable);
imageView.setVisibility(ImageView.VISIBLE);
}
}
}
after reading your question and the code i think you should know that whenever you scroll a listview up or down it communicates with the adapter to populate the new items that came in to focus and for the case of different row for different data you should make a arraylist that will contain the name or id of the items whose backgrounds are different and then you start making there background according to your need on every call to adapter.
i have a complex xml layout which has list views..a row in the list view contains several text fields which are spaced evenly. i am using textview to store the text and then finally add all the items to the row...its working perfectly fine.
but now i have case where in i am not sure, how many text fields i might get from a webservice. therefore i need to create the textview dynamically on run time, populate them and then insert into the list..
is there anyway to declare,add and populate new textview fields on runtime?
or is there is anyway to implement the spacing between the two fields?
result of first call
__________________________
|_____|_____|_____|______|
result of second call
________________________________
|_____|_____|_____|______|______|
I tried implementing the solution that was provided below (Kenny), but for some reason I am unable to add views into the list.. below is my code
public class HeaderAdapter extends ArrayAdapter<Header> {
final Header[] listSymbols;
private TextView textView;
private LinearLayout row;
public HeaderAdapter(Context context, int symResourceID,
Header[] objects) {
super(context, symResourceID, objects);
listSymbols = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.header_view, parent, false);
Header headerRec = listSymbols[position];
for(int i = 0 ; i < listSymbols.length;i++){
textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
textView.setId(i);
row.add??
}
}
The main activity that calls this
setContentView(R.layout.main);
headerList.add(new Header("Symbol","Quantity","Price","Price Yesterday","52 Week High","52 Week Low","Change","Days Gain","Days Gain %","Returns"));
Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]);
ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList);
headerListView.setAdapter(headerAdapter);
xml layout file..the main file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scrollbars="none"
android:id="#+id/headerHv">
<ListView android:id="#+id/header_listView1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#color/white" android:cacheColorHint="#00000000"
android:smoothScrollbar="true" android:scrollbars="none" />
</HorizontalScrollView>
</LinearLayout>
the file in which the template for the row is defined
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="#+id/headerList" android:layout_width="80dp"
android:layout_height="wrap_content" android:textColor="#000000"
android:typeface="sans" android:textStyle="normal" />
</LinearLayout>
Here is the way i dynamically generate custom buttons from a list, you could do the same thing with textViews:
//Setup Buttons
layout = (LinearLayout)findViewById(R.id.layoutBars);
int count = lBars.size();
for(int i = 0; i< count;i++){
final Bar b = lBars.get(i);
BarButton button = new BarButton(DDTBars.this, R.drawable.barsmall , b.getName().toUpperCase());
button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
button.setId(i);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Run activity passing name of the bar to retrieve data
Intent i = new Intent(DDTBars.this, DDTBar.class);
i.putExtra("name", b.getName());
startActivity(i);
}
});
layout.addView(button);
}
So you could try:
//Setup TextViews
layout = (LinearLayout)findViewById(R.id.mylayout);
int count = myTextList.size();
for(int i = 0; i< count;i++){
TextView txtView = new TextView(this);
txtView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
txtView.setId(i);
layout.addView(txtView);
}
You could do it in code. Declare TextView 's in a loop and use RelativeLayout to position them wrt each other.