Android ExpandableListView does not expand/not clickable - java

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.

Related

Unexpected behaviour with custom ListView adapter

So I've got a listView and each item basically displays 3 TextView-s one of the TextView-s represents username and if the username is equal to the logged user the item shall be highlighted(see code)
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class ListAdapter extends ArrayAdapter<HallOfFame.User> {
private int resourceLayout;
private Context mContext;
public ListAdapter(Context context, int resource, List<HallOfFame.User> items) {
super(context, resource, items);
this.resourceLayout = resource;
this.mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
v = vi.inflate(resourceLayout, null);
}
HallOfFame.User user = getItem(position);
if (user != null) {
TextView rankText = v.findViewById(R.id.rank);
TextView usernameText = v.findViewById(R.id.username);
TextView scoreText = v.findViewById(R.id.score);
rankText.setText(Integer.toString(user.rank));
usernameText.setText(user.username);
scoreText.setText(Integer.toString(user.overall));
if(Utils.USERNAME.equals(user.username)){
usernameText.setTextColor(Color.RED);
}
}
return v;
}
}
In this example only MikiThenics should be highlighted. Adding else after if(Utils.USERNAME.eq...) will stop the other usernames from being highlighted but there's still this weird shift with the number on the right. And the items that are "not working" aren't always the same.
This is how the HallOfFame.User looks:
public class User{
public String username;
public int pullups=0,pushups,dips,handstandpushups,plank,pistolsquats,muscleups;
public int overall;
public int rank = 0;
public User(String data,int i){
try {
JSONObject jsonObject = new JSONObject(data);
username = jsonObject.getString(Utils.S_USERNAME);
pullups = jsonObject.getInt(Utils.S_PULL_UPS);
pushups = jsonObject.getInt(Utils.S_PUSH_UPS);
dips = jsonObject.getInt(Utils.S_DIPS);
handstandpushups = jsonObject.getInt(Utils.S_HANDSTAND_PUSHUPS);
plank = jsonObject.getInt(Utils.S_PLANK);
pistolsquats = jsonObject.getInt(Utils.S_PISTOL_SQUATS);
muscleups = jsonObject.getInt(Utils.S_MUSCLE_UPS);
overall = pullups+pushups+dips+handstandpushups+pistolsquats+muscleups+plank/60;
rank = i;
if(username.equals(Utils.USERNAME))
yourPosition = i;
}catch (Exception e){
}
}
}
This is the example of the JSON I'm using
String tmp = "[{\"username\":\"MikiThenics\",\"pullups\":\"25\",\"pushups\":\"70\",\"dips\":\"30\",\"handstandpushups\":\"16\",\"muscleups\":\"8\",\"pistolsquats\":\"1\",\"plank\":\"360\"},{\"username\":\"user01\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user02\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user0\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user1\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user2\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user3\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user4\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user5\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user6\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user7\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user8\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user9\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user10\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user11\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user12\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user13\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user14\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user15\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user16\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user17\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user18\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user19\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user20\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user21\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user22\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user23\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user24\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user25\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user26\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user27\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user28\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user29\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user30\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user31\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user32\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user33\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user34\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user35\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user36\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user37\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user38\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user39\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user40\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user41\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user42\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user43\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user44\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user45\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user46\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user47\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user48\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user49\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user50\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user51\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user52\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user53\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user54\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user55\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user56\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user57\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user58\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user59\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user60\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user61\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user62\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user63\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user64\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user65\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user66\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user67\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user68\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user69\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user70\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user71\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user72\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user73\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user74\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user75\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user76\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"},{\"username\":\"user77\",\"pullups\":\"0\",\"pushups\":\"0\",\"dips\":\"0\",\"handstandpushups\":\"0\",\"muscleups\":\"0\",\"pistolsquats\":\"0\",\"plank\":\"0\"}]";
The problem is caused by the username. Since setting username in User to fixed String solves the problem.
Even though I'm using fixed string as JSON the "not working items" aren't the same when restarting the activity.
It's a XML issue. You can try with this.
<?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"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="#+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="RANK"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="16sp"
android:layout_alignParentStart="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="Name"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="16sp"
android:layout_toEndOf="#+id/rank"
/>
<TextView
android:id="#+id/score"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="SCORE"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="16sp"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You can add an boolean into your if statement that at first it is true and when tries to highlited the username's text , set the boolean false
if(Utils.USERNAME.equals(user.username) && boolean){
usernameText.setTextColor(Color.RED);
}

Android ListView not scrolling at all

I've a ListView that gets populated from a JSON response.
The results go beyond the screen and whatever I tried the listview won't be scrolling.
Currently the XML looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".DrinkActivity">
<ListView
android:id="#+id/drinkList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbarSize="3dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:scrollingCache="true"
android:smoothScrollbar="true" />
</LinearLayout>
Adapter which the listview is using:
package com.example.tijn.bartenderapp;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Map;
public class DrinksAdapter extends ArrayAdapter<Drink> {
private Activity activity;
private ArrayList<Drink> drinksArrayList;
private static LayoutInflater inflater = null;
public DrinksAdapter(Activity activity, int textViewResourceId, ArrayList<Drink> _drinksArrayList) {
super(activity, textViewResourceId, _drinksArrayList);
try {
this.activity = activity;
this.drinksArrayList = _drinksArrayList;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} catch (Exception ex) {
}
}
#Override
public int getCount() {
return drinksArrayList.size();
}
public Drink getItem(Drink position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView display_name;
public TextView ingredient0;
public TextView ingredient1;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final DrinksAdapter.ViewHolder holder;
try {
if (convertView == null) {
vi = inflater.inflate(R.layout.drinksrow, null);
holder = new DrinksAdapter.ViewHolder();
holder.ingredient0 = (TextView) vi.findViewById(R.id.ingredientName0);
holder.ingredient1 = (TextView) vi.findViewById(R.id.ingredientName1);
vi.setTag(holder);
} else {
holder = (DrinksAdapter.ViewHolder) vi.getTag();
}
Drink d = drinksArrayList.get(position);
holder.ingredient0.setText(d.getName());
StringBuilder ingredients = new StringBuilder();
for (Map.Entry<String, Integer> ingredient : d.getIngredients().entrySet()) {
ingredients.append(" ");
ingredients.append(ingredient.getKey());
ingredients.append(", ");
ingredients.append(Integer.toString(ingredient.getValue()));
ingredients.append("ML \n");
}
holder.ingredient1.setText(ingredients.toString().trim());
} catch (Exception e) {
}
return vi;
}
}
Here is the contents of the drinksrow.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/ingredientName0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ingredientName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="92dp"
android:layout_marginTop="40dp"
app:layout_constraintStart_toEndOf="#+id/ingredientName0"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ingredientName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="40dp"
tools:layout_editor_absoluteY="69dp" />
<TextView
android:id="#+id/ingredientName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="192dp" />
</android.support.constraint.ConstraintLayout>
Here is the method that sets the ListView :
#Override
public void processFinish(Object output) {
String jsonString = output.toString();
Gson gson = new Gson();
ArrayList<Drink> drinks = gson.fromJson(jsonString, new TypeToken<List<Drink>>(){}.getType());
final ListView listview = (ListView) findViewById(R.id.drinkList);
final DrinksAdapter adapter = new DrinksAdapter(this, R.layout.drinksrow, drinks);
listview.setAdapter(adapter);
listview.setEnabled(false);
}
Already tried to surround the LinearLayout with a ScrollView.
Anybody got an idea?
listview.setEnabled(false);
This disables scrolling.
Not sure why you want to set it to false.
You can use
<ScrollView
android:layout_width="..."
android:layout_height="...">
<LinearLayout
android:orientation="vertical"
android:layout_width="..."
android:layout_height="...">
I use it, and works.

Android: onItemClick and onItemLongClick do not respond

My Android app is composed of an SQLite database which populates individual ListView items with data user saves. Those items are available for display in activity_main.xml.
I have a class called RecordsListFragment which contains the two problematic methods: onItemClick and onItemLongClick. Here is the class in its entirety:
package com.example.benignfella.projectworkinghoursapplication.Fragment;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import com.example.benignfella.projectworkinghoursapplication.R;
import com.example.benignfella.projectworkinghoursapplication.Adapter.RecordsListAdapter;
import com.example.benignfella.projectworkinghoursapplication.Database.RecordsDAO;
import com.example.benignfella.projectworkinghoursapplication.GetSet.Records;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
public class RecordsListFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener {
public static final String ARGUMENT_ITEM_ID = "records_list";
Activity activity;
ListView recordsListView;
ArrayList<Records> records;
RecordsListAdapter recordsListAdapter;
RecordsDAO recordsDAO;
private GetRecordsTask task;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
recordsDAO = new RecordsDAO(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_records_list, container, false);
findViewsById(view);
task = new GetRecordsTask(activity);
task.execute((Void) null);
return view;
}
private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Records record = (Records) parent.getItemAtPosition(position);
if (records != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedRecord,", record);
CustomRecordsDialogFragment customRecordsDialogFragment = new CustomRecordsDialogFragment();
customRecordsDialogFragment.setArguments(arguments);
customRecordsDialogFragment.show(getFragmentManager(), CustomRecordsDialogFragment.ARGUMENT_ITEM_ID);
}
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Records records = (Records) parent.getItemAtPosition(position);
recordsDAO.deleteRecord(records);
recordsListAdapter.remove(records);
return true;
}
public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>> {
private final WeakReference<Activity> activityWeakRef;
public GetRecordsTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
#Override
protected ArrayList<Records> doInBackground(Void... arg0) {
ArrayList<Records> recordsArrayList = recordsDAO.getRecords();
return recordsArrayList;
}
#Override
protected void onPostExecute(ArrayList<Records> empList) {
if (activityWeakRef.get() != null && !activityWeakRef.get().isFinishing()) {
records = empList;
if (empList != null) {
if (empList.size() != 0) {
recordsListAdapter = new RecordsListAdapter(activity, empList);
recordsListView.setAdapter(recordsListAdapter);
} else {
Toast.makeText(activity, "No Records about records... wait wot m8?",
Toast.LENGTH_LONG).show();
}
}
}
}
}
public void updateView() {
task = new GetRecordsTask(activity);
task.execute((Void) null);
}
public void onResume() {
getActivity().setTitle(R.string.app_name);
getActivity().getActionBar().setTitle(R.string.app_name);
super.onResume();
}
}
Here is activity_main.xml with FrameLayout:
<RelativeLayout
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">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Layout resource file which contains a ListView is called fragment_records_list.xml and here it is:
<?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"
android:background="#f9f9f9">
<ListView
android:id="#+id/list_records"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:drawSelectorOnTop="true"
android:footerDividersEnabled="false"
android:padding="10dp"
android:scrollbarStyle="outsideOverlay"/>
</RelativeLayout>
Lastly, there is a resource file containing the layout of a single item, list_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"
android:background="#ededed"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:id="#+id/layout_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_record_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="ID"
android:textSize="20dp"/>
<TextView
android:id="#+id/text_record_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/text_record_id"
android:padding="5dp"
android:text="Date"
android:textColor="#00942b"
android:textSize="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/text_record_date"
android:drawableStart="#drawable/ic_date_range_black_24dp"
android:padding="5dp"
/>
<TextView
android:id="#+id/text_record_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_id"
android:padding="5dp"
android:text="Description"
/>
<TextView
android:id="#+id/text_record_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_description"
android:padding="5dp"
android:text="17:00"
android:textSize="16dp"
android:textColor="#004561"
/>
<TextView
android:id="#+id/text_record_dash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_description"
android:layout_toRightOf="#id/text_record_start"
android:padding="5dp"
android:text="-"
android:textSize="16dp"
/>
<TextView
android:id="#+id/text_record_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_description"
android:layout_toRightOf="#id/text_record_dash"
android:padding="5dp"
android:text="20:00"
android:textSize="16dp"
android:textColor="#c7002a"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_below="#id/text_record_description"
android:layout_toRightOf="#id/text_record_finish"
android:drawableStart="#drawable/ic_timer_black_24dp"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/layout_item"
android:background="#000000"
/>
</RelativeLayout>
I haven't found a definite answer to my question, so I'm asking for a bit of help here.
You need set OnItemClickListener and OnLongItemClickListener to ListView, I edited your method in the initializing variable ListView:
private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
recordsListView.setOnItemClickListener(this);
recordsListView.setOnItemLongClickListener(this);
}

Recyclerview is not displayed in fullscreen

Recyclerview is not displayed in fullscreen. Below is the layout file and the code I've written. After executing, the contents are displayed as shown in the below picture. The height is only the highlighted part. I want the contents to be fullscreen. I remaining contents are within this highlighted area which is scrollable. I want the contents to be displayed in fullscreen. Any help would be helpful.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/activity_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="#+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/genre"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
Code:
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;
public class setSearchData extends RecyclerView.Adapter<setSearchData.MyViewHolder> {
private List<SearchDisplayContents> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
year = (TextView) view.findViewById(R.id.year);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_searchresults, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
SearchDisplayContents movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(movie.getYear());
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
Calling function code:
List<SearchDisplayContents> movieList = new ArrayList<>();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
setSearchData mAdapter = new setSearchData(movieList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
SearchDisplayContents movie = new SearchDisplayContents("Mad Max: Fury Road", "Action & Adventure", "2015");
movieList.add(movie);
movie = new SearchDisplayContents("Inside Out", "Animation, Kids & Family", "2015");
movieList.add(movie);
mAdapter.notifyDataSetChanged();
You should use different layout for your viewholder. Take the RelativeLayout you have there, cut it out, put it in different xml file then pass it to onCreateViewHolder.
From look at your code - it looks like what is in the preview screenshot is the same as what is within the RelativeLayout you have staticly defined.
There is an issue with your approach. It is that you are referencing the RelativeLayout (within the same xml hierarchy as your RecylerView) as the CellViewHolder in the RecyclerView.Adapter. My approach personally is to create a seperate xml layout for the cell viewholder.
Follow this guide, it is very detailed:
https://guides.codepath.com/android/using-the-recyclerview
You should use different layout for your viewholder. Take the RelativeLayout you have there, cut it out, put it in different xml file then pass it to onCreateViewHolder.
The above coding has something work use different layout mean one for Recycleview and other for contents in list
main_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"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/uslist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="#dimen/_49sdp"
android:divider="#android:color/transparent" />
</RelativeLayout>
list_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="#+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/genre"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
Adapter.java
public class yourAdapter extends RecyclerView.Adapter<yourAdapter .SimpleViewHolder> {
ArrayList<UsageRPDetails> mylist;
private Context mContext;
public yourAdapter (Context context, ArrayList<yourArray or model> checklist) {
mContext = context;
mylist = checklist;
}
#Override
public yourAdapter .SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_content, parent, false);
return new yourAdapter .SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(final yourAdapter .SimpleViewHolder holder, final int position) {
holder.title.setText();
holder.year.setText();
holder.genre.setText();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public int getItemCount() {
return mylist.size();
}
#Override
public int getItemViewType(int i) {
return 0;
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.title)
TextView title;
#Bind(R.id.genre)
TextView genre;
#Bind(R.id.year)
TextView year;
public SimpleViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.title);
genre= (TextView) itemView.findViewById(R.id.genre);
genre= (TextView) itemView.findViewById(R.id.genre);
}
}
}
Also write a java file for Activity that calls this adapter class and call the recycleview in this activity

RecycleView Android Crashing With Error: Resource ID #0x7f0d00aa type #0x12 is not valid

I'm implementing adapter for recycleview in android, but it throws error Resource ID #0x7f0d00aa type #0x12 is not valid.
This is my card layout for client_info_fragment_revision_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content"
app:cardElevation="2dp"
android:layout_margin="7dp"
android:clickable="true"
android:focusable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/revMainLayout">
<!--TOP-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/revRelativeTop">
<ImageView android:id="#+id/imagePartRevision"
android:layout_width="28dp"
android:layout_height="28dp"
android:scaleType="fitXY"
android:layout_marginLeft="7dp" android:layout_marginTop="5dp"
android:src="#drawable/ic_indicator_program"/>
<TextView
android:id="#+id/partRevision"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:maxLines="2"
android:padding="10dp"
android:text="Paper"
android:textSize="16sp" android:layout_marginLeft="34dp"/>
<TextView
android:id="#+id/deadlineRevision"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="23-04-2016"
android:textSize="16sp"
android:padding="10dp"
android:layout_marginRight="7dp"/>
</RelativeLayout>
<!--MIDDLE-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/revLinearMiddle">
<View
android:id="#+id/divider1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#a4b9adba"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id="#+id/listViewRevision"/>
<View
android:id="#+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#a4b9adba"/>
</LinearLayout>
<!--BOTTOM-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/revRelativeBottom">
<TextView
android:id="#+id/statusRevision"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/statusRevisionIndicator"
android:maxLines="2"
android:padding="10dp"
android:text="On Progress"
android:textSize="16sp"/>
<ImageView
android:id="#+id/statusRevisionIndicator"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_alignBottom="#+id/statusRevision"
android:layout_alignParentRight="true"
android:layout_below="#id/divider"
android:layout_marginRight="7dp" android:layout_centerVertical="true"
android:layout_marginBottom="5dp" android:src="#drawable/ic_indicator_not_finished"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And this is code for my adapter RevisionRecycleCardsAdapter.java:
package com.putraxor.prola.skripsi.client.activity.profileui;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.putraxor.prola.skripsi.R;
import com.putraxor.prola.skripsi.pojo.Revisi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Putra on 27/03/2016.
*/
public class RevisionRecycleCardsAdapter extends RecyclerView.Adapter<RevisionRecycleCardsAdapter.CardViewHolder> {
private ArrayList<Revisi> revision_list;
Context context;
public static class CardViewHolder extends RecyclerView.ViewHolder{
ImageView imagePartRevision;
ImageView statusRevisionIndicator;
TextView partRevision;
TextView deadlineRevision;
TextView statusRevision;
ListView listViewRevision;
public CardViewHolder(View itemView) {
super(itemView);
imagePartRevision = (ImageView)itemView.findViewById(R.id.imagePartRevision);
statusRevisionIndicator = (ImageView)itemView.findViewById(R.id.statusRevisionIndicator);
partRevision = (TextView)itemView.findViewById(R.id.partRevision);
deadlineRevision = (TextView)itemView.findViewById(R.id.deadlineRevision);
statusRevision = (TextView)itemView.findViewById(R.id.statusRevision);
listViewRevision = (ListView)itemView.findViewById(R.id.listViewRevision);
}
}
public RevisionRecycleCardsAdapter(ArrayList<Revisi> revision) {
this.revision_list = revision;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.client_info_fragment_revision_layout, parent, false);
CardViewHolder cardViewHolder = new CardViewHolder(view);
return cardViewHolder;
}
#Override
public void onBindViewHolder(final CardViewHolder holder, final int listPosition) {
Revisi rev = revision_list.get(listPosition);
int srcImagePartRevision = rev.getBagian().equalsIgnoreCase("Paper") ? R.drawable.ic_indicator_paper : R.drawable.ic_indicator_program;
int srcStateIndicator = rev.isSelesai() ? R.drawable.ic_indicator_finished : R.drawable.ic_indicator_not_finished;
String status = rev.isSelesai()?"Revisions Finished" : "On Process";
holder.imagePartRevision.setImageResource(srcImagePartRevision);
holder.partRevision.setText(rev.getBagian());
holder.deadlineRevision.setText(rev.getDeadline());
holder.statusRevision.setText(status);
holder.statusRevisionIndicator.setImageResource(srcStateIndicator);
String[] values = rev.getRevisi();
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
StableArrayAdapter adapter = new StableArrayAdapter(this.context, R.id.listViewRevision, list);
holder.listViewRevision.setAdapter(adapter);
}
#Override
public int getItemCount() {
return revision_list.size();
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
}
Change your code in
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.R.layout.client_info_fragment_revision_layout, parent, false);
CardViewHolder holder = new CardViewHolder (view);
}
It's just for debug purpose:
If you are using Android studio then open your R.java under folder
build->generated->source->r->debug->yourpackagename->R.java
Then search 0x7f0d00aa in the file; It will be like
public static final int TextView01=0x7f0d00aa;
Search the corresponding ID in xml files under resources folder. Then check the XML content syntax,naming,source etc.

Categories