How can I use two adapters on one GridView?
So I am trying to create a GridView, and fill it with two different types of info, so I tried using to adapters to do so, such as:
GridView gridView = (GridView) view.findViewById(R.id.gridView);
ArrayList<Grids> cellArray = new ArrayList<>();
GridAdapter cellAdapter = new GridAdapter(this.getActivity(), cellArray);
gridView.setAdapter(cellAdapter);
Grids arrayOfCells[] = new Grids[grid];
for (int j = 0; j < arrayOfCells.length; j++) {
arrayOfCells[j] = new Grids(false);
cellAdapter.add(arrayOfCells[j]);
}
ArrayList<Time> arrayOfTime = new ArrayList<>();
timeAdapter adapter = new timeAdapter(this.getActivity(), arrayOfTime);
gridView.setAdapter(adapter);
Time arrayGrid[] = new Time[grid];
for (int i = 0; i < arrayGrid.length; i++) {
arrayGrid[i] = new Time(arrayOfHours[i], arrayOfPeriod[i]);
adapter.add(arrayGrid[i]);
}
However, when I do so, only the second one is added to the gridView. So how would I go about doing this? Any help is greatly appreciated.
You can only set one Adapter instance on GridView. When you set the second adapter it's replacing the first adapter.
To solve this, you need to combine both data sources and put them inside of a single Adapter. One way to do it would be to create an ArrayList and add Grids and Time objects to it:
ArrayList<Object> adapterData = new ArrayList<Object>();
for (int j = 0; j < arrayOfCells.length; j++) {
adapterData.add(new Grids(false));
}
for (int i = 0; i < arrayGrid.length; i++) {
adapterData.add(new Time(arrayOfHours[i], arrayOfPeriod[i]));
}
TimeOrGridsAdapter adapter = new TimeOrGridsAdapter(this.getActivity(), adapterData);
gridView.setAdapter(adapter);
Note that inside of this new TimeOrGridsAdapter, you'll need to figure out whether the Object is a Time OR a Grids object and then create the view accordingly.
Related
String sessionId = getIntent().getStringExtra("numbers");
final String[] values = new String[]{sessionId};
ArrayList list = new ArrayList<>(Arrays.asList(values));
final ArrayList arrayList = new ArrayList();
for (int i = 0; i < values.length; i++)
{
Log.d(TAG, "listValue -" + values[i]);
arrayList.add(values[i]);
}
listView = (ListView)findViewById(R.id.textView2);
listView.setAdapter(new
ArrayAdapter(ListDisplayActivity.this,R.layout.list_display,R.id.text, arrayList));
How to fix listview value single string have multiple values for android?
You can pass values like
intent.putStringArrayListExtra("sessionIds",yourSessionIdList);
and get like
ArrayList<String> sessionIds = getIntent().getStringArrayListExtra("sessionIds");
Check the below link: You may need to use custom adapter to display multiple values in each row of listview:
Listview with custom adapter
And I also recommend you to use recyclerview instead of listview
I created a listview that connects to a database. I want to show some of the newest items (maybe 10 newest items) that will add in a random time? How can I accomplish this? This what I have tried:
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
But this doesn't work.
Sorry but, this code block makes no sense.
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
To explain what you have did;
you are getting the list from your database
initializing the list adapter
setting adapter to list
then reverse your order of the list (but you are not notifying the adapter)
You are doing this in a loop exactly 5 times.
What you need to do is:
get your list from database
then reverse your order of the list
initialize the list adapter
set adapter to list
I should look like this:
mKondisiList = myDbHelper.getListKondisi();
Collections.reverse(mKondisiList);
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
If you want to show only limited number of items, you can do something like below:
ArrayList<Object> fullList = new ArrayList<>();
fullList = myDbHelper.getListKondisi();
Collections.reverse(fullList);
//Add first 10 item to your mKondisiList
for (int i = 0; i < 10; i++) {
mKondisiList.add(fullList.get(i));
}
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
How to get value to dynamically add multiple spinner where spinner id is same.i used 'String spin = parent.getSelectedItem().toString();' but i get all time last spinner value.Plz help me?
you can create array list of spinner
ArrayList<Spinner> listSpinner = new ArrayList<>();
listSpinner.add(spinner1);
listSpinner.add(spinner2);
listSpinner.add(spinner3);
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem();
}
get different value for all spinner out side for loop and store it in arraylist.
ArrayList<Spinner> listSpinner = new ArrayList<>();
ArrayList<ArrayList<String>> status_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
status_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layout.custom_asset_coding_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, status_list.get(i));
spinner_status.setAdapter(adapter);
listSpinner.add(spinner_status);
}
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem().toString();
}
What I'm trying to is making a list which contains series of numbers like [1,2,3,4,5,6...100].
In Java, it is simple using 'range' so I tried to find similar class in android.
Therefore, I found that some classes like Range and Intstream, but I don't know how to use them.
I'll be appreciated if you teach me how can I get my purpose, thanks.
You could write a simple function which would look like this:
public List<Integer> buildList(int maximum) {
List<Integer> list = new ArrayList();
for (int i = 1; i <= maximum; i++) {
list.add(i);
}
return list;
}
And a call which produces your desired result would look like this:
List<Integer> list = buildList(100);
If you want an array instead of a list, do this:
int[] array = list.toArray(new int[list.size()]);
int size = 100;
ArrayList<Integer> list = new ArrayList<>(size);
for (int i = 1; i <= size; i++) {
list.add(i);
Log.i("Value is = ", i+"");
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
.....
You don't need to define the size because ArrayList is dynamically
while in some other language we use list[index] to get the value.
But here we use list.get(index) to do that.
i want to make a Real Time Trading App.. but i have a problem to combine 2 adapter into one adapter. so i need help from you all :D
here's my code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
final uraikonversiActivity cp = new uraikonversiActivity(this);
final ListView list = (ListView) findViewById (R.id.list);
final String list_array[] = new String[cp.kodekonversi.size()];
final Float list_nilai[] = new Float[cp.nilaikonversi.size()];
for (int i = 0; i < cp.kodekonversi.size(); i++) {
list_array[i] = cp.kodekonversi.get(i);
}
for (int i = 0; i < cp.nilaikonversi.size(); i++) {
list_nilai[i] = cp.nilaikonversi.get(i);
}
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list_array);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list_nilai);
list.setAdapter(adapter);
}}
(note : this a real time so i got the arraylist from the internet)
my goal is to show something like this at the list = "USD - 1.xxxxx".
thanks in advance :)
At last you are displaying all data withing one listview.So just convert both arrays into one like this(as both arrays are of same lenth):
String[] finalArray = new String[list_array.length + list_nilai.length];
for(int i = 0; i< cp.nilaikonversi.size(); ++i)
finalArray[i] = list_array[i] +"-" + list_nilai[i];
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, finalArray);
list.setAdapter(adapter);
Hope it will help you. :)
Build up your own adapter, do not just rely on ArrayAdapter.
When building an adapter there is the method public View getView(int position, View convertView, ViewGroup parent) in wich you can code how every single line should look like. Just return a layout containing two textfields wich are filled by your list_array and list_nilai.
See also: http://developer.android.com/reference/android/widget/Adapter.html
or for a complete tutorial: http://developerlife.com/tutorials/?p=327