简体   繁体   English

ArrayAdapter正在为ListView中的每个项重复UI标头

[英]ArrayAdapter is repeating UI Header for every item in ListView

I am facing an issue in showing a list of items in the UI. 我在UI中显示项目列表时遇到问题。

My UI layout page layout: 我的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>

In the Class, onSuccess() method is called once the list is retrieved from a remote system and populate the TextView in the UI. 在类中,一旦从远程系统检索列表并在UI中填充TextView ,就会调用onSuccess()方法。

But,the Header part included in the layout is getting repeated for every record. 但是,布局中包含的标题部分会针对每条记录重复。 Am I missing anything here?? 我在这里遗漏了什么?

For instance....Actual result is 例如....实际结果是

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

Expected Result is 预期结果是

<Header Button for LogOut>
Item1
Item2

Code is shown below: 代码如下所示:


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));

    }

}

Please help if I am missing anything. 如果我遗漏任何东西,请帮忙。

I'm guessing that the XML you provided is used as the row layout, which means: 我猜你提供的XML被用作行布局,这意味着:

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

is included in every row (like what you are seeing). 包含在每一行中(就像你所看到的那样)。

If you want to add a header to your ListView try: 如果要向ListView添加标题,请尝试:

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

and remove the include element from the row layout. 并从行布局中删除include元素。

Make your header a separate XML layout and add to your listview using addHeaderView. 将标题设置为单独的XML布局,并使用addHeaderView添加到列表视图中。 IE: IE:

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

hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ListView和ArrayAdapter中删除项目 - Remove item from ListView and ArrayAdapter 列表视图中的项目重复 - Item in listview is repeating Android:按钮单击以显示datepicker,然后从datepicker.Button中的日期位于ArrayAdapter中的每个listview项目中 - Android:Button Click to show datepicker and then date from datepicker.Button is in every listview item in ArrayAdapter 如何将 onclick 添加到列表视图中的每个按钮,填充自定义数组适配器,从数组中删除该项目并更新它? - How to add an onclick to every button in a listview, populated with a custom arrayadapter, that deletes that item from the array and updates it? ListView clear() 清除 ArrayAdapter 中的每个 List - ListView clear() clearing every List in the ArrayAdapter Android:如何从ListView和arrayAdapter中删除项目 - Android: how to remove an item from a listView and arrayAdapter 带有ArrayAdapter和ViewHolder的ListView将图标添加到错误的项目 - ListView with ArrayAdapter and ViewHolder adding icons to the wrong item 通过ArrayAdapter将ImageView添加到listView项 - Adding ImageView to listView item via ArrayAdapter 使用ArrayAdapter / SimpleAdapter在ListView上使一项变为粗体 - Make one item bold on ListView with ArrayAdapter/SimpleAdapter ArrayAdapter在列表视图上仅添加一项 - ArrayAdapter add only one item on listview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM