user2880056
user2880056

Reputation: 121

Android ListFragment addfooterview()

I have been attempting to add a footer button to the end of my list. My list is working appropriately however I cannot figure out why I cannot add the footer view.
My end goal is to inflate the view and add a footer button via xml however I need to get this to work at least first.

public void onActivityCreated(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // storing string resources for users inventory into Array 
    String[] adobe_products = getResources().getStringArray(R.array.adobe_products);

    ListView lv = getListView();
    // Binding Array to ListAdapter        
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.inventory_list, R.id.label, adobe_products);

    // LoadMore button
    Button btnScan = new Button(getActivity());
    btnScan.setText("Scan Inventory");

    // Adding Load More button to list view at bottom
    lv.addFooterView(btnScan); 

    this.setListAdapter(adapter);

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String product = ((TextView) view).getText().toString();

          // Launching new Activity on selecting single List Item
          //Intent i = new Intent(PhotosFragment.this, InventoryItem.class);
          // sending data to new activity
          //i.putExtra("product", product);
          //startActivity(i);
      }
    });

    btnScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Starting a new async task
            //Intent send_Scan=new Intent(PhotosFragment.this, ScanActivity.class);
            //PhotosFragment.this.startActivity(send_Scan);
        }
    });
}

Edit : When running the application it seems to be stuck in an infinite loop. Consistently loading.

Upvotes: 0

Views: 736

Answers (1)

Victor Pinto
Victor Pinto

Reputation: 447

You can't get ListView in onCreateView.

Use in onStart():

@Override
public void onStart() {
    super.onStart();
    LayoutInflater inflater = getActivity().getLayoutInflater();
    getListView().addFooterView(inflater.inflate(R.layout.ly_footer_acreditacao, null));
}

Upvotes: 1

Related Questions