Combine 2 ArrayAdapter - android

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

Related

How to fix listview value single string have multiple value for android

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

get value from Dynamically add multiple spinner in android?

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>> s‌​tatus_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
s‌​tatus_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layou‌​t.custom_asset_codin‌​g_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, s‌​tatus_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();
}

Android: How to have a GridView filled with 2 adapters

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.

I have all these items in a textview, how to put them in listview now

I am parsing JSON into a textview and i need some help trying to put that into a listview instead. I know this might be very easy for some, but my main focus of confusion is that in a textview, you are setting the text using the setText function. I am also new to android, so I don't have this basic down yet, but I appreciate any help in advance, thank you.
public void onClick(View arg0) {
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
try {
String buildings = getJSON("http://iam.colum.edu/portfolio/api/course?json=True");
//JSONObject jsonObject = new JSONObject(buildings);
JSONArray queryArray = new JSONArray(buildings);
//queryArray = queryArray.getJSONArray(0);
List<String> list = new ArrayList<String>();
for (int i=0; i<queryArray.length(); i++) {
list.add( queryArray.getString(i) );
}
String finaltext="";
StringBuilder sb = new StringBuilder();
for (int i=0; i<queryArray.length(); i++) {
// chain each string, separated with a new line
sb.append(queryArray.getString(i) + "\n");
}
// display the content on textview
tv1.setText(sb.toString());
//tv1.setText(arr[0]);
} catch (Exception e) {
e.printStackTrace();
Log.d("JSONError", e.toString());
}
}});
Basically you add your ListView in your layout file, and then in the code you set an Adapter on the ListView, which holds the data. And then all the magic is going to be done for you.
Please read
http://developer.android.com/guide/topics/ui/layout/listview.html
http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
So in your case it would look something like this:
ListView listView = (ListView) findViewById(R.id.listview);
...
JSONArray queryArray = new JSONArray(buildings);
//queryArray = queryArray.getJSONArray(0);
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<queryArray.length(); i++) {
list.add( queryArray.getString(i) );
}
ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
This will take the content of your list and create one ListView item for each entry. The text is set to the layout file android.R.layout.simple_list_item_1 which is provided by android.
If you change the content of the ArrayAdapter (and thus the ListView) you have to call adapter.notifyDataSetChanged() to inform the system that the content has changed.
To change the layout, or add an Adapter for other data sources, please refer to the documentation.

sort array list from second item android

i have a array list for using it in spinner ,i have first value i spinner as title and i want to sort array list from second item in the spinner but i dont know how to do this i am using below trick but it sort whole array list including first item which is title so how to statr sorting from second item...
my code is below...
// this is my title ie. "provincia"
String select2= "Provincia";
if(!estado1.contains(select2)){
estado1.add(select2);
}
for (int i = 0; i < sitesList1.getEstado().size(); i++)
{
if(!estado1.contains(sitesList1.getEstado().get(i)))
{
estado1.add(sitesList1.getEstado().get(i));
Collections.sort(estado1);
}
use below code for show it in spinner...
final ArrayList<String> estado1 = MainMenu.barrio1;
final Spinner estado11 = (Spinner) findViewById(R.id.Spinner04);
ArrayAdapter<String> adapterbarrio = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, estado1)
estado11.setAdapter(adapterbarrio);
Why not remove the title / only add the title after the list has been sorted?
How about this
List<String> list = new ArrayList<String>();
// Fill list
String title = list.get(0);
list.remove(0);
Collections.sort(list);
list.add(0, title);
One way would be to split the arraylist like
estado1.subList(1,estado1.size()-1);
This would return a sublist excluding your title.
Use bubble sort! and start at index = 1!
final ArrayList<String> estado1;
for(int i=1; i<estado1.size() ; i++) {
for(int c=i; c<estado.size() ; c++) {
if(estado1.get(i).compareTo(estado1.get(c)))
{
String temp = estado1.get(i);
estado1.remove(i);
estado1.add(i, estado1.get(c));
estado1.remove(c);
estado1.add(c, temp);
}
}
}
PS: very bad performance

Categories

Resources