Why do my Custom GridView show only one column? - java

I'v tried making a Custom GridView that adds proggramatically TextView items.
The TextViews needs to act as an ImageView only that it has a text within it.
But for some odd reason it shows me only 1 column out of 4 (but if its too big then 3 is my minimum capcity).
I look through similar cases for people who attempted the same thing, but all the same as it ended failing.
All I get is a one column Gridview each time.
I'd like to hear some opinions about it, in fix this issue.
My GridView:
<GridView
android:id="#+id/gridView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="73dp"
android:layout_toEndOf="#+id/imageView15"
android:columnCount="4"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth" />
My item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:backgroundTint="#android:color/black"
android:alpha="100"
android:layoutDirection="ltr">
<TextView
android:id="#+id/item"
android:layout_width="150px"
android:layout_height="150px"
android:layout_marginRight="50px"
android:textColor="#android:color/black"
android:textSize="10sp" />
</LinearLayout>
And finally the Adapter:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CustomListAdapter extends BaseAdapter {
private Activity context;
private List<TextView> items;
private static LayoutInflater inflater;
public CustomListAdapter(Activity context, List<TextView> items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.inventory_item, null);
Holder holder = new Holder();
holder.tv = (TextView) rowView.findViewById(R.id.item);
holder.tv.setText(items.get(position).getText());
holder.tv.setBackground(items.get(position).getBackground());
holder.tv.setTextColor(items.get(position).getTextColors());
return rowView;
}
}
Thanks in advance.

shows me only 1 column out of 4
you need to use: android:numColumns="4"
in your code:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="#color/color_white"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none|horizontal"
android:stretchMode="columnWidth"></GridView>

Related

Why is my view changing color upon reloading?

I am developing an android app, and need to use a recyclerview for it. I want its background to be white, but for some reason when I run the app it turns out gray. That being said, if I reload the activity, or go back and then again to the activity, the color does turn white. Has anyone ever encountered such a problem?
This is how it looks when you first enter the app
And this is what happens if you reload the activity.
Here's my item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/trip"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#null"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/destination"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".333"
android:gravity="center"
android:textSize="24sp" />
<TextView
android:id="#+id/start_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:gravity="center"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="#+id/destination" />
<TextView
android:id="#+id/trip_length"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".333"
android:gravity="center" />
</LinearLayout>
and here is my main activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView3"
android:layout_width="180dp"
android:layout_height="180dp"
android:contentDescription="#string/app_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/app_logo" />
<TextView
android:id="#+id/main_name"
android:layout_width="359dp"
android:layout_height="26dp"
android:text="#string/welcome_message"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.865"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.178" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/trips"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:fadingEdge="horizontal|vertical"
android:maxHeight="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my adapter
package com.alonkh2.finalproject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.ViewHolder> {
private ArrayList<Trip> trips;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView destination, startDate, endDate, length;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
destination = (TextView) itemView.findViewById(R.id.destination);
startDate = (TextView) itemView.findViewById(R.id.start_date);
// endDate = (TextView) itemView.findViewById(R.id.end_date);
length = (TextView) itemView.findViewById(R.id.trip_length);
}
}
public TripAdapter(ArrayList<Trip> trips) {
this.trips = trips;
}
#NonNull
#Override
public TripAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.activity_listview, parent, false);
// Return a new holder instance
return new ViewHolder(contactView);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull TripAdapter.ViewHolder holder, int position) {
Trip trip = trips.get(position);
// Set item views based on your views and data model
TextView textView = holder.destination;
textView.setText(trip.getDestination());
textView = holder.startDate;
textView.setText(trip.getStrStartDate().substring(0, trip.getStrStartDate().indexOf(" ")));
// textView = holder.endDate;
// textView.setText(trip.getStrEndDate().substring(0, trip.getStrEndDate().indexOf(" ")));
textView = holder.length;
textView.setText(trip.getLength() + (trip.getLength() == 1 ? " day" : " days"));
}
#Override
public int getItemCount() {
return trips.size();
}
}
Put this code below to replace your TripAdapter code.
package com.alonkh2.finalproject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.ViewHolder> {
Context context;
private ArrayList<Trip> trips;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView destination, startDate, endDate, length;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
destination = (TextView) itemView.findViewById(R.id.destination);
startDate = (TextView) itemView.findViewById(R.id.start_date);
// endDate = (TextView) itemView.findViewById(R.id.end_date);
length = (TextView) itemView.findViewById(R.id.trip_length);
}
}
public TripAdapter(Context context, ArrayList<Trip> trips) {
this.context = context;
this.trips = trips;
}
#NonNull
#Override
public TripAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.activity_listview, parent, false);
// Return a new holder instance
return new ViewHolder(contactView);
}
#Override
public void onBindViewHolder(#NonNull TripAdapter.ViewHolder holder, int position) {
Trip trip = trips.get(position);
// Set item views based on your views and data model
holder.destination.setText(trip.getDestination());
holder.startDate.setText(trip.getStrStartDate().substring(0, trip.getStrStartDate().indexOf(" ")));
// holder.endDate.setText(trip.getStrEndDate().substring(0, trip.getStrEndDate().indexOf(" ")));
holder.length.setText(trip.getLength() + (trip.getLength() == 1 ? " day" : " days"));
}
#Override
public int getItemCount() {
return trips.size();
}
}

Get access to other view in fragment

I have a problem with setting the visibility of an TextView.
I have two xmls. One for the fragment itself and the other for the RecyclerView.
In the fragment_settings.xml I'm setting the RecyclerView. In the settings_list_row.xml I define the RecyclerView row.
When I create a new RecyclerView row I sometimes have a row where twice of the TextView's are filled and sometimes just one. Because of this I want to set the visibility of one TextView to gone if it's empty.
Because my View is the fragment_settings.xml I don't have access to the row view. Is there a solution for this? Thanks a lot!
fragment_settings.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.myfitindustry.myfitindustry.app.SettingsFragment">
<TextView
android:id="#+id/accountTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:text="#string/accountTitle" />
<android.support.v7.widget.RecyclerView
android:id="#+id/settings_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
settings_list_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:focusable="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/settingTitle"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:textSize="16dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/settingSubtitle"
android:layout_below="#id/settingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Fragments onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.settings_recycler_view);
sAdapter = new SettingsAdapter(settingList);
RecyclerView.LayoutManager sLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()) {
// Disable scrolling in the RecyclerView
#Override
public boolean canScrollVertically() {
return false;
}
};
recyclerView.setLayoutManager(sLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
prepareSettingsData();
recyclerView.setAdapter(sAdapter);
// HERE I WANT TO CHANGE THE VISIBILITY
TextView settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
if(!settingSubtitle.getText().equals("")) {
settingSubtitle.setVisibility(TextView.GONE);
}
return view;
}
My RecyclerView Adapter:
package de.myfirstapp.app;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by Johannes on 19.01.2017.
*/
public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.MySettingHolder> {
private List<Settings> settingList;
public class MySettingHolder extends RecyclerView.ViewHolder {
public TextView settingTitle, settingSubtitle;
public MySettingHolder(View view) {
super(view);
settingTitle = (TextView) view.findViewById(R.id.settingTitle);
settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
}
}
public SettingsAdapter (List<Settings> settingList) {
this.settingList = settingList;
}
#Override
public MySettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.settings_list_row, parent, false);
return new MySettingHolder(itemView);
}
#Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
#Override
public int getItemCount() {
return settingList.size();
}
}
Why you don't do it in SettingsAdapter onBindViewHolder method? Imagine, you have bunch of items in your RecyclerView and bunch of settingSubtitle, how system will understand which TextView do you really want?
Update
#Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
if (setting.getSettingSubtitle().equals("")) {
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(View.GONE);
} else {
holder.settingSubtitle.setVisibility(View.VISIBLE);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
}

Display Data in GridView with DataAdapter in Android

I have try the following code for displaying data from data adapter in gridview.
Following link is for GridView with DataAdapter Tutorial.
I am wanted the following output :
I have follow the tutorial and get the output as i wanted, but unfortunetly when scroll the page,
application will be closed automatically.
ERROR : "Application Failure Detected, Please Try Again"
How to resolve these problem?
code : main.xml file.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip" />
code : MainActivity.java file.
package com.example.gridviewdata;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new DataAdapter(this));
}
code : customgrid.xml file
<?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="fill_parent"
android:orientation="vertical">
<TableLayout android:id="#+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow android:id="#+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtId"
android:layout_gravity="center_horizontal" />
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
code : DataAdapter.java file
package com.example.gridviewdata;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DataAdapter extends BaseAdapter {
Context mContext;
private String [] id = {"S001","S002","S003","S004","S005","S006","S007","S008","S009","S010","S011","S012"};
private String [] name={"Rohit","Rahul","Ravi","Amit","Arun","Anil","Kashif","Nayan","Jay","Sagar","Jairaj","Vishal"};
private LayoutInflater mInflater;
public DataAdapter(Context c)
{
mContext=c;
mInflater = LayoutInflater.from(c);
}
public int getCount()
{
return id.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder=null;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.customgrid,parent,false);
holder = new ViewHolder();
holder.txtId=(TextView)convertView.findViewById(R.id.txtId);
holder.txtId.setPadding(100, 10,10 , 10);
holder.txtName=(TextView)convertView.findViewById(R.id.txtName);
holder.txtName.setPadding(100, 10, 10, 10);
if(position==0)
{
convertView.setTag(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtId.setText(id[position]);
holder.txtName.setText(name[position]);
return convertView;
}
static class ViewHolder
{
TextView txtId;
TextView txtName;
}
}
Give any solution for these problem.
if any other resource is available then give me an reference.
Solution :
In getView() method return all items, and i have setting the tag just in the first item,
when android calls again getView for position 1, i am try to get the tag from convertView,
but i didn't called setTag for convertView 1, then getTag returns a null object, and i am try to get the object txtName from a null object, and then the error occurs.
removing the if(position==0) then application work perfectly and give the scrolling.

Android ExpandableListView does not expand/not clickable

i'm pretty new to android programming and tried to implement a basic expandable listview showing a list of Courses which should show some additional details when clicked. i feel like the problem is very basic as i don't include anything fancy in any of the views, but i still could not find a solution online, most of the other questions are a lot more specific.
The first part works fine(displaying the list of courses), the problem is that the list is not clickable, meaning it does not expand.
what i have determined right now is the following:
1. the getChildView is never called(debugger never stops at breakpoint)
2. there is data for the children as it's the same data that's used for the groupheading which works fine.
I have also tried to set an onGroupClickListener which was never called, so i removed it again.
my code:
Group heading layout:
<TextView
android:id="#+id/CourseName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="CourseName"
android:layout_weight="7"
android:layout_marginRight="25dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/CourseDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_weight="3"
android:text="CourseDate" />
<CheckBox
android:id="#+id/checkBox"
style="?android:attr/starStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
child_row layout:
<?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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="Button" />
<TextView
android:id="#+id/phases"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:layout_below="#+id/button1"
android:text="phases:" />
<TextView
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/phases"
android:gravity="center"
android:text="information:" />
</RelativeLayout>
activity layout:
<?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/sportName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Sport Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/profile_title" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:numStars="5"
android:stepSize="1"
android:layout_centerInParent="true" />
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/sportName" >
</ExpandableListView>
</RelativeLayout>
Adapter java:
package ch.unibe.unisportbern.views.details;
import java.util.ArrayList;
import com.example.unisportbern.R;
import ch.unibe.unisportbern.support.Course;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class SportsAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Course> courseList;
public SportsAdapter(Context context, ArrayList<Course> courseList) {
this.context = context;
this.courseList = courseList;
}
#Override
public Object getChild(int index, int stub) {
return courseList.get(index);
}
#Override
public long getChildId(int index, int stub) {
return stub;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView phases = (TextView) convertView.findViewById(R.id.phases);
TextView info = (TextView) convertView.findViewById(R.id.info);
phases.setText("phases:\n" + courseList.get(groupPosition).getPhases());
info.setText(courseList.get(groupPosition).getInformation());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return courseList.get(groupPosition);
}
#Override
public int getGroupCount() {
return courseList.size();
}
#Override
public long getGroupId(int groupPosition) {
return courseList.get(groupPosition).getId();
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_heading, null);
}
TextView courseName = (TextView) convertView.findViewById(R.id.CourseName);
TextView courseDate = (TextView) convertView.findViewById(R.id.CourseDate);
courseName.setText(courseList.get(groupPosition).getName());
courseDate.setText(courseList.get(groupPosition).getDay() + courseList.get(groupPosition).getTime());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
activity java:
package ch.unibe.unisportbern.views.details;
import java.util.ArrayList;
import com.example.unisportbern.R;
import ch.unibe.unisportbern.support.Course;
import ch.unibe.unisportbern.support.DBMethodes;
import ch.unibe.unisportbern.support.Sport;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class DActivity extends Activity{
public final static String NAME = "SportName";
public final static String ID = "SportID";
private Sport sport;
private ArrayList<Course> courses;
private SportsAdapter sportsadapter;
private ExpandableListView myList;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSport();
getCourses();
setContentView(R.layout.details_layout);
myList = (ExpandableListView) findViewById(R.id.expandableListView);
sportsadapter = new SportsAdapter(this, courses);
myList.setAdapter(sportsadapter);
}
private void getCourses() {
DBMethodes dbMethodes = new DBMethodes(this);
try {
courses = dbMethodes.getAllCourses(sport);
} catch (Exception e) {
}
}
private void getSport() {
Intent intent = this.getIntent();
int id = intent.getIntExtra(ID, 0);
String name = intent.getStringExtra(NAME);
this.sport = new Sport(id, name);
}
}
thanks a lot in advance!
EDIT:
i found it by chance. the checkbox inside the groupheader was focusable, stealing all the clicks away from the list. to solve this problem simply set the focusable attribute of the checkbox/button in your list to false.
I found it by chance. the checkbox inside the groupheader was focusable, stealing all the clicks away from the list. to solve this problem simply set the focusable attribute of the checkbox/button in your list to false with either:
in Java:
checkbox.setFocusable(false);
OR, in xml:
android:focusable="false"
i was facing the same issue but what i added the below line to my Parent view
android:descendantFocusability="blocksDescendants"
and it worked for me.

Show an Image and Text in a ListView

I'm developing an App, that shows a ListView with 30 items.
In each row, there should be a text and an Image.
I searches for different tutorials, but unfortunately all don't work for me.
So I'm asking you to have a short look to my code and tell me what's wrong in there, because when I start the app, the ListView is empty.
mainActivity.java:
setContentView(R.layout.auswertung);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new MyListAdapter(this, fragen));
MyListAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<Frage>{
private final Context context;
private ArrayList<Frage> fragen;
public MyListAdapter(Context context, ArrayList<Frage> f) {
super(context, R.layout.reihe);
this.context = context;
fragen = f;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.reihe, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.textView4);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img1);
textView.setText("Frage " + (position+1));
if (fragen.get(position).getRichtig()==true)
{
imageView.setImageResource(R.drawable.richtig);
}
else
{
imageView.setImageResource(R.drawable.falsch);
}
return rowView;
}
}
auswertung.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:layout_weight="0.96"
android:background="#drawable/hintergrund"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="19dp"
android:text="Auswertung"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="460dp"
android:layout_marginTop="60dp">
</ListView>
</RelativeLayout>
reihe.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/textView4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="5"
android:textSize="16dp"
android:textColor="#000000" />
<ImageView
android:id="#+id/img1"
android:layout_width="25dp"
android:layout_height="25dp" />
</LinearLayout>
As you see, I have a background image, and would also ask you, how can I make the background of the ListView invisible, so that I can see my background behind the text and the Image?
you should pass the the ArrayAdapter constructour your Frage data set, or at least,
ovveride ArrayAdapter.getCount(), and let it returns the number of items in ArrayList<Frage> f
int getCount() {
return (f == null) ? 0 : f.size();
}
here the doc for getCount()

Categories