Choice Mode does not effect on Custom Adapter with Layout - android

Previously i worked with GridView and set ChoiceMode to single and everything was OK.
Now i create my Custom Adapter with it's Layout.
And then added items to the GridView.
And set ChoiceMode to Single, As below you will see.
But Choice mode does not work in appearance and view.
Here is Custom Adapter :
public class CarAdapter extends ArrayAdapter<Car>{
public CarAdapter(Context context, int resource, ArrayList<Car> carArrayList) {
super(context, resource, carArrayList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Car car = getItem(position);
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_car,parent, false);
}
TextView carName = (TextView)convertView.findViewById(R.id.textView2);
TextView carSIM = (TextView)convertView.findViewById(R.id.textView);
carName.setText(car.getName());
carSIM.setText(car.getSIM());
return convertView;
}
}
And here is it's Layout XML :
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_x="12dp"
android:layout_y="2dp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginLeft="20dp"
android:id="#+id/textView"
android:layout_x="157dp"
android:layout_y="0dp"
android:textColor="#000"/>
</AbsoluteLayout>
And here is Main Activity :
public class DisplayActivity extends Activity {
CarAdapter carAdapter;
Car car;
Car car2;
Car car3;
ArrayList<Car> carArrayList;
GridView gridView;
Button test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
carArrayList = new ArrayList<Car>();
gridView = (GridView) findViewById(R.id.gridView);
carAdapter = new CarAdapter(getApplicationContext(), android.R.layout.select_dialog_singlechoice, carArrayList);
test = (Button) findViewById(R.id.button);
car = new Car("Test", "aaa");
car2 = new Car("Test2", "aaa");
car3 = new Car("Test3", "aaa");
Car car4 = new Car("Test4", "aaa");
carArrayList.add(car);
carArrayList.add(car2);
carArrayList.add(car3);
carArrayList.add(car4);
gridView.setAdapter(carAdapter);
Toast.makeText(getApplicationContext(), "Choice mode is : " + gridView.getChoiceMode(), Toast.LENGTH_SHORT ).show();
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (gridView.isItemChecked(gridView.getCheckedItemPosition())) {
String name = (carAdapter.getItem(gridView.getCheckedItemPosition())).getName();
Toast.makeText(getApplicationContext(), "Here is " + name, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "This is not true", Toast.LENGTH_SHORT).show();
}
}
});
}
}
As you see i put a test Button and return the name of selected GridView Item.It returns the value,But with no checkable or selected View.
And Main Activity 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".DisplayActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1"
android:layout_marginTop="30dp">
<GridView
android:layout_width="wrap_content"
android:layout_height="387dp"
android:id="#+id/gridView"
android:background="#abc"
android:layoutDirection="ltr"
android:choiceMode="singleChoice"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="#+id/button" />
</LinearLayout>
Should i set it by myself?
If yes, where?
Any solution will appreciate.
Thx

First create the array list. then create the adapter and then assign it as an adapter to grid view.
what you are doing is creating adapter first the creating array-list and the setting it to grid view.

Related

ListView OnItemClickListener is not listening

I checked all the previous questions regarding this issue , but none of them are helpfull to me .
My listview is not responding , i tried changing this
list.setOnItemClickListener(new ContactsListItemClickListener(this));
to
list.setOnItemClickListener(this);
by making my PrioritiseContacts activity just imeplement OnItemClickListener , but then too its not working .
The activity is successfully running , but i am unable to listen for listclick events.
How to correct this?
Here is my class :
public class PrioritiseContacts extends Activity implements OnClickListener {
private ListView list;
// list of contacts with name
private List<Contacts> contactsList;
private Controller controll;
private ContactListAdapters adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_contacts);
controll = new Controller();
contactsList = controll.fetchContacts(this);
// call the adapter to set the list view layout
adapter = new ContactListAdapters(contactsList, this);
list = (ListView) findViewById(R.id.lv_contacts);
// set the adapter to list
list.setAdapter(adapter);
list.setOnItemClickListener(new ContactsListItemClickListener(this));
// inflate the list of contact
}
#Override
public void onClick(View arg0) {
Toast.makeText(this, "clicked", 1000).show();
}
class ContactsListItemClickListener implements OnItemClickListener {
private Context c;
public ContactsListItemClickListener(
PrioritiseContacts prioritiseContacts) {
this.c = prioritiseContacts;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(c, "Clicked", 1500).show();
System.out.print("clicked");
}
}
}
My select_contacts xml :
<LinearLayout 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"
android:background="#000000"
android:orientation="vertical">
<TextView
android:id="#+id/tv_select_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Choose Contacts"
android:textColor="#fdfdfd"
android:textSize="30dip"
android:gravity="center" >
</TextView>
<ListView
android:id="#+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:cacheColorHint="#00000000"
android:clickable="true"
android:focusable="true"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none" >
</ListView>
</LinearLayout>
And this is my adapter's getview() :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// layout infklater to inflate the post list view
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.contacts_list_view, null);
}
Contacts c = contactList.get(position);
// set text views in contact lists
// Typeface custom_font =
// Typeface.createFromAsset(context.getAssets(),"fonts/calibril.ttf");
TextView name = (TextView) view.findViewById(R.id.tv_contact_name);
// date.setTypeface(custom_font);
name.setText(c.getName());
TextView number = (TextView) view.findViewById(R.id.tv_number);
// title.setTypeface(custom_font);
number.setText(c.getPhone());
ImageView contact_image = (ImageView) view.findViewById(R.id.iv_single_contact);
// hut.setTypeface(custom_font);
if(c.getContactImage() != null)
contact_image.setImageBitmap(c.getContactImage());
else
contact_image.setImageDrawable(view.getResources().getDrawable(R.drawable.ic_contact_picture_2));
return view;
}
My contacts_list_view xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_post_list"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#000000"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="2dp"
android:paddingTop="2dp" >
<ImageView
android:id="#+id/iv_single_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.87"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="left"
android:paddingLeft="2dp"
android:text="Contact Name"
android:textColor="#fdfbfb"
android:textStyle="bold" />
<View style="#style/Divider" />
<TextView
android:id="#+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:gravity="left"
android:text="this is number"
android:textColor="#fdfbfb"
android:textSize="10dp"
android:textStyle="bold" />
</LinearLayout>
<CheckBox
android:id="#+id/cb_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
</LinearLayout>
If any row item of list contains focusable or clickable view then OnItemClickListener won't work such as for checkbox or button etc in the row item.There are two solution:
1. row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
2. Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
For example if there is any checkbox or button or image in the row item then
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />

ListView Item not clickable when I display ad's with PublisherAdView inside the Item [duplicate]

I checked all the previous questions regarding this issue , but none of them are helpfull to me .
My listview is not responding , i tried changing this
list.setOnItemClickListener(new ContactsListItemClickListener(this));
to
list.setOnItemClickListener(this);
by making my PrioritiseContacts activity just imeplement OnItemClickListener , but then too its not working .
The activity is successfully running , but i am unable to listen for listclick events.
How to correct this?
Here is my class :
public class PrioritiseContacts extends Activity implements OnClickListener {
private ListView list;
// list of contacts with name
private List<Contacts> contactsList;
private Controller controll;
private ContactListAdapters adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_contacts);
controll = new Controller();
contactsList = controll.fetchContacts(this);
// call the adapter to set the list view layout
adapter = new ContactListAdapters(contactsList, this);
list = (ListView) findViewById(R.id.lv_contacts);
// set the adapter to list
list.setAdapter(adapter);
list.setOnItemClickListener(new ContactsListItemClickListener(this));
// inflate the list of contact
}
#Override
public void onClick(View arg0) {
Toast.makeText(this, "clicked", 1000).show();
}
class ContactsListItemClickListener implements OnItemClickListener {
private Context c;
public ContactsListItemClickListener(
PrioritiseContacts prioritiseContacts) {
this.c = prioritiseContacts;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(c, "Clicked", 1500).show();
System.out.print("clicked");
}
}
}
My select_contacts xml :
<LinearLayout 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"
android:background="#000000"
android:orientation="vertical">
<TextView
android:id="#+id/tv_select_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Choose Contacts"
android:textColor="#fdfdfd"
android:textSize="30dip"
android:gravity="center" >
</TextView>
<ListView
android:id="#+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:cacheColorHint="#00000000"
android:clickable="true"
android:focusable="true"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none" >
</ListView>
</LinearLayout>
And this is my adapter's getview() :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// layout infklater to inflate the post list view
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.contacts_list_view, null);
}
Contacts c = contactList.get(position);
// set text views in contact lists
// Typeface custom_font =
// Typeface.createFromAsset(context.getAssets(),"fonts/calibril.ttf");
TextView name = (TextView) view.findViewById(R.id.tv_contact_name);
// date.setTypeface(custom_font);
name.setText(c.getName());
TextView number = (TextView) view.findViewById(R.id.tv_number);
// title.setTypeface(custom_font);
number.setText(c.getPhone());
ImageView contact_image = (ImageView) view.findViewById(R.id.iv_single_contact);
// hut.setTypeface(custom_font);
if(c.getContactImage() != null)
contact_image.setImageBitmap(c.getContactImage());
else
contact_image.setImageDrawable(view.getResources().getDrawable(R.drawable.ic_contact_picture_2));
return view;
}
My contacts_list_view xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_post_list"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#000000"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="2dp"
android:paddingTop="2dp" >
<ImageView
android:id="#+id/iv_single_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.87"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="left"
android:paddingLeft="2dp"
android:text="Contact Name"
android:textColor="#fdfbfb"
android:textStyle="bold" />
<View style="#style/Divider" />
<TextView
android:id="#+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:gravity="left"
android:text="this is number"
android:textColor="#fdfbfb"
android:textSize="10dp"
android:textStyle="bold" />
</LinearLayout>
<CheckBox
android:id="#+id/cb_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
</LinearLayout>
If any row item of list contains focusable or clickable view then OnItemClickListener won't work such as for checkbox or button etc in the row item.There are two solution:
1. row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
2. Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
For example if there is any checkbox or button or image in the row item then
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />

How can I create a dynamic ListView in Android that contains images in between a fixed header and footer?

I'm a beginner to Android development, but not to software development. I've been looking for tutorials on how to create dynamic ListViews with images and have had some trouble.
The tutorial I followed most closely was #2 on this link (the Custom ArrayAdapter example): http://www.mkyong.com/android/android-listview-example/
This works fine if I remove the header and footer from my layout xml file, but I really need those to be included. Below I'll post my source from my Activity java file, my custom array adapter java file, and my layout xml file as well as the output. I think there may be something I'm missing or not understanding about how the layout inflater works.
Thanks for your time.
MainActivity.java
public class MainActivity extends ListActivity {
static final String[] MAIN_MENU =
new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ImageAdapter(this, MAIN_MENU));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
// to contact
if(selectedValue.equals(MAIN_MENU[4])) {
contactPressed(v);
}
// to icd-10
else if(selectedValue.equals(MAIN_MENU[0])) {
startActivity(new Intent(MainActivity.this, ICDActivity.class));
}
// to services
else if(selectedValue.equals(MAIN_MENU[3])) {
startActivity(new Intent(MainActivity.this, ServicesActivity.class));
}
}
public void contactPressed(View v) {
startActivity(new Intent(MainActivity.this, ContactActivity.class));
overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
}
}
ImageAdapter.java
public class ImageAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;
public ImageAdapter(Context context, String[] values) {
super(context, R.layout.activity_main, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.lvText);
ImageView imageView = (ImageView) rowView.findViewById(R.id.lvImage);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
if (s.equals("ICD-10"))
{
imageView.setImageResource(R.drawable.icon_icd10);
}
else if(s.equals("EDUCATION"))
{
imageView.setImageResource(R.drawable.icon_education);
}
else if(s.equals("NEWS"))
{
imageView.setImageResource(R.drawable.icon_news);
}
else if(s.equals("SERVICES"))
{
imageView.setImageResource(R.drawable.icon_services);
}
else if(s.equals("CONTACT"))
{
imageView.setImageResource(R.drawable.icon_contact);
}
return rowView;
}
}
activity_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" >
<ImageView
android:id="#+id/custom_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/gray">
</ImageView>
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="#drawable/logo_header" />
</RelativeLayout>
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="#+id/footerLayout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="30dp" >
<ImageView
android:id="#+id/footer"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="30dp"
android:scaleType="center"
android:src="#drawable/footer" />
<TextView
android:id="#+id/footerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="14dp"
android:onClick="contactPressed"
android:layout_marginBottom="5dp"
android:text="#string/footerText" />
</RelativeLayout>
<ScrollView
android:id="#+id/scrollableContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout" >
<LinearLayout
android:id="#+id/linearList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:padding="5dp"
tools:context=".MainActivity" >
<!-- Making a dynamic ListView with images with the two attributes below -->
<ImageView
android:id="#+id/lvImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/icon_icd10" >
</ImageView>
<TextView
android:id="#+id/lvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/lvText"
android:textSize="25sp" >
</TextView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
The output I get for this is:
If I comment out the header and footer code in the xml I get:
The goal is for it to look similar to this but with images on the right side of each line:
Thanks for your time.
First looks like you have
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
on your #+id/scrollableContents, which is good. But you also have it on the view nested inside it, #+id/linearList which is not right. There may be other layout problems but it is hard to tell. In general this layout looks very complex for what you are trying to achieve.
But I'm not sure really what you mean by "dynamic ListView" and it looks like you are trying to re-create the functionality of a listview using a ScrollView. I don't think you should take that approach. The ListView is very flexible and you can use a custom layout for your items.
So I found the solution to my problem while searching for another tutorial. I finally ended up using this tutorial: http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html
My problem was that I tried to replace my ListView with a TextView and ImageView in the same layout.xml file when I should have created my TextView and ImageView in a different layout.xml file. Below is the final code and the correct output I was looking for. Thanks!
list_single.xml (below)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="5dp"
android:layout_marginTop="9dp"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="17dp"
android:layout_marginLeft="5dp"
android:textSize="23sp" />
</RelativeLayout>
activity_main.xml (below)
<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" >
<ImageView
android:id="#+id/custom_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/gray">
</ImageView>
<ImageView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="#drawable/logo_header" />
<!-- Footer aligned to bottom -->
<ImageView
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:scaleType="center"
android:src="#drawable/footer" />
<TextView
android:id="#+id/footerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="14dp"
android:onClick="contactPressed"
android:text="#string/footerText" />
<ListView
android:id="#+id/mainMenuList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:divider="#000000"
android:dividerHeight="0.1dp" />
</RelativeLayout>
ImageAdapter.java (below)
public class ImageAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;
public ImageAdapter(Context context, String[] values) {
super(context, R.layout.list_single, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView textView = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
if (s.equals("ICD-10"))
{
imageView.setImageResource(R.drawable.icon_icd10);
}
else if(s.equals("EDUCATION"))
{
imageView.setImageResource(R.drawable.icon_education);
}
else if(s.equals("NEWS"))
{
imageView.setImageResource(R.drawable.icon_news);
}
else if(s.equals("SERVICES"))
{
imageView.setImageResource(R.drawable.icon_services);
}
else if(s.equals("CONTACT"))
{
imageView.setImageResource(R.drawable.icon_contact);
}
return rowView;
}
}
MainActivity.java (below)
public class MainActivity extends Activity {
ListView list;
static final String[] MAIN_MENU =
new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.mainMenuList);
list.setAdapter(new ImageAdapter(MainActivity.this, MAIN_MENU));
// register onClickListener to handle click events on each item
list.setOnItemClickListener(new OnItemClickListener() {
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
String selectedValue = MAIN_MENU[position];
// to contact
if(selectedValue.equals(MAIN_MENU[4])) {
contactPressed(v);
}
// to icd-10
else if(selectedValue.equals(MAIN_MENU[0])) {
startActivity(new Intent(MainActivity.this, ICDActivity.class));
}
// to services
else if(selectedValue.equals(MAIN_MENU[3])) {
startActivity(new Intent(MainActivity.this, ServicesActivity.class));
}
}
});
}
public void contactPressed(View v) {
startActivity(new Intent(MainActivity.this, ContactActivity.class));
overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
}
}
And the final output (below)

Android Subitems on a Listview

My question: Why am I getting an error of java.lang.NullPointerException for trying to set set an adapter to one of my listviews in subListView.setAdapter(adapter2);
I am trying to create an app for learning purposes that will display subjects in a listview and when one of the items/subjects on the listview is clicked a sub item list will appear.
Right now I am just trying to show a listview view with their subitems shown.
Here is my code:
activity_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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:layout_alignLeft="#+id/button1"
android:layout_alignRight="#+id/listView1"
android:layout_below="#+id/button1"
android:text="#string/hello_world" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textview1" >
</ListView>
</RelativeLayout>
group_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/groupItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="#+id/sublistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/groupItem"
android:layout_marginLeft="26dp" >
</ListView>
</RelativeLayout>
db_items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/subSubjectTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Jesus is God"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/subIdtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Small Text"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/subScriptTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:text="John 1:1-12"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
//Creates DB and Tables if they do not exist... This part works
popList();
}
public void popList(){
ListView listView = (ListView) findViewById(R.id.listView1);
ListView subListView = (ListView) findViewById(R.id.sublistView);
TextView textv = (TextView)findViewById(R.id.textview1);
String topList[] = {"First", "Second", "Third"};
//Queries the DB and stores it in a Cursor... This part works
textv.append(" Cursor Count = " + c.getCount()); //this is for debugging purposes
int iId = c.getColumnIndex("id");
int iSummary = c.getColumnIndex("Summary");
int iScripts = c.getColumnIndex("Scripts");
int iDescription = c.getColumnIndex("Description");
int iSourceType = c.getColumnIndex("SourceType");
cursor.moveToFirst();
for (int j=0; j<c.getCount(); j++)
{
int image = 0;
if(c.getString(iSourceType).equals("Bible"))
{
image = R.drawable.bible;
}
if(c.getString(iSourceType).equals("Article"))
{
image = R.drawable.article;
}
if(c.getString(iSourceType).equals("Video"))
{
image = R.drawable.video;
}
myLVItems.add(new lvItem(c.getString(iId), c.getString(iSummary), c.getString(iScripts), c.getString(iDescription), image));
c.moveToNext();
}
ArrayAdapter<lvItem> adapter2 = new myListAdapter();
textv.append(" subListView Count = " + myLVItems.size()+ " adapter2 Count: " + adapter2.getCount()); //this is for debugging purposes
subListView.setAdapter(adapter2); //this is first error that the logcat points to
for (int i=0; i<topList.length; i++)
{
myGroupTopItems.add(new topgroupItem(topList[i], null));
}
ArrayAdapter<topgroupItem> adapter = new myTopListAdapter();
listView.setAdapter(adapter);
}
private class myListAdapter extends ArrayAdapter<lvItem>{
public myListAdapter(){
super(MainActivity.this, R.layout.db_items, myLVItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// make sure we have a view to work with (may have been given null
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.db_items, parent, false);
}
//find the item to work with
lvItem currentLVItem = myLVItems.get(position);
//fill the view
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView1);
imageView.setImageResource(currentLVItem.getIconId());
TextView hiddenView = (TextView) itemView.findViewById(R.id.subIdtextView);
hiddenView.setText(currentLVItem.getId());
TextView summaryView = (TextView) itemView.findViewById(R.id.subSubjectTextView);
summaryView.setText(currentLVItem.getSummary());
TextView descripView = (TextView) itemView.findViewById(R.id.subScriptTextView2);
descripView.setText(currentLVItem.getScripts());
return itemView;
}
}
private class myTopListAdapter extends ArrayAdapter<topgroupItem>{
public myTopListAdapter() {
super(MainActivity.this, R.layout.group_item, myGroupTopItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.group_item, parent, false);
}
topgroupItem currentTGItem = myGroupTopItems.get(position);
TextView textView = (TextView) itemView.findViewById(R.id.groupItem);
textView.setText(currentTGItem.getText());
return itemView;
}
}
}
This is what textv displays: Cursir Count = 3 subListView Count = 3 adapter2 Count: 3
I'm not sure why I am getting a Null for subListView. I'm not looking for an expanded listview with what I am doing
Sorry for all the code. Any help would be greatly appreciated
First: you call popList() in onCreate(). That is before the List gets populated with items and therefore there is no child view with the id sublistView.
Second: DO NOT ADD A LIST INTO A LIST ITEM! No ScrollViews inside another ScrollView! You might wanna check out ExpandableListView if you'd like to have sub lists.

ListView Item not clickable

im trying to make an activity that searches a DB based on an input string and then returns the results in a list view. All this is working however, My listview items are not clickable. I have tried changing the focus and clickable attributes but nothing has worked so far.
Main XML (add_friend_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="#+id/AddFriendHeaderLayout"
android:layout_marginTop="3dip"
android:layout_height="45dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center">
<EditText
android:id="#+id/et_SearchFriend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/bt_SearchFriend"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:focusable="false"
android:icon="#android:drawable/ic_search_category_default"
android:text="Search" >
</Button>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="60dip"
android:layout_marginBottom="5dip"
android:layout_alignParentBottom="true"
android:layout_above="#+id/AddFriendHeaderLayout"
android:id="#+id/searchFriendFooterLayout">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</RelativeLayout>
ListView XML (single_listview_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true" >
<TextView
android:id="#+id/tv_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
</LinearLayout>
Main Java Function (SearchFriendActivity.java):
public class SearchFriendActivity extends ListActivity {
//Progress Dialog
private ProgressDialog pDialog;
private Button bt_searchFriend;
private EditText et_Search_String;
private ListView tv_listview;
String[] arrFriends;
String[] arrUserId;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.add_friend_layout);
bt_searchFriend= (Button) findViewById(R.id.bt_SearchFriend);
et_Search_String = (EditText) findViewById(R.id.et_SearchFriend);
tv_listview = (ListView) findViewById(R.id.tv_list);
String searchString="";
//searchString = String.valueOf(et_Search_String.getText());
Log.i ("log_search","value of searchString: " + searchString);
bt_searchFriend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
try{
searchFriends aTask = new searchFriends();
aTask.execute(String.valueOf(et_Search_String.getText()));
String rResult = aTask.get();
// Convert aResult to array
arrFriends = convertJSONData(rResult);
arrUserId = convertJSONDataToUserArray(rResult);
setListAdapter(new ArrayAdapter<String>
(getBaseContext(),
R.layout.single_listview_layout,
R.id.tv_list,
arrFriends));
}catch(Exception e){
Log.e ("log_search_load",e.toString());
}
}
});
tv_listview = getListView();
//handler for List Item click
tv_listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
"Friend request sent for " +
((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Log.i("log_search_listclick", "Listview clicked");
}
});
}catch(Exception e){
Log.e ("log_search_load2",e.toString());
}
}
Firstly, don't use focusable and clickable. When you use setOnclick or another they are auto creating.
Secondly, remove try cache blog then start application. Because when the problem created program will contining and you can't understand where is the problem.
===>Thirdly<====
you are used (tv_listview=...) second twice
tv_listview = (ListView) findViewById(R.id.tv_list);
tv_listview = getListView();
then you are trying tv_listview.setOnItemClickListener..... If second view is not first view seton not working on "R.id.tv_list".

Categories

Resources