How to inflate setListAdapter into main layout?
I am trying to create a list of businesses grabbed from a server and display them beneath the search box in a listview.
I am using setListAdapter to call my arrayAdapter class that I send the array to. When I run the code it creates a new screen with the resuts in. I want the results on the same screen.
Here is my main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FF9900"
android:textColor="#000000"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="60px"
android:scaleType="fitXY"
android:src="@drawable/logo" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/rel1" android:gravity="right">
<AutoCompleteTextView android:id="@+id/autocomplete_classif"
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true"/>
<Button android:id="@+id/button1" android:text="Search" android:onClick="myClickHandler" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
</RelativeLayout>
<EditText android:layout_height="wrap_content" android:id="@+id/editText2"
android:layout_gravity="center_horizontal" android:text="" android:layout_width="match_parent" android:visibility="gone">
</EditText>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/mylocation"
android:lines="1" android:textColor="#000000" android:gravity="center"
android:scrollbars="vertical">
</TextView>
<ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content"
android:layout_gravity="center_horizontal" android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" android:visibility="gone"> </ProgressBar>
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/pagetext" android:lines="50"
android:textColor="#000000"
android:background="#CCCCCC"
android:padding="5px"
android:scrollbars="vertical">
</TextView>
</LinearLayout>
Here is the layout I am inflating dynamincally:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="10px"
android:layout_height="140px" android:id="@+id/rlt_main"
android:background="#FFFFFF"
android:textColor="#000000">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/bus_name"
android:text="Name" ></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/description"
android:layout_below="@+id/bus_name" android:layout_marginLeft="10px"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/distance"
android:layout_below="@+id/description" android:layout_marginLeft="10px"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/longitude"
android:layout_below="@+id/distance" android:layout_marginLeft="10px"></TextView>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerVertical="true"
android:id="@+id/iv_forward" android:background="@drawable/forward_arrow"
android:layout_alignParentRight="true"></ImageView>
</RelativeLayout>
This is how I collate the data and send it to my class:
if(found) {
jArray = new JSONObject(result);
JSONArray jResult = jArray.getJSONArray("result");
StringBuilder output = new StringBuilder();
ArrayList<ArrayList<String>> varray = new ArrayList<ArrayList<String>>();
for(int i=0;i < jResult.length();i++){
JSONObject e = jResult.getJSONObject(i);
output.append(e.getString("webid") + "n");
output.append(e.getString("webDistance") + "n");
String longi = e.getString("webLongtitude");
String lati = e.getString("webLatitude");
String geoURI = String.format("geo:%s,%s", lati, longi);
output.append(geoURI+"n");
output.append("--------------------------------------------n");
ArrayList<String> det = new ArrayList<String>();
det.add(e.getString("webEntryName"));
det.add(e.getString("webAddress"));
det.add(e.getString("webDistance"));
det.add(e.getString("webLongtitude"));
det.add(e.getString("webLatitude"));
varray.add(det);
}
setListAdapter(new busListAdapter(this, varray ));
}
And this is the arrayAdapter class:
package com.dentons.dentonsweb;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class busListAdapter extends ArrayAdapter<ArrayList<String>> {
private final Activity context;
private final ArrayList<ArrayList<String>> varray;
private ArrayList<String> varray2;
public busListAdapter(DentonswebActivity context, ArrayList<ArrayList<String>> varray) {
super(context, R.layout.business_list_item, varray);
this.context = context;
this.varray = varray;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row=inflater.inflate(R.layout.business_list_item, parent, false);
TextView label=(TextView)row.findViewById(R.id.bus_name);
TextView description=(TextView)row.findViewById(R.id.description);
TextView distance=(TextView)row.findViewById(R.id.distance);
TextView longitude=(TextView)row.findViewById(R.id.longitude);
varray2 = varray.get(position);
label.setText(varray2.get(0));
description.setText(varray2.get(1));
distance.setText(varray2.get(2));
longitude.setText(varray2.get(3));
return row;
}
}
If I could find a way of telling the layout to inflate into main that would be great but I don't seem to be able to get a reference to main from within this class. The best I have managed is for the inflation to happen within first screen but with no result. I can't remember how I did this. I am quite new to Java.
This is the latest code:
This appear in main.xml TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/pagetext" android:lines="50" android:textColor="#000000" android:background="#CCCCCC" android:padding="5px" android:scrollbars="vertical"> /TextView>
This is the new busListAdapter:
package com.dentons.dentonsweb;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class busListAdapter extends ArrayAdapter<ArrayList<String>> {
private final Activity context;
private final ArrayList<ArrayList<String>> varray;
private ArrayList<String> varray2;
public busListAdapter(DentonswebActivity context, ArrayList<ArrayList<String>> varray) {
super(context, R.layout.business_list_item, varray);
this.context = context;
this.varray = varray;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row=inflater.inflate(R.layout.business_list_item, parent, false);
TextView label=(TextView)row.findViewById(R.id.bus_name);
TextView description=(TextView)row.findViewById(R.id.description);
TextView distance=(TextView)row.findViewById(R.id.distance);
TextView longitude=(TextView)row.findViewById(R.id.longitude);
varray2 = varray.get(position);
label.setText(varray2.get(0));
description.setText(varray2.get(1));
distance.setText(varray2.get(2));
longitude.setText(varray2.get(3));
return row;
}
}
This is the calling code:
ViewStub stub = (ViewStub) findViewById(R.id.viewStub1);
stub.inflate();
setListAdapter(new busListAdapter(this, varray ));
Any ideas why this is still opening on a blank page.
You can use a ViewStub to create your layout. You put your ViewStub in the main layout where you want your layout to be inflated, and set the parameters as you want them to be.
Example:
<ViewStub android:id="@+id/stub"
android:inflatedId="@+id/newId"
android:layout="@layout/layoutName"
android:layout_width="match_parent"
android:layout_height="140dp" />
This will be inflated to the layout defined in layout/layoutName.xml. When you want to inflate the view you would find the stub and then call its inflate()
method:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
stub.inflate();
Now the new layout is where the stub previously was, and you can find your list and set the adapter on it:
ListView list = (ListView) findViewById(R.id.listId);
list.setAdapter(yourAdapter);
Note: it is not necessary to use the ViewStub to create a list, it is simply used here as your original question related to inflating a list view, although the problem you describe are because you use a ListActivity instead of an Activity (See below).
You should also change your activity to a normal Activity instead of a ListActivity (or change your layout according to the documentation guidelines) as the ListActivity is designed to have a list as the only display element, and that is why your custom layout is replaced with the list now.
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)
I hope this clears up some things.
链接地址: http://www.djcxy.com/p/93246.html上一篇: 宽度和布局