How to add Listview as footer in listview in android? - android

I have Mainactivity which contain listview with some item and I want to add another listview in that listview as footer.How can I add this

Its not recommended to add a ListView as a footer of another ListView. You might consider making a list of objects containing both list and pass the list to your Adapter.
So if you merge two lists in a common format, you need to be tricky for the layout selection for each item in your list. Let me show you an example of a common class.
public class CommonClass {
// Set null values initially.
private ClassA mFirstListClass = null;
private ClassB mSecondListClass = null;
}
Now take an ArrayList of this object to pass it to your Adapter. In your bindView check if the item is a ClassA object or ClassB object (as you can easily determine them by checking which object is null) and then set proper action.
I think for these kinds of problems RecyclerView is better. Its very simple to implement and you can find the implementation document here.

// Create a List from String Array elements
final List<String> tests = new ArrayList<String>(Arrays.asList(test));
// Create an ArrayAdapter from List
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, tests);
// DataBind ListView with items from ArrayAdapter
lv.setAdapter(arrayAdapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Add new Items to List
tests.add("abc");
tests.add("def");
/*
notifyDataSetChanged ()
Notifies the attached observers that the underlying
data has been changed and any View reflecting the
data set should refresh itself.
*/
arrayAdapter.notifyDataSetChanged();
}
});
for more http://android--code.blogspot.in/2015/08/android-listview-add-items.html

Related

How to create a listarray globally in Android Studio and show the content in a list view?

I want to create an ArrayList, add values to it from different other activities and then show it as a listview in some another activity. I know how to create a list view but I am don't know how to create an ArrayList globally and add values to it via other activities. It would be really helpful if you can provide me with a sample code for the full problem. I don't want to go for any advanced method, as it is for a beginner college project and I want to explain my implementation method properly.
Basically, it's a shopping app where I am storing the name of bought products in an ArrayList on the click of a button. And then showing the final list in listview on the cart activity.
First, create a class to hold the ArrayList globally:
public class ListHolder {
public static List<String> list = new ArrayList<>();
}
Use the array list in any activity you want:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
... // Init views
// Obtain the list
List<String> list = ListHolder.list;
// Maybe add something to it
list.add("sample");
// Set the list to your adapter to render it by your ListView
yourAdapter.setData(list);
yourAdapter.notifyDataSetChanged();
}
}

Add data in listview using background service

Creating an app in which I am showing feeds in listview and storing feeds record in sqlitedatabase. Now I want that if anyone add new feed, that listview should be automatically update using background service and service will be run in every 1 min and that new value should be inserted in database and generate notification of new added feed.
I don't know how to add service and how to set adapter for listview in service class. I know I have to add intent service for this.
Searched but I did not understood.
What I want: Assume I have two images in feed list view and someone added one more image, so this new added image should be automatically shown in my feed without refreshing the feed listview. For this I have to run background service which will run in every 1 min and that new added image will be shown.
But I don't know how to add service, for this how to call server in service class and add data in listview in service class and add record in sqlitedatabase and generate notification for new feed. This all will be in service class. Extending baseadapter for listview.
Like i said, you need a ListAdapter which controls your changes and asynchronous stuff.
For example something like this:
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
You only need your Service Routine which adds or changes the Array - The adapter will handle your background stuff.
If you use for example the Firebase Database there is a special FirebaseListAdapter which checks deleting, adding, changing the data and will update your list automatically.
For your specific problem with SQLite you should do something like this:
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
CountriesDbAdapter.KEY_CODE,
CountriesDbAdapter.KEY_NAME,
CountriesDbAdapter.KEY_CONTINENT,
CountriesDbAdapter.KEY_REGION
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.country_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
You could use a BusEvent solution. When your service detect a change you post a new event with the chages. The activity which contains the ListView, will be listen this event and will update the adapter.
You could use a library like Otto or use a new aproach with RxAndroid and RxJava RxBus.
I think the best option for you is otto because integrate RxAndroid is too much for a simple communication.
A solution with Otto could be:
dependencies {
compile 'com.squareup:otto:1.3.8'
}
BusProvider.java
import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;
public final class BusProvider {
private static final Bus BUS = new Bus(ThreadEnforcer.ANY);
private BusProvider() {
// No instances.
}
public static Bus getInstance() {
return BUS;
}
}
ListDataEvent.java
public class ListDataEvent {
List<YourObject> data; ////set your data
}
YourActivity.java
public class YourActivity extends AppCompatActivity {
#Override
public void onResume() {
super.onResume();
BusProvider.getInstance().register(this);
}
#Override
public void onPause() {
super.onPause();
BusProvider.getInstance().unregister(this);
}
#Subscribe
public void onListDataEvent(ListDataEvent event) {
//fill your adapter with event.data and update the listView
}
}
Finally the service:
public class YourService extends IntentService {
#Override
protected void onHandleIntent(Intent workIntent) {
//Gets data and post the event
BusProvider.getInstance().post(new ListDataEvent(data));
}
}

ListView does not show changes until focus changes after notifyDataSetChanged

I have an AlertDialog with a ListView set to multiple selection on it. It also has a Button on it.
The Button open another AlertDialog that if ok'ed will remove the selected items from the data set of the ListView, and then tell the adapter of the list view that the dataset has changed with the notifyDataSetChanged() method.
This all works fine except for one thing. The ListView does not update it's content until I interact with something. Then it updates to the correct data.
This is not a big problem, but I really would like the ListView to appear correct at once, and not just after the focus has changed.
Code:
Button remove = (Button) view.findViewById(R.id.btn_remove_questions_edit_rack);
final Context con = this;
remove.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Builder warnBuild = new Builder(con);
warnBuild.setMessage(R.string.question_deletion_warning);
warnBuild.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
SparseBooleanArray checked = list.getCheckedItemPositions();
for (String s : keys)
{
int i = keys.indexOf(s);
if (checked.get(i))
{
toRemove.add(map.get(s));
map.remove(s);
}
}
keys.clear();
keys.addAll(map.keySet());
((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
list.clearChoices(); //This makes sure the selection is cleared, if it isn't, some of the other items (those that now has the index of the selected items) will be selected when the View refreshes.
dialog.dismiss();
}
});
//Negative button here, not relevant.
}
});
Where map and keys are:
final HashMap<String, QualityQuestion> map = new HashMap<>();
//I add items to the map
final ArrayList<String> keys = new ArrayList<>(map.keySet());
And toRemove is where I store the items to be removed from the actual object they are on when the ok button on the original AlertDialog is pressed.
This is how I populate my ListView in the first place:
final ListView list = (ListView) view.findViewById(R.id.list_questions_edit_rack);
list.setAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1,
keys));
I have tried things like list.invalidateViews(), list.invalidate and other things I found in questions similar to mine here on SO. But none of that made any difference. I suspect my problem to be different from theirs since my items clearly are updated, it just takes a change of focus on the original AlertDialog for the change to be visible.
How can I make the ListView show the changes in it's data source imidiatly insted of after a focus change?
By calling
((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
you get a fresh adapter which is almost certainly not identical to the anonymous adapter you used to populate your list in the first instance.
See also the documentation for ListView.getAdapter()
Returns the adapter currently in use in this ListView.
The returned adapter might not be the same adapter passed to setAdapter(ListAdapter) but might be a WrapperListAdapter.
From the point of view of this fresh adapter, the data set hasn't changed because the changes happened way before it was instantiated.
To solve your problem, make your list and your list adapter members of your activity class (or the scope where you want to keep them alive):
private ArrayList<String> keys;
private ArrayAdapter myAdapter;
private ListView list;
Then in your "onCreate()"
keys = ...; // initialization of ArrayList with the needed data
myAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1,
keys);
list = (ListView) view.findViewById(R.id.list_questions_edit_rack);
list.setAdapter(myAdapter);
This way, in your "OnClickListener" you can notify "myAdapter":
keys.addAll(map.keySet());
myAdapter.notifyDataSetChanged();
Hope this helps :)
You can tweak it, by granting focus to another view, and then requesting it back:
view.requestFocus();
You can also use:
view.requestFocusFromTouch();

Update custom listview item attribute

I am using listview in my app.I am adding items to list with this line:
conversationsAdapter.add(user);
and this initializes list
conversationsAdapter=new ArrayAdapter<JsonObject>(this,0) {
#Override
public View getView(int c_position,View c_convertView,ViewGroup c_parent) {
if (c_convertView == null) {
c_convertView=getLayoutInflater().inflate(R.layout.random_bars,null);
}
JsonObject user=getItem(c_position);
String name=user.get("name").getAsString();
String image_url="http://domain.com/photos/profile/thumb/"+user.get("photo").getAsString();
TextView nameView=(TextView)c_convertView.findViewById(R.id.tweet);
nameView.setText(name);
ImageView imageView=(ImageView)c_convertView.findViewById(R.id.image);
Ion.with(imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
return c_convertView;
}
};
ListView conversationsListView = (ListView)findViewById(R.id.conversationList);
conversationsListView.setAdapter(conversationsAdapter);
conversationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startChat(conversationsAdapter.getItem(position));
}
});
My list view is looking like this:
I want to update an item in the list.How can I do this ?
Example:We can write a method like: changeName when this method calls,method sets name "Tolgay Toklar" to "Tolgay Toklar Test" so I want to update custom listview item attributes.
I totally disagree with tyczj. You never want to externally modify an ArrayAdapter's list and yes it's possible to update just an individual item. Lets start with updating an individual item.
You can just invoke getItem() and directly modify the object and call notifyDataSetChanged(). Example:
JSONObject object = conversationAdapter.getItem(position);
object.put("name", data);
conversationAdapter.notifyDataSetChanged();
Why does this work? Because the adapter will feed you the same object reference used internally, allowing you to modify it and update the adapter. No problem. Of course, I'd recommend instead building your own custom adapter to perform this directly on the adapter's internal list. As an alternative, I highly recommend using the ArrayBaseAdapter instead. It already provides that ability for you while fixing some other major bugs with Android's ArrayAdapter.
So why is tyczj wrong about modifying the external list? Simple. There's no guarantee that your external list is the same as the adapters. Once you perform a filter on the ArrayAdapter, your external list and the adapters are no longer the same. You can get into a dangerous scenario where (for example) index 5 no longer represents position 5 in the adapter because you later added an item to the adapter. I suggest reading Problems with ArrayAdapter's Constructors for a little more insight.
Update: How External List Fails
Lets say you create a List of objects to pass into an ArrayAdapter. Eg:
List<Data> mList = new ArrayList<Data>();
//...Load list with data
ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(context, resource, mList);
mListView.setAdapter(adapter);
So far so good. You have your external list, you have an adapter instantiated with it and assigned to listview. Now lets say at some later point, the adapter is filtered and cleared.
adapter.filter("test");
//...later cleared
adapter.filter("");
Now at this point mList is NOT the same as the adapter. So if the adapter is modified:
adapter.add(newDataObject);
You'll find that mList does not contain that new data object. Hence why external lists like this can be dangerous as the filter creates a NEW ArrayList instance. It won't continue to use your mList referenced one. You could even try adding items to mList at this point and it won't be reflected in the adapter.
If you change the data in your list you need to call notifyDatasetCanged on the adapter to notify the list that the underlying data has changed needs to be updated and.
Example
List<MyData> data = new ArrayList<MyData>();
private void changeUserName(String name){
//find the one you need to change from the list here
.
.
.
data.set(myUpdatedData);
notifyDatasetChanged()
}

Adding data at the specific position of Listview in android

I have a listview and I have to add two more element in listview. I have to add notifyDataSetChanged() . but not sure how to do it.
listView = (ListView) findViewById(R.id.my_listview);
myListAdapter = new MyListAdapter (this, sourceList);
listView.setAdapter(myListAdapter);
myListAdapter.notifyDataSetChanged();
sourceList is the List .
I need to add 2 items at the end of the Listview. Thanks.
Just add your desired data to the adapter. For example:
myListAdapter.add(obj1);
The newly added data will be attached at the end of your ListView.
Remember: when injecting data directly into an Adapter, you don't need to call its notifyDataSetChanged method.
If you modify your sourceList object, in that case its necessary to call notifyDataSetChanged method in order for the Adapter to refresh its contents.
Check out this Dummy Example to add the adapter to the List.
Read the Comment
ListView lstViewtodo; //Your ListView
List<NoteModel> list_note_model = new ArrayList<NoteModel>(); // for Adding the Array of your Model
NoteModel noteModel = new NoteModel(txttitle, jsonArray, obj.getUid(), txtColorType, false); //Your Model
list_note_model.add(0, noteModel); //Adding the value to array
lstViewtodo.setAdapter(list); //finally adding the content to list
To Add the Content Dynamically...
NoteListAdapter list = new NoteListAdapter( NoteMainActivity.this , R.layout.todo_adapter_view , list_note_model); //Your class extending the ArrayAdapter<NoteModel>
NoteModel noteModel = new NoteModel(txttitle, jsonArray, obj.getUid(), txtColorType, false);'
list_note_model.add(0, noteModel);
list.notifyDataSetChanged();
Let me Know if You Difficulty...

Categories

Resources