Android 如何将setListAdapter充气到主布局中?

Android 如何将setListAdapter充气到主布局中?,android,Android,我试图创建一个从服务器抓取的企业列表,并在listview的搜索框下显示它们 我正在使用setListAdapter调用我将数组发送到的arrayAdapter类。当我运行代码时,它会创建一个新的屏幕,显示Results。我希望结果显示在同一屏幕上 以下是我的主要布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

我试图创建一个从服务器抓取的企业列表,并在listview的搜索框下显示它们

我正在使用setListAdapter调用我将数组发送到的arrayAdapter类。当我运行代码时,它会创建一个新的屏幕,显示Results。我希望结果显示在同一屏幕上

以下是我的主要布局:

<?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>
你知道为什么这仍然在空白页上打开吗。

你可以使用来创建布局。您可以将ViewStub放在主布局中,在主布局中调整布局,并根据需要设置参数。
例如:

现在,新的布局就是以前存根所在的位置,您可以找到列表并在其上设置适配器:

ListView list = (ListView) findViewById(R.id.listId);
list.setAdapter(yourAdapter);
注意:无需使用ViewStub创建列表,此处仅将其用作与扩展列表视图相关的原始问题,尽管您描述的问题是因为您使用了ListActivity而不是Activity(请参见下文)

您还应该将“活动”更改为普通而不是(或根据更改布局),因为ListActivity旨在将列表作为唯一的显示元素,这就是为什么现在将自定义布局替换为列表的原因

ListActivity具有由单个, 屏幕中央的全屏列表。但是如果你愿意的话,, 您可以通过设置自己的视图布局来自定义屏幕布局 在onCreate()中使用setContentView()。要做到这一点,你必须有自己的观点 包含一个id为“@android:id/list”的ListView对象(如果需要,则为list) 它在代码中)


我希望这能澄清一些问题。

你能发布增加listview的代码吗?在任何情况下,您都可以在主布局中使用ViewStub并将其膨胀。@Jave-它只是这一行:setListAdapter(new-busListAdapter(this,varray));那么,您使用的是ListActivity?啊,对不起,是的,它最初是listview,但我认为它是listview的一种。那么,如何使用此ViewStub?我现在要去做一些研究,但不知道你能否给我举个例子。真的很感谢你的帮助。你可以先看看这个链接:我看看明天是否能举个例子。谢谢你,等我有时间的时候我会试试的。我唯一关心的是将多维数组传递给listview。不客气,您的适配器似乎能够处理多维数组,所以这不会引起任何问题。我已经尝试过了,但它仍然在一个空白屏幕上打开。请参阅下面的代码:有关新的有问题的代码,请参阅原始问题中的新代码。谢谢。您仍然在活动本身上调用setListAdapter(),而不是在listview上。。。如果您有一个ListView,请使用findViewById()获取它,然后在该ListView上调用setAdapter()。
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 ));
}
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;
    }
}
<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>
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 ));
 <ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/newId"
           android:layout="@layout/layoutName"
           android:layout_width="match_parent"
           android:layout_height="140dp" />
ViewStub stub = (ViewStub) findViewById(R.id.stub);
stub.inflate();
ListView list = (ListView) findViewById(R.id.listId);
list.setAdapter(yourAdapter);