I just need that items in a Spinner rows wraps when their length is large.
So I did this:
item.xml
<TextView
android:id="#+id/tvAddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Address"
android:textSize="14sp" />
layout with Spinner:
...
<Spinner
android:id="#+id/spAddr"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
and a code to implement data
Cursor c = tbl.getAddr(id);
getActivity().startManagingCursor(c);
String[] from = new String[] { "address" };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, c,
from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner s = (Spinner) v.findViewById(R.id.spAddr);
s.setAdapter(adapter);
But I do not know how to use my row-resource item.xml in here. How can I implement my custom row?
UPD: after I change item.xml with MSquare code.
Closed Spinner
Opened Spinner
And when I choose item I've got picture 1 again.
Try this:
Your item.xml should be like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Address"
android:textSize="14sp" />
and in the code you can use now:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
R.layout.item, c, from, to);
Try it out.
It's too late but maybe it will be helpfull for someone
This is my code for custom spinner
final CardDAO cardDAO = new CardDAO(activity);
final Cursor cursor = cardDAO.getNameAllActiveCard();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(activity,
R.layout.spinner_row,
cursor, new String[] {"pan", "name"}, new int[] {R.id.spinner_pan, R.id.spinner_name}, 0);
adapter.setDropDownViewResource(R.layout.spinner_dropdown);
Spinner spinner = (Spinner) view.findViewById(R.id.operCardList);
spinner.setAdapter(adapter);
spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_pan"
android:paddingRight="5dp"
android:textSize="13sp" />
spinner_dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_name"
android:padding="7dp"
android:textSize="16sp" />
Now you can customise it as you wish
Related
I'm looking for a way to retrieve one specific datum from the database, but right now I only can retrieve the data by using a ListView - not a TextView.
Here is my java file.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
employeeList = (ListView) findViewById (R.id.list);
}
public void search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName = ?",
new String[]{searchText.getText().toString()});
adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"title"},
new int[] {R.id.title});
employeeList.setAdapter(adapter);
}
My XML files: main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/searchText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/searchButton"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="search"/>
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
employee_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">
<TextView
android:id="#+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/lastName"
android:layout_marginLeft="6px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/firstName"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/firstName"/>
</RelativeLayout>
I tried to change all the specific ListView references to TextView but I got errors on the SimpleCursorAdapter.
You don't need an adapter anymore. So, now, instead of this:
adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"title"},
new int[] {R.id.title});
employeeList.setAdapter(adapter);
use this
if (cursor != null) && (cursor.moveToFirst)
{
txtMyTextView.setText(String,format("%s %s %s",
cursor.getString(cur.getColumnIndex("title")),
cursor.getString(cur.getColumnIndex("firstName")),
cursor.getString(cur.getColumnIndex("lastName"))))
}
Output: Mr. Abdul Hanan
[EDIT]
If you prefer an output like: Mr. Hanan, Abdul
Then, simply change the string format and the parameters order to
txtMyTextView.setText(String,format("%s %s, %s",
cursor.getString(cur.getColumnIndex("title")),
cursor.getString(cur.getColumnIndex("lastName")),
cursor.getString(cur.getColumnIndex("firstName"))))
I have this line in my ListActivity
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MealsActivity.this, R.layout.meal_row);
and 2 of my layouts are like this:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/android:list"
android:layout_weight="1" />
<TextView
android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string/empty" />
meals_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" >
</TextView>
I believe smth is wrong with 'R.layout.meal_row' parameter.
Process: mycalories.com.jalle.mycalories, PID: 2659
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
How do I specify textview in which data should be listed ?
You can try this to set custom adapter for array list..
//creating String Array
ArrayList<String> arraylist = new ArrayList<String>;
arraylist = "Your String Array";
//Creating adapter
ArrayAdapter<String> adpt = new ArrayAdapter<String>(getApplicationContext(), R.layout.meals_row.xml, R.id.text, arraylist);
//setting adapter to string
list.setAdapter(adpt);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" />
</RelativeLayout>
Here is my XML of the spinner but it did not work.
<Spinner
android:id="#+id/frequency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/spinner_voices"
android:gravity="center|center_horizontal"
/>
You need to set your own layout for spinner item.
ArrayAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"ABC", "DEF", "GHI"});
spriner.setAdapter(adap);
Where R.layout.spinner_item is a layout with content:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
I'm trying to use an ArrayAdapter in a ListActivity:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.id.text1, new String[] { "a", "b"});
setListAdapter(adapter);
This is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#id/android:text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_below="#id/android:text1" >
</ListView>
</RelativeLayout>
None of the solutions I found on StackOverflow seems to work. I'm using API 10. Can you help please?
You are using the wrong type of resource, you need to reference R.layout not R.id. Try android.R.layout.simple_list_item_1:
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { "a", "b"});
Define a layout for the rows of your ListView, and call it, for example, simple_list_item_1 (create the file simple_list_item_1.xml in the folder res/layout). Put your TextView into this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#id/android:text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</RelativeLayout>
and then create your ArrayAdapter like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, new String[] { "a", "b"});
Here you'll find a detailed explanation about Lists and Adapters.
I have made a list view containing two childs. Now i am trying to append a series of children to each of these two childs.
I am using following procedure.
super.onCreate(savedInstanceState);
setContentView(R.layout.report_main_layout);
CharSequence[] dummyChar = {"a","b"};
listView = (ListView) findViewById(R.id.UIMainAccountListView);
adapter = new CustomArrayAdapterOfMainList(this, dummyChar);
listView.setAdapter(adapter);
In the custom adapter i am using following code.
if (position == 0)
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, debitAccounts,sumDebit);
viewHolder.listView.setAdapter(adapter);
}
else
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, creditAccounts, sumCredit);
viewHolder.listView.setAdapter(adapter);
}
All i am getting is like this, I want to extend it so that all of the accounts show, List view should wrap content but it is not wrapping content
This will stack two listviews and force them to take up half of their parent each.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
You can remove the cache color by setting android:cacheColorHint="#android:color/transparent" for each listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="20sp" >
<ListView
android:id="#+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/child1" />
</RelativeLayout>
This layout for first child1 listview above the second child2 listview
Thanks