如何将 setListAdapter 填充到主布局中?

发布于 2024-12-22 06:16:08 字数 9763 浏览 0 评论 0原文

我正在尝试创建从服务器抓取的企业列表,并将它们显示在列表视图中的搜索框下方。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2024-12-29 06:16:08

您可以使用 ViewStub 来创建布局。您将 ViewStub 放在主布局中您希望布局膨胀的位置,并根据需要设置参数。
示例:

 <ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/newId"
           android:layout="@layout/layoutName"
           android:layout_width="match_parent"
           android:layout_height="140dp" />

这将膨胀为在layout/layoutName.xml 中定义的布局。
当您想要膨胀视图时,您会找到存根,然后调用其 inflate() 方法:

ViewStub stub = (ViewStub) findViewById(R.id.stub);
stub.inflate();

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

ListView list = (ListView) findViewById(R.id.listId);
list.setAdapter(yourAdapter);

注意: 没有必要使用ViewStub 来创建列表,它在这里只是用作与膨胀列表视图相关的原始问题,尽管您描述的问题是因为您使用 ListActivity 而不是 Activity (见下文)。

您还应该将 Activity 更改为正常的 Activity 而不是 ListActivity (或根据 文档指南),因为 ListActivity 被设计为将列表作为唯一的显示元素,这就是为什么您的自定义布局现在被列表替换。

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

我希望这能澄清一些事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文