Android ListFragment addfooterview() - android

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.

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

Related

Set ListView onItemClick() to only proceed when item pressed down, not item released

Im stuck. I have a listView item that when pressed calls an other activity. But the activity is being called twice. Once when the list item is pressed and once when it is released.
I looked for examples for filtering the Motion event on a listView item but couldn't find any. I tried to manually add MotionEvent event to the onItemClick() arguments, but that failed.
Can anyone provide an example?
Current Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
generateListView();
}
public void generateListView(final JSONArray relevantGames, ArrayList listViewItems, String teamName) {
ArrayAdapter<String> adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listViewItems);
final ListView lv = findViewById(R.id.gameList);
lv.setVisibility(View.VISIBLE);
lv.setAdapter(adapterList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Want To Put a motion down check
selectedGame = relevantGames.getJSONObject(position);
//Start new intent with putExtra(selectedGame)
});
}
}

ListView creates one more item than needed

I have a ListView which is populated in my MainActivity, but when i want to find the selected item, all of the items appear to have the same position.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_teams);
ListView mainListView;
// Find the ListView in the UI.
mainListView = (ListView) findViewById( R.id.listView );
String values = "Team A vs Team B=Team C vs Team D";
//Matches are send in one long string, and are separated by the = sign.
//This splits the string up and puts it into an array.
String[] array = values.split("=", -1);
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.addAll( Arrays.asList(array) );
ArrayAdapter listAdapter;
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.list_view_style, arraylist);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
}
This makes a list like this:
Team A vs Team B [checkbox]
Team C vs Team D [checkbox]
I have a button and when it is clicked it runs this method:
public void matchStart(View view){
String selectedMatch = String.valueOf(((ListView) findViewById( R.id.listView )).getSelectedItemPosition());
Toast.makeText(SelectTeams.this, selectedMatch, Toast.LENGTH_SHORT).show();
}
However whenever i click the button, the toast displays the same value no matter which item in the listview is selected. Why is this?
You should use the adapters getView method, something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.some_layout, parent,
false);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//here get what you need by the position
}
});
}
}

notifyDataSetChanged() in Fragment is not refreshing listview

I'm trying to display a list of items fetched from a url but I only want to fetch 20 of them at a time... so I've implemented an OnScrollListener to fetch the items when the users is on the last item of the listview.
The items are fetched but my only problem is that the listview is not updated: here's my code so far:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.activity_event_speakers_alphabetically, container,false);
listView = (ListView) parentView
.findViewById(R.id.speakersAlphabeticallyActivityList);
nameOrderedMembers.addDataBatch(communityMembers);
nameOrderedAdapter = DelegateViewStateAdapterFactory
.makeUserListAdapter(getActivity(),
nameOrderedMembers.getSortedData(), DelegateViewStateAdapterFactory.OrderType.NAME);
listView.setAdapter(nameOrderedAdapter);
AQuery aQuery = new AQuery(listView);
aQuery.id(R.id.speakersAlphabeticallyActivityList).scrolled(new EndlessScrollListener());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (nameOrderedAdapter.getItem(arg2) instanceof User) {
User u = (User) nameOrderedAdapter.getItem(arg2);
Intent i = new Intent(getActivity(),
UserProfileFragmentActivity.class);
startActivity(i);
}
}
});
return parentView;
}
Now - in the AsynkTask in the onPostExecute method - i notify the adapter like this:
communityMembers.addAll(newMembers);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
nameOrderedAdapter.addAll(communityMembers);
nameOrderedAdapter.notifyDataSetChanged();
}
});
but the listview doesn't refresh - only if I go back and reopen the activity - the new data is shown.
So how can I notify the adapter that there's new data to display?
call listView.invalidateViews() after notifyDataSetChanged()
add below line after nameOrderedAdapter.notifyDataSetChanged(); :
listView.setAdapter(nameOrderedAdapter);
use
notifyDataSetInvalidated()
method
if you are using any custom ListView or Custom GridView like /
HorizontalListView

how to remove checked items/ specific items from an array list / listview

I have this code i am used to pass an array list to another page and show it as a listview. When the list shows up, i want to be able to check an item and remove it at "button click" which will modify the array.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oppout);
final ListView lv2 = (ListView) findViewById (R.id.custom_list_view);
lv2.setClickable(true);
lv2.setAdapter(new ArrayAdapter<String>(Oppout.this,
android.R.layout.simple_list_item_checked,
Entername.playerList));
lv2.setOnItemClickListener (
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView,
View view,int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "mu"+ selectedPosition,
Toast.LENGTH_SHORT).show();
}
});
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// on click call the adapterview and delete string at selected position.
// This is my problem, am not getting how to call the adapter and deleted
// the selected item/position
int selectedPosition = adapterView.getSelectedItemPosition();
adapterView.remove(player.SelectedPosition);
Intent myIntent = new Intent (view.getContext(), Callacab.class);
startActivityForResult(myIntent, 0);
}
});
}}
contacts.remove(index); //the arraylist you gave it to your adapter
arrayadapter.remove(index); // this is your adapter that you give it to the listview
arrayadapter.notifyDataSetChanged();
//you can delete from your arraylist or your adapter and then notifyDataSetChanged(); to make the effect happen in your listview....it better to delete from arraylist and its indexs will be the arg2 in your listview item listener

ANDROID: on Notification received, I want to refresh the listView with the new data

Ths activity it's call from TabActivity.
And I want: if I receive new notification , I want to refresh the list of the current Activity.
public class TestListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
ListView lv = null;
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.test_list, ListUtil.asStringList(TestServiceUtil.getTests())));
lv = getListView();
lv.setSelector(R.drawable.listindicator);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(TestListActivity.this, TestViewActivity.class);
// Next create the bundle and initialize it
Bundle bundle = new Bundle();
// Add the parameters to bundle as
bundle.putLong("testId", TestServiceUtil.getTests().get(position).getTestId());
// Add this bundle to the intent
intent.putExtras(bundle);
// Start next activity
TestListActivity.this.startActivity(intent);
}
});
}
}
I need just example, how i can make the refresh of the list.
I recommend using notifyDataSetChanged() on the ArrayAdapter.

Categories

Resources