How to make a view appear below - android

Hey I want to make a view appear below the last that has been created.
It is showing like this:
I want to make the next view show below the last one, so for each new view added, it shows below. Understand?
Here is my code. The xml file and the java file.
listitem.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
RatedCalls.java
public class RatedCalls extends Activity {
private static final String LOG_TAG = "RatedCalls";
private TableLayout table;
private CallDataHelper cdh;
private TableRow row;
private TableRow row2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
Log.i(LOG_TAG, "calling from onCreate()");
cdh = new CallDataHelper(this);
startService(new Intent(this, RatedCallsService.class));
Log.i(LOG_TAG, "Service called.");
Log.i(LOG_TAG, "before call fillList");
List<String> ratedCalls = new ArrayList<String>();
ratedCalls = this.cdh.selectTopCalls();
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int i = 0; i < 1; i++) {
View item = inflater.inflate(R.layout.listitem, null);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText(ratedCalls.get(0));
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText(ratedCalls.get(1));
ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}

Your problem is that you should be using listitem.xml from a list adapter. What you're doing is, first setting your content view to an instance of listitem.xml, then trying to insert an instance of listitem INTO listitem's first LinearLayout. You should use listitem.xml as the view that you inflate in your custom adapter's getView() method. See the QuoteAdapter class from this question for an example, or just Google search "custom ArrayAdapter Android" for tons of results on this topic.

To work so you need to create two different xml for each inflater
In your layout folder,new xml layout/main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
Then your layout/main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linearId"
android:orientation="vertical"
>
<TextView android:id="#+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="#+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
Then you can set you activity like this
private int ELEMENTINEACHROW=2 ;
private int NOOFROW = 4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int j = 0; j < NOOFROW; j++) {
View itemMain = inflater.inflate(R.layout.main, null, false);
TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount);
rowCount.setText("Row:"+(j+1));
LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2);
for (int i = 0; i < ELEMENTINEACHROW; i++) {
View item = inflater.inflate(R.layout.main2, null, false);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText("test");
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText("good");
ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT);
}
ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT);
}
}
Hope it give you the idea.

mmm...I did not understand fully what is required, but may be you need to add android:orientation="vertical" to LinearLayout with id="linearId".

In your xml file, set the android:orientation attribute to vertical on your <LinearLayout/>:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
android:orientation="vertical"/>
Check out the LinearLayout class documentation and the orientation documentation for more information.

Related

Dynamic edit text on button add and remove

I already read tun of code here or anywhere else. And i am realy confused. I found on web simple prog on add and remove edittext with remove button. But when i add text view more then ones, it has same id, but i need that text what is wrote inside. And if was possible i need next id with letters, not in number.
here is my code:
public class MainActivity extends AppCompatActivity {
private LinearLayout parentLinearLayout1;
private LinearLayout parentLinearLayout2;
TextView a1;
TextView txt;
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
parentLinearLayout1 = (LinearLayout) findViewById (R.id.parent_linear_layout1);
parentLinearLayout2 = (LinearLayout) findViewById (R.id.parent_linear_layout2);
a1 = (TextView) findViewById (R.id.textView5);
txt = (EditText) findViewById (R.id.txt);
}
public void onAddField( View v ) {
LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate (R.layout.field, null);
parentLinearLayout1.addView (rowView, parentLinearLayout1.getChildCount () );
Button addbtn = (Button) findViewById (R.id.add_field_button);
int id = addbtn.getId ();
a1 = (TextView) findViewById (R.id.textView5);
a1.setText (id + "");
}
public void onDelete( View v ) {
parentLinearLayout1.removeView ((View) v.getParent ());
Button del = (Button) findViewById (R.id.deletebuton1);
// c=(EditText)findViewById (R.id.number_edit_text);
int id = del.getId ();
a1 = (TextView) findViewById (R.id.textView5);
a1.setText (id + "");
}
}
activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Město" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_linear_layout1"
android:layout_width="374dp"
android:layout_height="120dp"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<EditText
android:id="#+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="text" />
<Button
android:id="#+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#555"
android:onClick="onAddField"
android:paddingLeft="5dp"
android:text="Add Field"
android:textColor="#FFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
and field xml:
<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="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:inputType="text" />
<Button
android:id="#+id/deletebuton1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete">
</LinearLayout>
Its even possible or this code is unseless and i must start again?
Thanks for answer.

How to add custom layout to linear layout in scrollView dynamicaly

I want to show a screen with text area that is vertically scrolling and image area that is horizontally scrolling.
I have created two layouts:
activity_display.xml
<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"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/displaySV1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:id="#+id/displayLL1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<HorizontalScrollView
android:id="#+id/displaySV2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:id="#+id/displayLL2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and custom_layout.xml : I want to add the custom layout in the linear layout displayLL1.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customFrame">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/customTV1"
android:layout_gravity="start|top"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="Test text"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customTV2"
android:layout_gravity="top|center_horizontal"
android:text="Test Value"
android:layout_marginTop="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDIT"
android:id="#+id/editButton"
android:layout_gravity="right|top" />
</FrameLayout>
Display activity calls:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
for(int i = 0; i < count1; i++)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame);
TextView label = (TextView) findViewById(R.id.customTV1);
TextView value = (TextView) findViewById(R.id.customTV2);
label.setText("Test");
value.setText("Test");
linearLayout.addView(custLayout);
}
for (int x = 0; x < count2; x++)
{
final ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout2.addView(image);
}
I am able to populate the image part if text part is commented out. But with the above code nothing is displayed on the screen.
Any idea where I might be going wrong?
Thank you!
Probably you have a NullPointerException.
Because there is no customTV1 and customTV2 ids in your activity xml, so calling findViewById(R.id.customTV1); will return a null value, and label.setText("Test"); will throw a NullPointerException.
Actually calling FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame); will not add your custom xml to the activity one, you need to inflate it.
Try this code:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
LayoutInflater inflater = LayoutInflater.from(yourActivity.this);
for(int i = 0; i < count1; i++){
View custLayout= inflater.inflate(R.layout.custom_layout, null, false);
TextView label = (TextView) custLayout.findViewById(R.id.customTV1);
TextView value = (TextView) custLayout.findViewById(R.id.customTV2);
label.setText("Test");
value.setText("Test");
linearLayout.addView(custLayout);
}
for (int x = 0; x < count2; x++){
ImageView image = new ImageView(this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout2.addView(image);
}

How to dispaly result list into TableLayout row in android?

I have a little bit problem that displaying list data to TextView in Android. My scenario is I have a TableLayout with Default One TableRow. Inside table row, I has been created new LinearLayout. Then, Four TextView created inside LinearLayout. I adding some default value to this textview. My default value is
1, 2014-02-05, S02, 20
Here my code snippet for above scenari0.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/sync_history_table_color"
android:id="#+id/history_table"
android:orientation="vertical"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="1dp"
android:id="#+id/row_start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp" android:id="#+id/row_linear" android:layout_weight="1"
android:background="#color/isp_home_color">
<TextView android:layout_width="0dp" android:layout_height="match_parent" android:text="#string/sync_default_no"
android:id="#+id/history_display_no" android:layout_weight="0.165"
android:gravity="center_vertical|left" android:paddingLeft="10dp"
android:textColor="#color/sync_history_text_color"/>
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_date"
android:id="#+id/history_display_date"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_order"
android:id="#+id/history_display_orderid"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="#string/sync_default_qty"
android:id="#+id/history_display_quantity" android:layout_weight="0.5" android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/sync_history_text_color"/>
</LinearLayout>
</TableRow>
</TableLayout>
Actually, I want to create dynamically TableRow, LinearLayout and TextView. Then, I put the list result to add these. The list result come from sqlite db. But, I can't get what I want. Here my code for looping the list and display the result. But, I got my some default value. Please pointing to me how can I get this. Thanks.
Here snippet for display list.
public void displayHistory(){
List<IspHistoryListObject> historyList = sqlaccess.getAllItems();
TableLayout showRow = (TableLayout) findViewById(R.id.history_table);
int count = 0;
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(this);
TextView tv_sync_date = new TextView(this);
TextView tv_sync_orderid = new TextView(this);
TextView tv_sync_qty = new TextView(this);
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
}
try this way here I gave sample or demo code you have to modify as per your requirement and still have problem let me know
main layout
1. table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
table row layout
2. table_item.xml
<?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"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/history_display_no"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_date"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_orderid"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/history_display_quantity"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
activity class
3. Activity
public class MyActivity extends Activity {
private TableLayout tableLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
for (int i=0;i<5;i++){
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView history_display_no = (TextView) tableRow.findViewById(R.id.history_display_no);
TextView history_display_date = (TextView) tableRow.findViewById(R.id.history_display_date);
TextView history_display_orderid = (TextView) tableRow.findViewById(R.id.history_display_orderid);
TextView history_display_quantity = (TextView) tableRow.findViewById(R.id.history_display_quantity);
history_display_no.setText(""+(i+1));
history_display_date.setText("2014-02-05");
history_display_orderid.setText("S0"+(i+1));
history_display_quantity.setText(""+(20+(i+1)));
tableLayout.addView(tableRow);
}
}
}
check out context hierachy
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(showRow.getContext());
LinearLayout ll = new LinearLayout(tr.getContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(ll.getContext());
TextView tv_sync_date = new TextView(ll.getContext());
TextView tv_sync_orderid = new TextView(ll.getContext());
TextView tv_sync_qty = new TextView(ll.getContext());
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
With listview you can easily do this:
ActivityMain.java
ListView lst = (ListView) findViewById(R.id.listView);
View header = getLayoutInflater().inflate(R.layout.list_header, null);
lst.addHeaderView(header);
lst.setAdapter(new CustomerListAdapter(this,4));
list_header.xml is header layout

Dynamic layout inflator within a loop

I want to display x number of an xml layout within an activity based upon a loop of data from a parcelable array. For this example, I've changed the array size to a fixed value of 3.
The xml of the layout I am wanting to inflate is:
<?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:orientation="vertical" >
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup android:id="#+id/rg_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:tag="1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:tag="0" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't Know"
android:tag="3" />
</RadioGroup>
</LinearLayout>
The xml of the parent page is:
<?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">
<LinearLayout
android:id="#+id/ll_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
This is the code for the Activity:
public class PainRecord_FollowUp extends Activity {
LinearLayout llContent;
Context context;
PainDataItem pd_in; //this is the Parcelable extra received
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.painrecord_followup);
Bundle extras = getIntent().getExtras();
if (extras != null) {
pd_in = extras.getParcelable("m_PainDataItem");
}
LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll_content);
List views = new ArrayList();
for(int i=0; i<3; i++){
View view = layoutInflator.inflate(R.layout.row_painrecord_followup, null);
TextView textView = (TextView) view.findViewById(R.id.tv_title);
textView.setText("did x help " + i);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
insertPoint.addView((View) views.get(i));
}
}
Instead of showing each layout underneath each other, it displays it like this:
If I change to LayoutParams.FILL_PARENT, then just the [0] loop value shows.
Any advice greatly received.
Thanks
Set your LinearLayout orientation to vertical
<LinearLayout
android:id="#+id/ll_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>

How use ListView in android Tab?

Good day.
In my app I have three tab (one Activity extend TabActivity and others activitys provides access to content). In first tab I have ImageView, a few TextView and it is works. But when I add ListView and in activity that contain ListView I add a few rows it was not show in may tab.
Can someone tell me where I was wrong? Here my code:
In StartActivity:
intent = new Intent().setClass(this, GoodsAndShopsActivity.class);
spec = tabHost.newTabSpec("shops").setIndicator("Shops",
res.getDrawable(R.drawable.ic_tab_shops))
.setContent(intent);
tabHost.addTab(spec);
In GoodsAndShopsActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.descriptions);
m_shopsLayout = (ListView) findViewById(R.id.shops);
m_shopList = new ArrayList<Shop>();
m_shopAdapter = new ShopAdapter(m_shopList, this);
m_shopsLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
m_shopsLayout.setAdapter(m_shopAdapter);
for (int i = 0; i<3; i++) {
m_shopList.add(new Shop("new description"));
m_shopAdapter.notifyDataSetChanged();
}
}
In class that extends BaseAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = m_inflater.inflate(R.layout.shop, null);
holder = new ViewHolder();
holder.descriptions = (TextView) convertView.findViewById(R.id.shop);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String textOnView = m_shops.get(position).getDescription();
holder.descriptions.setText(textOnView);
return convertView;
}
static class ViewHolder{
TextView descriptions;
}
And my xml where define ListView (Sorry that so much):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/full_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:src="#drawable/icon">
</ImageView>
<LinearLayout
android:id="#+id/short_info"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/icon"
android:layout_alignParentRight="true">
<TextView
android:id="#+id/name_for_good"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Наименование товара">
</TextView>
<TextView
android:id="#+id/best_price"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Лучшая цена: ">
</TextView>
<TextView
android:id="#+id/worst_price"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="Худшая цена: ">
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/description_and_shop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/full_info">
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Большое подробное описание товара с всякими деталями, нюансами и т.п.">
</TextView>
<ScrollView
android:id="#+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/shops"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</ScrollView>
</LinearLayout>
UPDATE: After coffe brake I was find mistake: in my xml I add in last LinearLayout "orientation=vertical" and my rows appeared.
SOLVED
Also, remember that the ListView is already scrollable, so you shouldn't need to put it under ScrollView.
Im glad you solved your problem youself :)

Categories

Resources