Customizing Android ListView with Custom Adapter - java

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])

Related

How to add multiple values inside 1 list view and style it approriatle

I want to be able to add multiple values on a list but properly style the list view.
For example right now the list view will look like this:
namepostcodedate
which is because of the following code
ListArray.add(jobs.get(finalJ).customer.getName() + job.getPostcode() + job.getDate());
The way that I am currently adding the values in the ListArray doesn't seem ideal but I am not sure if there is another way to do this and display the list formated?
This is my list_item file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="#+id/rows2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ddOrangeLight"
android:showDividers="middle">
<!-- android:divider="?android:attr/dividerHorizontal"-->
<TextView
android:id="#+id/customerNameView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="4sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="4sp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</RelativeLayout>
My adapter class
public class SearchableAdapter extends BaseAdapter implements Filterable {
private List<String>originalData = null;
private List<String>filteredData = null;
private final LayoutInflater mInflater;
private final ItemFilter mFilter = new ItemFilter();
public SearchableAdapter(Context context, List<String> data) {
this.filteredData = data ;
this.originalData = data ;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unnecessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.uName = (TextView) convertView.findViewById(R.id.customerNameView);
// holder.uPostCode = (TextView) convertView.findViewById(R.id.postCode);
// holder.UDateTime = (TextView) convertView.findViewById(R.id.dateTimeView);
// Bind the data efficiently with the holder.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// If weren't re-ordering this you could rely on what you set last time
// holder.text.setText(filteredData.get(position));
holder.uName.setText(filteredData.get(position));
// holder.uPostCode.setText(filteredData.get(position));
// holder.UDateTime.setText(filteredData.get(position));
return convertView;
}
static class ViewHolder {
TextView uName;
// TextView uPostCode;
// TextView UDateTime;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
Fragment class
public class CompletedJobsFragment extends Fragment {
AppActivity a;
String search;
TableLayout tableLayout;
Vehicle vehicle;
ListView listView;
SearchableAdapter arrayAdapter;
EditText searchInput;
List<String> ListArray = new ArrayList<String>();
ArrayList<ListItem> results = new ArrayList<>();
ListItem repairDetails = new ListItem();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_completed_jobs,container,false);
a = (AppActivity) getActivity();
assert a != null;
tableLayout = (TableLayout) v.findViewById(R.id.completedJobsTable);
Button clear = v.findViewById(R.id.btnClearTextCarReg);
searchInput = v.findViewById(R.id.txtEditSearchCarReg);
listView = v.findViewById(R.id.list__View);
search = searchInput.getText().toString().trim();
clear.setOnClickListener(av -> searchInput.setText(""));
// Button searchButton = v.findViewById(R.id.btnSearchVehicleReg);
arrayAdapter = new SearchableAdapter(getContext(),ListArray);
listView.setAdapter(arrayAdapter);
listView.setClickable(true);
searchInput.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
arrayAdapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
JobRepository jobRepository = new JobRepository(a.getApplication());
VehicleRepository vehicleRepository = new VehicleRepository(a.getApplication());
jobRepository.findCompleted().observe(getViewLifecycleOwner(), jobs -> {
for (int j = 0; j < jobs.size(); j++) {
if (jobs.get(j).job == null) {
continue;
}
int finalJ = j;
vehicleRepository.findByJob(jobs.get(j).job.getUuid()).observe(getViewLifecycleOwner(), vehicles -> {
for (int vh = 0; vh < vehicles.size(); vh++) {
if (vehicles.get(vh).vehicle == null) {
continue;
}
vehicle = vehicles.get(vh).vehicle;
Job job = jobs.get(finalJ).job;
ListArray.add(jobs.get(finalJ).customer.getName() + job.getPostcode() + job.getDate());
View viewToAdd = arrayAdapter.getView(finalJ, null, null);
TableRow[] tableRows = new TableRow[jobs.size()];
tableRows[finalJ] = new TableRow(a);
tableRows[finalJ].setId(finalJ + 1);
tableRows[finalJ].setPadding(0, 20, 0, 20);
tableRows[finalJ].setBackgroundResource(android.R.drawable.list_selector_background);
tableRows[finalJ].setBackgroundResource(R.drawable.table_outline);
tableRows[finalJ].setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT
));
tableRows[finalJ].addView(viewToAdd);
tableLayout.addView(tableRows[finalJ], new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT
));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text or do whatever you need.
// Toast.makeText(getContext(), "asd", Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString(JOB_ID, job.getUuid().toString());
bundle.putString(CUSTOMER_NAME, jobs.get(finalJ).customer.getName());
// bundle.putString(JOB_DATE, sdf.format(job.getDate()));
Fragment fragment = new ViewCustomerInformationFragment();
fragment.setArguments(bundle);
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
}
});
}
});
return v;
}
Fragment xml file
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imageViewBGhm"
android:scaleType="center"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:id="#+id/txtTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="180dp"
android:text="#string/completed_jobs"
android:textColor="#color/ddGrey"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.375"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/txtEditSearchCarReg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle2"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:ems="12"
android:inputType="textPersonName"
android:paddingStart="5dp"
android:paddingEnd="10dp"
android:paddingBottom="22dp"
android:textAlignment="textStart"
android:textSize="18sp" />
<TableRow
android:id="#+id/tableTitleRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtEditSearchCarReg"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="6dp"
android:background="#color/lightGrey"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle"
android:visibility="visible">
<TextView
android:id="#+id/txtCustomerNameTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="4sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="4sp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/customer_name_hc"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtPostcodeTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/postcode_placeholder"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtDateTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/date"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtTimeTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginEnd="20sp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/enquiry_vat_value"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<ListView
android:id="#+id/list__View"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/tableTitleRow"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:visibility="visible" />
<TableLayout
android:id="#+id/completedJobsTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtEditSearchCarReg"
android:layout_marginStart="2dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:stretchColumns="0,1,2"
android:visibility="gone">
</TableLayout>
<Button
android:id="#+id/btnClearTextCarReg"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignTop="#+id/txtEditSearchCarReg"
android:layout_alignParentEnd="true"
android:layout_marginStart="175dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="20dp"
android:background="#drawable/ic_outline_cancel_24"
android:backgroundTint="#color/ddOrange"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground" />
<Button
android:id="#+id/btnSearchVehicleReg"
style="#style/DDButtons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/txtEditSearchCarReg"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="14dp"
android:background="#drawable/custom_buttons"
android:drawableStart="#drawable/ic_outline_search_24"
android:drawablePadding="12dp"
android:letterSpacing="0.2"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:singleLine="true"
android:text="#string/search_postcode"
android:textAlignment="textStart"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"/>
</RelativeLayout>

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 - Remove extra space of listview header image

I have a listview with a header image , there is extra space top and bottom of the image . I want to remove the space from it ,
My layout is LinierLayout if anyone knows please help ?
my xml files is down
this is the code for list items xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:textColor="#android:color/white"
android:id="#+id/tvHeader"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvHeader"
android:orientation="horizontal"
android:paddingLeft="10dp">
<ImageView
android:id="#+id/imageView"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="0dp"
android:layout_marginTop="10dp"
android:paddingBottom="0dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="10dp"/>
<!-- img -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginTop="30dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/fname"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_weight="1"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lname"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/organizationname"
android:textSize="10sp"
android:paddingTop="10px" />
<TextView
android:text="yyyyyyyyyyyyyyyyy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/idposition"
android:textSize="10sp"
android:layout_below="#+id/fname"
android:layout_alignLeft="#+id/fname"
android:layout_alignStart="#+id/fname" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
this is adapter file
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.parse.*;
import com.squareup.picasso.*;
import org.w3c.dom.Text;
import java.io.*;
import java.net.*;
import java.util.*;
public class IndividualsAdaptor extends ArrayAdapter {
private static final int TYPE_SECTION_HEADER = 0;
private static final int TYPE_LIST_ITEM = 1;
protected Context mContext;
ArrayList<Integer> mListHeader = new ArrayList<>();
// Code for Custom Filter.
protected List mBackupList = new ArrayList();
public IndividualsAdaptor(Context context, List status) {
super(context, R.layout.t3, status);
mContext = context;
// Code for Custom Filter.
mBackupList.addAll(status);
}
#Override
public int getViewTypeCount() { return 2; }
#Override
public int getItemViewType(int position)
{
if (mListHeader.contains(position)){
return TYPE_SECTION_HEADER;
} else {
return TYPE_LIST_ITEM;
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.t3, null);
holder = new ViewHolder();
holder.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader);
holder.usernameHomepage = (TextView) convertView.findViewById(R.id.fname);
holder.statusHomepage = (TextView) convertView.findViewById(R.id.lname);
holder.pposition = (TextView) convertView.findViewById(R.id.idposition);
holder.orgName = (TextView) convertView.findViewById(R.id.organizationname);
holder.logo = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject statusObject = (ParseObject) getItem(position);
// title
String username = statusObject.getString("firstname");
holder.usernameHomepage.setText(username);
// content
String status = statusObject.getString("lastname");
holder.statusHomepage.setText(status);
// Header
if(getItemViewType(position) == TYPE_SECTION_HEADER){
holder.tvHeader.setVisibility(View.VISIBLE);
holder.tvHeader.setText(String.valueOf(status.charAt(0)));
}else{
holder.tvHeader.setVisibility(View.GONE);
}
// content
String positions = statusObject.getString("position");
holder.pposition.setText(positions);
// content
String org = statusObject.getString("organizationName");
holder.orgName.setText(org);
// logo
URL url = null;
Bitmap bmp = null;
try {
url = new URL("file location" + statusObject.getString("image"));
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
}catch (IOException e) {
}
holder.logo.setImageBitmap(bmp);
Picasso.with(mContext)
.load(String.valueOf(url))
.transform(new Rounded( ))
.into(((ImageView) convertView
.findViewById(R.id.imageView)));
return convertView;
}
public static class ViewHolder {
TextView tvHeader;
TextView usernameHomepage;
TextView statusHomepage;
TextView orgName;
TextView pposition;
ImageView logo;
}
#Override
public Filter getFilter() {return new Filter(){
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String queryString = charSequence.toString().toLowerCase();
List<ParseObject> filteredList = new ArrayList<>();
ParseObject tmpItem;
String tmpUsername, tmpStatus, tmpPositions, tmpOrg;
for(int i=0; i<mBackupList.size(); i++){
tmpItem = (ParseObject) mBackupList.get(i);
tmpUsername = tmpItem.getString("firstname").toLowerCase();
tmpStatus = tmpItem.getString("lastname").toLowerCase();
tmpPositions = tmpItem.getString("position").toLowerCase();
tmpOrg = tmpItem.getString("organizationName").toLowerCase();
// The matching condition
if(tmpUsername.contains(queryString)||tmpStatus.contains(queryString)||
tmpPositions.contains(queryString)||tmpOrg.contains(queryString)){
filteredList.add(tmpItem);
}
}
FilterResults filterResults = new FilterResults();
filterResults.count = filteredList.size();
filterResults.values = filteredList;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
clear();
addAll((List<ParseObject>) filterResults.values);
}
};}
public void updateBackupList(List newList){
mBackupList.clear();
mBackupList.addAll(newList);
}
public void updateHeaderList(ArrayList<HashMap> newHeaderList){
for(int i=0; i<newHeaderList.size(); i++){
mListHeader.add(Integer.parseInt((String)newHeaderList.get(i).get("position")));
}
Log.d("Test", mListHeader.toString());
}
}
java file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="110dp"
/>
<TextView android:id="#+id/tvEmpty"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#083266"/>
<SearchView
android:id="#+id/ser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search.."
android:background="#color/FBC_RED"
android:layout_below="#id/tvEmpty"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="0dp">
</SearchView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_below="#+id/ser1">
<ListView
android:id="#id/android:list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.89" />
<ListView
android:id="#+id/listIndex"
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="none"
android:layout_weight="0.08">
</ListView>
</LinearLayout>
</LinearLayout>
This should work:
ListView listView = getListView();
ImageView mListHeader = new ImageView(getContext());
mListHeader.setImageResource(R.drawable.individuals_img);
mListHeader.setScaleType(ImageView.ScaleType.FIT_XY);
mListHeader.setLayoutParams(new AbsListView.LayoutParams(1400,974));
mListHeader.requestLayout();
listView.addHeaderView(mListHeader);
Inflate a custom layout as your listview header and add below imageview to it
code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.list);
View header = getLayoutInflater().inflate(R.layout.header, list, false); //custom layout
list.addHeaderView(header, null, false);
}
try this: in your header.xml add the header image
<ImageView
android:id="#+id/imageView"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:src="your_source"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:paddingBottom="0dp"
android:scaleType="fitXY" />

Android : Creating a vertical space for ImageView and tab

I am working on an Android project in which I am creating UI for editing a Note object in the app. For that, I will be replicating the UI which is designed with JS,HTML,CSS. Unfortunately, android has discrete elements like ListView, TextView and all. As you can see in the screenshot, the top horizontal bar is just a representation and inside it contains a photo.
Also, there is a tab like option in the activity itself. I don't know how to create both these things. The choice of colour I am planning to create with a List and get the element clicked in the List. I have basic functionality working, and working on UI part now.
I don't know what all is required for creating such fancy UI's. Any help would be nice.
edit_note.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/noteTagEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal" />
<EditText
android:id="#+id/noteTextEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.97"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/noteDate"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/noteNumber"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/attachCount"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<GridView
android:id="#+id/attachmentDisplay"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_gravity="center_horizontal"
android:numColumns="2"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
>
<Button
android:id="#+id/saveNoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="#string/saveNote" />
<Button
android:id="#+id/cancelEditButton"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
/>
</RelativeLayout>
</LinearLayout>
Java code :
public class EditNoteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_note);
}
public class getNoteByItsId extends AsyncTask<Integer, Void, ResponseEntity<RestNote>> {
#Override
protected void onPostExecute(ResponseEntity<RestNote> params) {
restNote = params.getBody();
restAttachmentList = restNote.getNotesAttachments();
final ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<>();
for (RestAttachment restAttachment : restAttachmentList) {
HashMap<String, String> restAttachmentDisplay = new HashMap<>();
restAttachmentDisplay.put(fileName, restAttachment.getFileName());
restAttachmentDisplay.put(fileSize, readableFileSize(restAttachment.getFileSize()));
restSectionArrayList.add(restAttachmentDisplay);
}
attachmentsGridView = (GridView) findViewById(R.id.attachmentDisplay);
noteDate = (TextView) findViewById(R.id.noteDate);
noteDate.setText(restNote.getNoteDate());
noteNumber = (TextView) findViewById(R.id.noteNumber);
String noteNos = "#" + String.valueOf(restNote.getNoteNumber());
noteNumber.setText(noteNos);
attachCount = (TextView) findViewById(R.id.attachCount);
attachCount.setText(String.valueOf(restNote.getAttachCount()));
editNoteAttachments = new EditNoteAttachments(editNoteActivity, restSectionArrayList);
attachmentsGridView.setAdapter(editNoteAttachments);
}
public class EditNoteAttachments extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public EditNoteAttachments(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.attachment_rows, parent, false);
TextView fileName = (TextView) vi.findViewById(R.id.fileName);
TextView fileSize = (TextView) vi.findViewById(R.id.fileSize);
HashMap<String, String> conversationList;
conversationList = data.get(position);
fileName.setText(conversationList.get(EditNoteActivity.fileName));
fileSize.setText(conversationList.get(EditNoteActivity.fileSize));
return vi;
}
}
Kindly let me know. Thank you.
For the top horizontal bar you definitely need a compound custom view like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="#+id/background
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/profileImageView"
android:layout_marginLeft="16dp"
/>
<ImageView
android:id="#+id/profileImageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
/>
</merge>
With code like this:
public class TopBarView extends RelativeLayout {
private TextView title;
private ImageView profileImage;
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TopBarView, 0, 0);
String titleText = a.getString(R.styleable.TopBarView_titleText);
a.recycle();
setGravity(Gravity.CENTER_VERTICAL);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_top_bar, this, true);
title = (TextView) findViewById(R.id.title);
title.setText(titleText);
profileImage = (ImageView) findViewById(R.id.profileImageView);
profileImage.setBackgroundColor(R.color.yellow);
profileImage.setImageResource(R.drawable.myProfileDrawable);
setBackgroundColor(Color.TRANSPARENT);
}
For tab options you could use android.support.design.widget.TabLayout from design support library.

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