如何将 setListAdapter 填充到主布局中?
我正在尝试创建从服务器抓取的企业列表,并将它们显示在列表视图中的搜索框下方。
我正在使用 setListAdapter 来调用我将数组发送到的 arrayAdapter 类。当我运行代码时,它会创建一个包含结果的新屏幕。我希望结果在同一屏幕上。
这是我的主要布局:
<?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>
这是我动态膨胀的布局:
<?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>
这是我整理数据并将其发送到我的类的方式:
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 ));
}
这是 arrayAdapter 类:
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;
}
}
如果我能找到一种方法告诉布局膨胀到 main那太好了,但我似乎无法从此类中获得对 main 的引用。我所能做到的最好的办法就是让通货膨胀发生在第一个屏幕上,但没有结果。我不记得我是怎么做到的。我对 Java 很陌生。
这是最新的代码:
这是出现在 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>
这是新的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;
}
}
这是调用代码:
ViewStub stub = (ViewStub) findViewById(R.id.viewStub1);
stub.inflate();
setListAdapter(new busListAdapter(this, varray ));
任何想法为什么这仍然在空白页面上打开。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ViewStub 来创建布局。您将 ViewStub 放在主布局中您希望布局膨胀的位置,并根据需要设置参数。
示例:
这将膨胀为在layout/layoutName.xml 中定义的布局。
当您想要膨胀视图时,您会找到存根,然后调用其
inflate()
方法:现在新布局位于存根之前所在的位置,您可以找到您的列表并在其上设置适配器:
注意: 没有必要使用ViewStub 来创建列表,它在这里只是用作与膨胀列表视图相关的原始问题,尽管您描述的问题是因为您使用 ListActivity 而不是 Activity (见下文)。
您还应该将 Activity 更改为正常的 Activity 而不是 ListActivity (或根据 文档指南),因为 ListActivity 被设计为将列表作为唯一的显示元素,这就是为什么您的自定义布局现在被列表替换。
我希望这能澄清一些事情。
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:
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:Now the new layout is where the stub previously was, and you can find your list and set the adapter on it:
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.
I hope this clears up some things.