Can't create ListView of RelativeLayouts for Android - android

What I'm trying to accomplish is have a scrollable ListView that contains a RelativeLayout for each item. Each RelativeLayout contains a CheckBox and an EditText. My ArrayAdapter for the ListView contains the RelativeLayout type. When I create this ListView, each item is shown as the toString() of the layout instead of the layout itself. What should I be doing to accomplish this?

Init your list view
ListView listView = (ListView) findViewById(R.id.list_view);
Create the adapter layout for your listview like this (customview.xml)
<?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="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<requestFocus />
</EditText>
</<RelativeLayout >
Create the AdapterClass and extend BaseAdapter
public class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
...
}
#Override
public Object getItem(int position) {
return ....;
}
#Override
public long getItemId(int position) {
return ...;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.customview, parent, false);
TextView name = (TextView) row.findViewById(R.id.name);
CheckBOx check = (CheckBOx) row.findViewById(R.id.checkBox1);
EditText edit = (EditText) row.findViewById(R.id.editText1);
name.setText("Name");
edit.setText("Address");
return row;
}
Finally set the adapter to your ListView
listView.setAdapter(new CustomAdapter(contacts) );

Make your own subclass of ArrayAdapter and construct your RelativeLayout in getView()
The type you set the ArrayAdapter to is for populating the content not constructing the view. To use a non-standard view like you want to do, you'll have to override BaseAdapter
You can see an example here:
http://www.vogella.com/articles/AndroidListView/article.html#adapterown

Related

Why is my listview not expanding to accommodate a larger row size?

I have a listview that I want the user to be able to delete items from. If the user holds on a row long enough, the row's checkbox is made visible. However, the listview doesn't grow to accommodate the (now taller) row despite there being plenty of space. I had this issue before when the listview was in a scrollview but that's no longer the case. I understand I can reformat the text as a bandage solution, but I want to understand what is happening.
Here is the listview before and after the first entry is held. You can see the listview's height stays the same and doesn't expand to accommodate the two entries:
Here is the listView:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Here is the listView item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns="https://"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<CheckBox
android:id="#+id/deleteCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="5dp"
android:textColor="#android:color/darker_gray"
android:textSize="18sp" />
</LinearLayout>
Here is how the layout is being inflated in my custom adapter:
public class CustomAdapter extends ArrayAdapter<CustomlistEntry> {
public CustomAdapter(Context context, ArrayList<CustomlistEntry> entries) {
super(context, 0, entries);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_custom_listview, parent, false);
}
CustomlistEntry entry = getItem(position);
TextView tvNumber = convertView.findViewById(R.id.number);
TextView tvDate = convertView.findViewById(R.id.date);
tvNumber.setText(entry.getNumber());
tvDate.setText(entry.getDateString());
return convertView;
}
}
and finally the adapter being set:
CustomAdapter adapter = new CustomAdapter(App.getApp(), arrayListSorted);
final ListView listView = activity.findViewById(R.id.listView);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
arg1.findViewById(R.id.deleteCheckBox).setVisibility(View.VISIBLE);
CustomAdapter adapter = (CustomAdapter)listView.getAdapter();
return true;
}
});
listView.setAdapter(adapter);

multiple elements within same drawable

I'm trying to add several items within a single Drawable which would be used in a GridView:
- text aligned at the center of the drawable
- image at the background
- a programmatically variable number (0-3) of images (stars) ALIGNED horizontally at the bottom of the drawable.
How could I accomplish this please?
At the moment I'm trying to use a custom ImageView and set the background image using setImageResource. An alternative would be to use a TextView, however I still have no clue about the stars at the bottom.
Any help would be appreciated!
George.
You don't mean Drawable, you mean to say a View. You simply have to find a decent tutorial about making custom views for AdapterView (ListView, GridView, ...).
One such tutorial to get you started is http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
Quick dump of the code:
main.xml
<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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}

How do i set visibility of selected item of spinner to false

I have a background image set for my spinner, but when I select an item from the drop-down list of the spinner, my spinner background image is replaced by the selected item, I want my background image to remain there and I don't want the selected item to be shown instead of the background image of spinner. How do I do that?
Code for my custom adapter:
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
false);
TextView main_text = (TextView) mySpinner
.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
}
Code for my custom spinner xml
<?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="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/text_main_seen"
android:layout_width="fill_parent"
android:layout_height="30sp"
android:background="#008000"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="30px" />
</RelativeLayout>
Code for my actual spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#008000"
android:layout_weight="0.08"
android:drawSelectorOnTop="true" />
</RelativeLayout
and code of my mainActivity where i am calling the spinner is
Spinner mySpinner = (Spinner) findViewById(R.id.planets_spinner);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner,
spinnerValues));
I wouldn't use a spinner at all for this. Just use a Button made to look like a Spinner or whatever else you want it to look like. You can make a regular Button look like a Spinner with this attribute:
android:background="#android:drawable/btn_dropdown"
Or, make your own background that looks like a Spinner and use that.
Then, have the onClick event of your Button open up a "single-item-select" Dialog.

Filling ListView out

I'm working on project and main layout contains ListView. I want to dynamically add subItems to ListViewItem View. My main layout is as follows :
main_layout.xml
<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="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#android:color/transparent" >
</LinearLayout>
And I want to add TextView to returned view in getView with following properties :
Height : fill_parent
Width : wrap_content
Text : Text View Item
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
return convertView;
}
Please note that, I want to dynamically add subItems to ListViewItem. You can suppose that, I have array of TextView and want to fill my listView using this Items. How can I do this scenario ? I can not find any result with searching on other threads and aslo Google. So, Could any one please help me to solve my problem ?
Thanks in Advance :)
This was my getView() method.
main.xml
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/button1"
android:cacheColorHint="#android:color/transparent"
android:divider="#00ff00"
android:fastScrollEnabled="true"
android:focusable="false"
android:listSelector="#000000"
android:paddingTop="5dp"
android:saveEnabled="true" >
</ListView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Adapter
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
/*convertView = mInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.getAppName().setText(str.get(position));*/
if(position==0)
{
ImageView iv = new ImageView(getApplicationContext());
iv.setBackgroundResource(R.drawable.ic_launcher);
convertView = iv;
}
else if(position==1)
{
TextView tv = new TextView(getApplicationContext());
tv.setText("text view");
convertView = tv;
}
else
{
EditText et = new EditText(getApplicationContext());
et.setText("ET go home");
convertView = et;
}
return convertView;
}
private class ViewHolder {
private View pathRow;
private TextView appNameView = null;
public ViewHolder(View row) {
pathRow = row;
}
public TextView getAppName() {
if (null == appNameView) {
appNameView = (TextView) pathRow.findViewById(R.id.tv);
}
return appNameView;
}
}
}
The commented out portion is the one used to be. The code below it is the one which I used to dynamically generate the view and return them. my listitem.xml still has a textView in it. But the adapter sets the view which we are returning from getView(). This works for me.
I really don;t see the point of the LinearLayout in your item.xml. You can do that in your own code.

Android - item and subItem from resources

I've faced a problem by adding a question for my questionnaire from res/values.
As I need a ListView followed by checkbox in each line, I've created a custom ListView.
This is my list_item.xml
I've deleted android:text="#string/textLarge" and "#string/textSmall" lines, that's why it has an empty line in TextView tags.
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:id="#+id/textView_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp">
</TextView>
<TextView
android:id="#+id/textView_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:gravity="right">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBxc">
</CheckBox>
</LinearLayout>
</LinearLayout>
and a simple list
<?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"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
my Main.java file is next
public class Main extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new MyAdapter(this,
android.R.layout.simple_list_item_2, R.id.textView_large,
getResources().getStringArray(R.array.questions)));
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.questions);
CheckBox chk = (CheckBox) row.findViewById(R.id.checkBxc);
TextView txtLarge = (TextView) row.findViewById(R.id.textView_large);
TextView txtSmall = (TextView) row.findViewById(R.id.textView_small);
txtLarge.setText(items[position]);
return row;
and it works correctly if I add an item from questions.xml, but it displays only 1 line, however I need to make a question as an item (i.e "Do you do some sports?") and some comment below (i.e "Check it if you do"). How could I do that?
You have to create a custom ArrayAdapter (a class that extends from ArrayAdapter) and set it for your list-view listView.setAdapter(yourAdapter)
In this adapter you have to override the getView method (to return your custom list-item-view).
See also this example:
http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html
This piece:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
is not good: you create new item view always, even if it already exists destroying excessive objects costs time. And you can use functionality of the ArrayAdapter itself. Look here.

Categories

Resources