【问题标题】:ArrayAdapter is repeating UI Header for every item in ListViewArrayAdapter 为 ListView 中的每个项目重复 UI Header
【发布时间】:2012-07-20 16:59:09
【问题描述】:

我在 UI 中显示项目列表时遇到问题。

我的 UI 布局页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ListOfRecordsContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#454545"
    android:orientation="vertical" >

    <include
        android:layout_weight="1"
        layout="@layout/header" />

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ListOfRecords"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.01"
        android:padding="1dp"
        android:textSize="12sp" >
    </TextView>

</LinearLayout>

在类中,一旦从远程系统检索到列表并在 UI 中填充 TextView,就会调用 onSuccess() 方法。

但是,布局中包含的 Header 部分对于每条记录都会重复。我在这里有什么遗漏吗??

例如....实际结果是

<Header Button for LogOut>
Item1
<Header Button for LogOut>
Item2

预期结果是

<Header Button for LogOut>
Item1
Item2

代码如下:


public class ItemMainPage extends ListActivity{

    private String[] AccountName;
    private String[] AccountIds;
    private RestClient client;
    private String apiVersion;

    LinearLayout layout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView lv = getListView();
        apiVersion = getString(R.string.api_version);

        lv.setTextFilterEnabled(true);

        /*layout = (LinearLayout) View.inflate(this, R.layout.list_item_main_page, null);
        Button buyButton = new Button(this);
        buyButton.setText("Search");
        layout.addView(buyButton);*/


    }

    @Override
    public void onResume(){
        super.onResume();

        // Login options
        String accountType = getString(R.string.account_type);
        LoginOptions loginOptions = new LoginOptions(
                null, // gets overridden by LoginActivity based on server picked by uuser 
                ForceApp.APP.getPasscodeHash(),
                getString(R.string.oauth_callback_url),
                getString(R.string.oauth_client_id),
                new String[] {"api"});

        new ClientManager(this, accountType, loginOptions).getRestClient(this, new RestClientCallback() {
            //@Override
            public void authenticatedRestClient(RestClient client) {
                if (client == null) {
                    ForceApp.APP.logout(ItemMainPage.this);
                    return;
                }
                ItemMainPage.this.client = client;
                getAccountList();
            }
        });
        EventsObservable.get().notifyEvent(EventType.RenditionComplete);
    }   

    private void getAccountList(){

        try {

            String soql = "select id, name from Account LIMIT 2";
            RestRequest request = RestRequest.getRequestForQuery(apiVersion, soql);

            client.sendAsync(request, new AsyncRequestCallback() {

                //@Override
                public void onSuccess(RestRequest request, RestResponse response) {
                    try {
                        if (response == null || response.asJSONObject() == null)
                            return;

                        JSONArray records = response.asJSONObject().getJSONArray("records");

                        if (records.length() == 0)
                            return;

                        AccountName = new String[records.length()];
                        AccountIds = new String[records.length()];

                        for (int i = 0; i < records.length(); i++){
                            JSONObject Account = (JSONObject)records.get(i);
                            AccountName[i] = Account.getString("Name");
                            AccountIds[i] = Account.getString("Id");
                        }
                        //ArrayAdapter<String> ad = new ArrayAdapter<String>(ItemMainPage.this, 
                        //                                                 R.layout.list_item_main_page, 
                        //                                                 AccountName);
                        ArrayAdapter<String> ad = new ArrayAdapter<String>(ItemMainPage.this, R.layout.list_item_main_page, R.id.ListOfRecords, AccountName); 
                        setListAdapter(ad);
                        Log.d("Adapter", ad.toString());
                        //EventsObservable.get().notifyEvent(EventType.RenditionComplete);
                    } catch (Exception e) {
                        e.printStackTrace();
                        displayError(e.getMessage());
                    }
                }

                //@Override
                public void onError(Exception exception) {
                    displayError(exception.getMessage());
                    EventsObservable.get().notifyEvent(EventType.RenditionComplete);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            displayError(e.getMessage());
        }
    }


    private void displayError(String error) {
        //ArrayAdapter<String> ad = new ArrayAdapter<String>(   AccountList.this, R.layout.list_item, new String[]{"Error retrieving Account data - " + error});
        //setListAdapter(ad);
      ((TextView) findViewById(R.id.welcome_text)).setText(getString(R.string.welcome, client.getClientInfo().username));

    }

}

如果我遗漏了什么,请帮忙。

【问题讨论】:

    标签: android listview user-interface android-arrayadapter


    【解决方案1】:

    我猜你提供的 XML 被用作行布局,这意味着:

    <include
        android:layout_weight="1"
        layout="@layout/header" />
    

    包含在每一行中(就像您所看到的一样)。

    如果您想在 ListView 中添加标题,请尝试:

    View header = getLayoutInflater().inflate(R.layout.header, null);
    listView.addHeaderView(header);
    

    并从行布局中删除 include 元素。

    【讨论】:

    • 非常感谢。该解决方案有效,但是说我需要在列表上方有一个搜索栏来搜索列表中的项目,另外还有一个列表的标题......这怎么可能?
    • 有很多方法可以做到这一点,事实上,这里有一个完整的Android Developer Article主题。如果您需要具体帮助,请随时提出新问题,提供与您在此处所做的一样多的详细信息。
    • 非常感谢。非常感谢。
    【解决方案2】:

    使您的标题成为一个单独的 XML 布局并使用 addHeaderView 添加到您的列表视图。即:

        View header = (View)getActivity().getLayoutInflater().inflate(R.layout.listview_header_row, null);
        lv.addHeaderView(header);
    

    希望这会有所帮助!

    【讨论】:

    • 非常感谢。该解决方案有效,但是说我需要在列表上方有一个搜索栏来搜索列表中的项目,另外还有一个列表的标题......这怎么可能?
    猜你喜欢
    • 2014-08-20
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2014-11-22
    • 2010-11-22
    • 1970-01-01
    相关资源
    最近更新 更多