Unexpected behaviour with custom ListView adapter - java
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);
}
Related
Android app crashing when I start an activity using ListView and a custom ArrayAdapter [duplicate]
This question already has answers here: Null pointer Exception - findViewById() (12 answers) findViewById returns null (4 answers) What is a NullPointerException, and how do I fix it? (12 answers) Closed 3 years ago. I've created a Location class and when I click on Cinemas in MainActivity, I want to open another activity that contains the list of Location objects. For this goal I have created a custom location_list_item layout file and a custom ArrayAdapter. For some reason, when I click on Cinemas, my app crashes. Why? MainActivity.java package com.example.android.tourguide; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View cinemasView = findViewById(R.id.cinemas_layout); cinemasView.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, CinemasActivity.class); startActivity(intent); } }); } } activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical" android:background="#fff8e1" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/main_activity_title" android:textSize="30sp" android:textStyle="bold" android:textColor="#android:color/black" android:layout_gravity="center_horizontal" android:layout_marginBottom="16dp"/> <LinearLayout android:id="#+id/cinemas_layout" android:layout_width="match_parent" android:layout_height="120dp" android:background="#4a148c"> <ImageView android:layout_width="90dp" android:layout_height="90dp" android:src="#drawable/category_cinemas" android:layout_gravity="center_vertical" android:layout_marginLeft="16dp"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="#string/category_cinemas" android:textSize="25sp" android:textColor="#android:color/white"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#android:color/darker_gray" /> <LinearLayout android:id="#+id/restaurants_layout" android:layout_width="match_parent" android:layout_height="120dp" android:background="#4a148c"> <ImageView android:layout_width="90dp" android:layout_height="90dp" android:src="#drawable/category_restaurants" android:layout_gravity="center_vertical" android:layout_marginLeft="16dp"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="#string/category_restaurants" android:textSize="25sp" android:textColor="#android:color/white"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#android:color/darker_gray" /> <LinearLayout android:id="#+id/gyms_layout" android:layout_width="match_parent" android:layout_height="120dp" android:background="#4a148c"> <ImageView android:layout_width="90dp" android:layout_height="90dp" android:src="#drawable/category_gyms" android:layout_gravity="center_vertical" android:layout_marginLeft="16dp"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="#string/category_gyms" android:textSize="25sp" android:textColor="#android:color/white"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#android:color/darker_gray" /> <LinearLayout android:id="#+id/parks_layout" android:layout_width="match_parent" android:layout_height="100dp" android:background="#4a148c"> <ImageView android:layout_width="90dp" android:layout_height="90dp" android:src="#drawable/category_parks" android:layout_gravity="center_vertical" android:layout_marginLeft="16dp"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="#string/category_parks" android:textSize="25sp" android:textColor="#android:color/white"/> </LinearLayout> </LinearLayout> location_category_list_item.xml <?xml version="1.0" encoding="utf-8"?> <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="wrap_content" android:orientation="vertical" > <ImageView android:id="#+id/location_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="#+id/location_hours_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:text="Hours" android:textStyle="bold"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="#+id/location_working_days" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="16dp" tools:text="Every Day" /> <TextView android:id="#+id/location_working_hours" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="16dp" tools:text="09:00 – 00:00" /> </LinearLayout> <TextView android:id="#+id/location_address_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:text="Address" android:textStyle="bold"/> <TextView android:id="#+id/location_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" tools:text="09:00 – 00:00" /> <TextView android:id="#+id/location_phone_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:text="Phone" android:textStyle="bold"/> <TextView android:id="#+id/location_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" tools:text="+7 (555) 555-55-55" /> </LinearLayout> Location.java package com.example.android.tourguide; import androidx.annotation.NonNull; public class Location { private int mImageResourceId; private String mAddress; private String mPhone; private String mWorkingDays; private String mWorkingHours; public Location(int imageResourceId, String address, String phone, String workingDays, String workingHours) { mImageResourceId = imageResourceId; mAddress = address; mPhone = phone; mWorkingDays = workingDays; mWorkingHours = workingHours; } public int getImageResourceId() { return mImageResourceId; } public String getAddress() { return mAddress; } public String getPhone() { return mPhone; } public String getWorkingDays() { return mWorkingDays; } public String getWorkingHours() { return mWorkingHours; } #NonNull #Override public String toString() { return mImageResourceId + mAddress + mWorkingDays + mWorkingDays + mPhone; } } LocationAdapter.java package com.example.android.tourguide; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; public class LocationAdapter extends ArrayAdapter<Location> { /** * This is our own custom constructor (it doesn't mirror a superclass constructor). * The context is used to inflate the layout file, and the list is the data we want * to populate into the lists. * * #param context The current context. Used to inflate the layout file. * #param locations A List of Location objects to display in a list */ public LocationAdapter(Activity context, ArrayList<Location> locations) { // Here, we initialize the ArrayAdapter's internal storage for the context and the list. // the second argument is used when the ArrayAdapter is populating a single TextView. // Because this is a custom adapter for one ImageView and 4 TextViews, the adapter is not // going to use this second argument, so it can be any value. Here, we used 0. super(context, 0, locations); } /** * Provides a view for an AdapterView (ListView, GridView, etc.) * * #param position The position in the list of data that should be displayed in the * list item view. * #param convertView The recycled view to populate. * #param parent The parent ViewGroup that is used for inflation. * #return The View for the position in the AdapterView. */ #Override public View getView(int position, View convertView, ViewGroup parent) { // Check if the existing view is being reused, otherwise inflate the view View listItemView = convertView; if (listItemView == null) { listItemView = LayoutInflater.from(getContext()).inflate( R.layout.location_category_list_item, parent, false); } // Get the {#link Location} object located at this position in the list Location currentLocation = getItem(position); // Find the ImageView in the location_category_list_item.xml layout // with the ID location_image ImageView image = listItemView.findViewById(R.id.location_image); // Get the image resource ID from the current Location object and // set the image to imageView image.setImageResource(currentLocation.getImageResourceId()); // Find the TextView in the location_category_list_item.xml layout // with the ID location_working_days TextView workingDaysTextView = listItemView.findViewById(R.id.location_working_days); // Get the working days from the current Location object and // set this text on the workingDaysTextView workingDaysTextView.setText(currentLocation.getWorkingDays()); // Find the TextView in the location_category_list_item.xml layout // with the ID location_working_hours TextView workingHoursTextView = listItemView.findViewById(R.id.location_working_hours); // Get the working hours from the current Location object and // set this text on the workingHoursTextView workingHoursTextView.setText(currentLocation.getWorkingHours()); // Find the TextView in the location_category_list_item.xml layout // with the ID location_address TextView addressTextView = listItemView.findViewById(R.id.location_address); // Get the address from the current Location object and // set this text on the addressTextView addressTextView.setText(currentLocation.getAddress()); // Find the TextView in the location_category_list_item.xml layout // with the ID location_phone TextView phoneTextView = listItemView.findViewById(R.id.location_phone); // Get the address from the current Location object and // set this text on the phoneTextView phoneTextView.setText(currentLocation.getPhone()); // Return the whole list item layout // so that it can be shown in the ListView return listItemView; } } CinemasActivity.java package com.example.android.tourguide; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; public class CinemasActivity extends AppCompatActivity { #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.location_category_list_item); //Create a list of cinemas final ArrayList<Location> locations = new ArrayList<>(); locations.add(new Location(R.drawable.cinemas_byl, "Zhukova mikrorayon, 38", "+7 (929) 002-20-09", "Every Day", "09:00 – 00:00")); locations.add(new Location(R.drawable.cinemas_charly, "Ol'minskogo mikrorayon, 17", "+7 (472) 523-34-56", "Every Day", "09:30 – 02:00")); locations.add(new Location(R.drawable.cinemas_cinema_5, "Molodezhnyy Proyezd, 10", "+7 (472) 523-37-27", "Every Day", "09:00 – 00:00")); // Create an {#link LocationAdapter}, whose data source is a list of // {#link Locations}. The adapter knows how to create list item views // for each item in the list. LocationAdapter locationAdapter = new LocationAdapter(this, locations); // Get a reference to the ListView, and attach the adapter to the listView. ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(locationAdapter); } }
Probably you set wrong layout in your CinemasActivity. setContentView(R.layout.location_category_list_item); R.layout.location_category_list_item is item view for your ListView. There is no ListView in location_category_list_item which causes the crash
Set TextView text with a for loop so multiple lines show
So I have a for loop that I want to output a new line in a textview every single time it goes through. However, I am not sure if that is possible. I have TableLayout in there but it put the items vertically instead of horizontally. I thought about ListView but not sure how to set the text of it. Here is my code import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route; import org.joda.time.DateTime; import org.w3c.dom.Text; import java.io.IOException; import java.util.Map; import okhttp3.Request; import okhttp3.Response; /** * Created by James Singleton on 8/13/2016. */ public class WeeklyDrives extends Fragment implements APIRequestsUtil.APIRequestResponseListener { View myView; Map<String, Route> drives; private TextView driveNumber; private TextView driveDistance; private TextView driveTime; private TextView driveNumList; private TextView driveDistList; private TextView driveTimeList; #Nullable #Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { myView = inflater.inflate(R.layout.weekly_drives, container, false); APIRequestsUtil.setOnNetWorkListener(this); return myView; } private void populateView() { this.getActivity().runOnUiThread(new Runnable() { #Override public void run() { drives = APIRequestsUtil.getRoutes(); driveNumber = (TextView) myView.findViewById(R.id.Drive_Number); driveDistance = (TextView) myView.findViewById(R.id.Drive_Distance); driveTime = (TextView) myView.findViewById(R.id.Drive_Time); driveNumber.setText("Drive Num."); driveDistance.setText("Distance"); driveTime.setText("Time"); int driveNum = 0; for (Map.Entry drive : drives.entrySet()) { TableRow tr = new TableRow(getActivity()); Route route = (Route) drive.getValue(); tr.setId(driveNum++); //tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); DateTime startTime = new DateTime(route.getStart_time()); DateTime endTime = new DateTime(route.getEnd_time()); driveNumList = (TextView) myView.findViewById(R.id.Drive_Number_List); driveDistList = (TextView) myView.findViewById(R.id.Drive_Distance_List); driveTimeList = (TextView) myView.findViewById(R.id.Drive_Time_List); driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator")); driveDistList.setText(driveDistList.getText().toString() + Float.parseFloat(route.getLen()) / 1000 + " km" + System.getProperty("line.separator")); driveTimeList.setText(driveTimeList.getText().toString() + ((endTime.getMillis() - startTime.getMillis())/ 1000)/60 + " min" + System.getProperty("line.separator")); } } }); } #Override public void onFailure(Request request, Throwable throwable) { } #Override public void onResponse(Response response) { populateView(); } } Here is my XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--Header aligned to top --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/header" android:gravity="center" android:background="#FC9"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Number" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Distance" android:layout_toRightOf="#+id/Drive_Number" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Time" android:layout_toRightOf="#+id/Drive_Distance" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#000"/> </RelativeLayout> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" android:background="#005" android:layout_below="#+id/header" android:id="#+id/scrollableContents"> <!--<TableLayout--> <!--android:id="#+id/fragment1_tlayout"--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="wrap_content"--> <!--android:stretchColumns="0,1">--> <!--</TableLayout>--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Number_List" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#CCCCCC" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Distance_List" android:layout_toRightOf="#+id/Drive_Number_List" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#CCCCCC"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Time_List" android:layout_toRightOf="#+id/Drive_Distance_List" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#CCCCCC"/> </LinearLayout> </ScrollView> </RelativeLayout> drive.entrySet() output What is this:[2016-07-11--08-52-18=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#12a862, 2016-07-11--09-37-46=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#50bbdf3, 2016-07-11--18-54-22=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#6d21ab0, 2016-07-12--09-15-59=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#fb72d29, 2016-07-12--09-29-29=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#776e8ae, 2016-07-12--09-33-03=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#d1a464f, 2016-07-12--09-38-56=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#5b631dc, 2016-07-12--09-41-08=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#dec72e5, 2016-07-12--09-42-39=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#acce1ba, 2016-07-12--09-44-33=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#5def86b, 2016-07-12--09-49-31=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#d628fc8, 2016-07-12--09-54-06=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#7765861, 2016-07-12--19-04-34=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#8575f86, 2016-07-12--19-39-20=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#3f2b047, 2016-07-12--19-40-27=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#df4e074, 2016-07-12--19-41-28=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#b5f199d, 2016-07-13--08-45-17=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#db9ee12, 2016-07-13--09-01-32=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#2a009e3, 2016-07-13--15-02-04=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#9b98fe0, 2016-07-14--08-46-22=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#518b299, 2016-07-14--19-22-46=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#d44d95e, 2016-07-14--19-34-02=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#d3f613f, 2016-07-14--20-16-47=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#957ca0c, 2016-07-15--08-36-28=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#b38df55, 2016-07-15--09-52-32=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#c012d6a, 2016-07-15--12-09-57=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#252d25b, 2016-07-15--12-15-07=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#6bd7af8, 2016-07-15--15-36-10=com.example.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route#df51bd1,
Add android:inputType="textMultiLine" to the TextView in the XML. And add data into TextView like this: driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator")); And so on. Edit: you can use "\n" instead of System.getProperty("line.separator") for adding the new line character.
I would recommend a ListView for this type of thing so that it is automatically scrollable, uses View recycling to maintain efficiency, cleans up your code and organizes it into digestible segments. To use a ListView, you first need to add it to your main XML file. So that would become the following: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--Header aligned to top --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/header" android:gravity="center" android:background="#FC9"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Number" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Distance" android:layout_toRightOf="#+id/Drive_Number" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Drive_Time" android:layout_toRightOf="#+id/Drive_Distance" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#000"/> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" android:background="#005" android:layout_below="#+id/header" android:id="#+id/scrollableContents"> <ListView android:id="#+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout> </RelativeLayout> Following that, you need to create your ListView adapter class. It is what handles what each individual list item's data is (the thing inside your for-loop). Here is one that you might use, although you may have to make your own tweaks and import more of your own classes, add your package name to the top, etc. So create a new Java class called DriveListAdapter.java and place the following code into it: import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.List; import java.util.HashMap; import java.util.Map; public class DriveListAdapter extends ArrayAdapter<Map.Entry> { private final Activity context; // you may need to change List to something else (whatever is returned from drives.entrySet()) private final List<Map.Entry> drives; // may also need to change List here (above comment) public DriveListAdapter(Activity context, List<Map.Entry> drives) { super(context, R.layout.drive_list_item, drives); this.context = context; this.drives = drives; } #Override public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.drive_list_item, null, true); Map.Entry drive = this.drives.get(position); // position is the index of the drives.entrySet() array int driveNum = position + 1; // need to import your Route class Route route = (Route) drive.getValue(); // need to import your DateTime class DateTime startTime = new DateTime(route.getStart_time()); DateTime endTime = new DateTime(route.getEnd_time()); TextView driveNumList = (TextView) rowView.findViewById(R.id.Drive_Number_List); TextView driveDistList = (TextView) rowView.findViewById(R.id.Drive_Distance_List); TextView driveTimeList = (TextView) rowView.findViewById(R.id.Drive_Time_List); driveNumList.setText(String.valueOf(driveNum)); driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km"); driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s"); return rowView; } } Then, create a new layout file, which will be what the individual list item looks like. This layout is rendered for each and every drive in the list. You can adjust this layout file so that everything is aligned properly. I gave an example of how to do that (create a layout file called drive_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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#CCCCCC" android:text="Medium Text" android:id="#+id/Drive_Number_List" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#CCCCCC" android:text="Medium Text" android:id="#+id/Drive_Distance_List" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:textSize="20sp" android:textColor="#CCCCCC" android:text="Medium Text" android:id="#+id/Drive_Time_List" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> And lastly, you just need to populate that ListView you created in the first step with this new adapter class. So use the following method (populates the ListView instead of using the for loop): private void populateView() { this.getActivity().runOnUiThread(new Runnable() { #Override public void run() { drives = APIRequestsUtil.getRoutes(); driveNumber = (TextView) myView.findViewById(R.id.Drive_Number); driveDistance = (TextView) myView.findViewById(R.id.Drive_Distance); driveTime = (TextView) myView.findViewById(R.id.Drive_Time); driveNumber.setText("Drive Number"); driveDistance.setText("Drive Distance"); driveTime.setText("Drive Time"); // edit: you need to generate your List data from the entrySet // the ArrayAdapter cannot take a Set argument - needs to be a List List<Map.Entry> list = new ArrayList<Map.Entry>(); for (Map.Entry drive : drives.entrySet()) { list.add(drive); } // populate the ListView // may need to change "getActivity()" to something else // this constructor needs the "this" context of the activity DriveListAdapter drivesListAdapter = new DriveListAdapter(getActivity(), list); ListView listView = (ListView)findViewById(R.id.listView); listView.setAdapter(drivesListAdapter); } }); }
first of all you use LinearLayout instead of TableLayoyt <LinearLayout android:id="#+id/llMainLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> try this code LinearLayout llMain=(LinearLayout) myView.findViewById(R.id.llMainLayout); for (Map.Entry drive : drives.entrySet()) { LinearLayout tr = new LinearLayout(getActivity()); tr.setOrientation(1); // horizontal Route route = (Route) drive.getValue(); tr.setId(driveNum++); // set params DateTime startTime = new DateTime(route.getStart_time()); DateTime endTime = new DateTime(route.getEnd_time()); TextView driveNumList = new TextView(); TextView driveDistList = new TextView(); TextView driveTimeList = new TextView(); driveNumList.setText(String.valueOf(driveNum)); driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km"); driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s"); tr.addView(driveNumList); tr.addView(driveDistList); tr.addView(driveTimeList); llMain.addView(tr); } }
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.
Customizing Android ListView with Custom Adapter
I'm working on a List View with Custom Adapter but it seems that my code isn't working. I don't know what is wrong. Can someone help me. Here's the code that I did: student_record.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_above="#+id/footerlayout" android:id="#+id/listviewlayout"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top|start" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingStart="10dp" android:paddingRight="10dp" android:paddingEnd="10dp" android:background="#drawable/header" android:text="#string/student_record" android:textSize="15sp" android:textColor="#ffffff" /> <ListView android:id="#+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent" > </ListView> </LinearLayout> <LinearLayout android:id="#+id/footerlayout" android:layout_marginTop="3dip" android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent" android:gravity="center" android:layout_alignParentBottom="true"> <Button android:id="#+id/tableOfSpecificationButton" android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:background="#drawable/roundedbutton" android:text="#string/table_of_specifications" android:textColor="#ffffff" android:textSize="15sp" /> <Button android:id="#+id/itemAnalysisButton" android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:background="#drawable/roundedbutton" android:text="#string/item_analysis" android:textColor="#ffffff" android:textSize="15sp" /> </LinearLayout> </RelativeLayout> student_row.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/studentTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:paddingBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:text="#string/hello_world" android:textSize="17sp" android:textStyle="bold" /> <TextView android:id="#+id/scoreTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:layout_marginEnd="15dp" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignBaseline="#+id/studentTextView" android:text="#string/hello_world" android:textSize="17sp" android:textStyle="bold" /> </RelativeLayout> StudentRecord.java package com.checkaidev1; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class StudentRecord extends Activity { private ListView listView1; String[] score = new String[] { "10", "45", "34", "28", "45", "30", "19", "33" }; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.student_record); Student student_data[] = new Student[] { new Student("Ace"), new Student("Brian"), new Student("Cathy"), new Student("Dave"), new Student("Ethel") }; StudentRecordCustomAdapter adapter = new StudentRecordCustomAdapter(this, R.layout.student_row, student_data, score); listView1 = (ListView)findViewById(R.id.listView1); listView1.setAdapter(adapter); } /** * #param args */ public static void main(String[] args) { // TODO Auto-generated method stub } } StudentRecordCustomAdapter.java package com.checkaidev1; 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; public class StudentRecordCustomAdapter extends ArrayAdapter<Student> { Context ctx; int layoutResourceId; Student data[] = null; String[] score; LayoutInflater inflater; public StudentRecordCustomAdapter(Context context, int layoutResourceId, Student data[], String[] score) { super(context, layoutResourceId, data); // TODO Auto-generated constructor stub this.ctx = context; this.layoutResourceId = layoutResourceId; this.data = data; this.score = score; } public View getView(int position, View convertView, ViewGroup parent){ View row = convertView; ViewHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)ctx).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ViewHolder(); holder.studentTextView = (TextView) convertView.findViewById(R.id.studentTextView); holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView); row.setTag(holder); } else { holder = (ViewHolder)row.getTag(); } Student student = data[position]; holder.studentTextView.setText(student.name); holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView); return row; } static class ViewHolder { TextView studentTextView; TextView scoreTextView; } } Thank you!
First of all you don't have model class named Student Even though you are declaring them as Student. Replace them with String. Write in this way String student_data[] = new String[] { new String("Ace"), new String("Brian"), new String("Cathy"), new String("Dave"), new String("Ethel") }; StudentRecordCustomAdapter adapter = new StudentRecordCustomAdapter(StudentRecord.this,R.layout.student_row,student_data,score); And Recieve them like this public StudentRecordCustomAdapter(Context context, int layoutResourceId, String data[], String[] score) { super(context, layoutResourceId, data); // TODO Auto-generated constructor stub this.ctx = context; this.layoutResourceId = layoutResourceId; this.data = data; this.score = score; } This is wrong.. String student = data[position]; You can access and set your variable To textView Directly.. Like holder.studentTextView.setText(data[position]); Review your code. Change them And try again what you want to do...
I suggest to always extend BaseAdapter for every single custom Adapter that you need to use in your App. Your code seems fine.
You repeat this line twice holder.scoreTextView = (TextView) convertView.findViewById(R.id.scoreTextView); second tine you must do holder.scoreTextView.setText(data[position])
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.