Android Custom Adapter not displaying any data in Activity? - java

I have created a custom adapter for my list (JobAdapter), however, whenever I set the adapter, even if there are values (jobs arraylist parameter has a size of 2), nothing is displayed on my activity.
This is my adapter:
public class JobAdapter extends ArrayAdapter<Job> {
private final Context context;
private final ArrayList<Job> jobs;
private final int layoutResourceId;
public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> jobs) {
super(context, layoutResourceId, jobs);
this.context = context;
this.jobs = jobs;
this.layoutResourceId = layoutResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService((Activity.LAYOUT_INFLATER_SERVICE));
if (view == null) {
view = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.title = (TextView)view.findViewById(R.id.tv_jobTitle);
holder.payrate = (TextView)view.findViewById(R.id.tv_payRate);
holder.startdate = (TextView)view.findViewById(R.id.tv_startDate);
holder.workinghrs = (TextView)view.findViewById(R.id.tv_duration);
holder.location = (TextView)view.findViewById(R.id.tv_location);
holder.companyname = (TextView)view.findViewById(R.id.tv_companyName);
holder.description = (TextView)view.findViewById(R.id.tv_JobDesc);
holder.experience = (TextView)view.findViewById(R.id.tv_experienceReq);
holder.equipment = (TextView)view.findViewById(R.id.tv_equipmentReq);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
Job j = jobs.get(0);
holder.title.setText(j.getJobTitle());
holder.payrate.setText(j.getPayrate());
holder.startdate.setText(j.getDurationStart());
holder.workinghrs.setText(j.getWorkingHrs());
holder.location.setText(j.getLocation());
holder.companyname.setText("ABC Company");
holder.description.setText(j.getDescription());
holder.experience.setText("3-5 years");
holder.equipment.setText("Hardhat");
//TODO: add hardhat, boots and other field to objects mapped
return view;
}
static class ViewHolder
{
TextView title;
TextView payrate;
TextView startdate;
TextView workinghrs;
TextView location;
TextView companyname;
TextView description;
TextView experience;
TextView equipment;
}
}
This is how I am setting the adapter in the AsyncTask OnPostExecute:
protected void onPostExecute(ArrayList<Job> result) {
arrayAdapter = new JobAdapter(getApplicationContext(), R.layout.jobcard, result);
arrayAdapter.notifyDataSetChanged();
flingContainer.setAdapter(arrayAdapter);
}
What could be the problem with this? There is no error message in Logcat and the application still runs fine, just no data shown. Data should be shown in the jobcard.xml layout which is defined for the JobAdapter.
Thanks in advance for any help with this!
EDIT:
It looks like getView() in the JobAdapter is not being called, I have tried to override the getCount() method in the JobAdapter to return 1 just so I can see one element but that did not work either.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="415dp"
android:padding="5dp"
android:layout_marginTop="20dp">
<LinearLayout
android:id="#+id/outerMainCardLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:background="#drawable/swipecard_shadow"
android:gravity="top"
android:layout_marginLeft="5dp">
<LinearLayout
android:id="#+id/mainCardLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:weightSum="1"
android:background="#FFFFFF"
android:gravity="top">
<TextView
android:id="#+id/tv_jobTitle"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#505353"
android:textAlignment="center"
tools:text="Cement Pouring Guy"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/tv_companyName"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#505353"
android:textAlignment="center"
tools:text="ABC Company"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="#+id/cardHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:id="#+id/tv_experienceReq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginLeft="10dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_experience"
tools:text="3-5 years" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_location"
tools:text="Langley, BC" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/tv_payRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginRight="10dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_payrate"
tools:text="125/day" />
</LinearLayout>
<TextView
android:id="#+id/tv_JobDesc"
android:textSize="14sp"
android:textColor="#505353"
android:background="#e6e7e8"
tools:text="Pour Cement, Mix Cement, Level Cement and go pick up cement bags."
android:layout_width="match_parent"
android:layout_height="150dp"
android:padding="15dp" />
<LinearLayout
android:id="#+id/additionalInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:id="#+id/tv_startDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:layout_marginLeft="10dp"
android:drawableTop="#drawable/ic_date"
tools:text="June 1, 2016" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_clock"
tools:text=" 8h" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tv_equipmentReq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:layout_marginRight="10dp"
android:drawableTop="#drawable/ic_hardhat"
tools:text="Hardhat" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="20dp"
android:background="#FFFFFF" />
<View
android:id="#+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="20dp"
android:layout_gravity="right"
android:background="#FFFFFF" />

Related

How to set Margins for View in android studio?

I have a view in an activity as below .I want to set Margin to the view.But i used Viewgroup.LayoutParams but it doesn't change the view of the row.Below i have given custom adapter which takes data from firstpagerowitems.xml.I have given the java code for adapter .I tried doing android:layout_marginbottom="10dp" for firstpagerowitems.xml and firstpage.xml but it doesnt work.
class MyAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> rTitle;
ArrayList<String> rDescription;
ArrayList<String> rImgs;
MyAdapter (Context c, ArrayList<String> title,ArrayList<String> description,ArrayList<String> imgs) {
super(c, R.layout.firstpagerowitems, R.id.textView1, title);
this.context = c;
this.rTitle = title;
this.rDescription = description;
this.rImgs = imgs;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
TextView images = row.findViewById(R.id.textView3);
TextView myTitle = row.findViewById(R.id.textView1);
TextView myDescription = row.findViewById(R.id.textView2);
Log.d("entered last","yes");
Log.d("postion", String.valueOf(position));
Log.d("rimgs", String.valueOf(rImgs));
Log.d("desc", String.valueOf(rDescription));
Log.d("title", String.valueOf(rTitle));
// now set our resources on views
images.setText(rImgs.get(position));
myTitle.setText(rTitle.get(position));
myDescription.setText(rDescription.get(position));
Random random=new Random();
int trp=random.nextInt(16);
Log.d("enteredcolor",mycolors[trp]);
// images.setBackgroundResource(R.color.lightgreen);
String fd=mycolors[trp];
// images.setBackgroundColor(Color.parseColor(fd));
// LinearLayout mylinear=row.findViewById(R.id.mylinear);
// mylinear.setBackgroundColor(Color.parseColor(fd));
row.setBackgroundColor(Color.parseColor(fd));
// ViewGroup.MarginLayoutParams margins=new ViewGroup.MarginLayoutParams(row.getLayoutParams());
// margins.setMargins(0,100,0,100);
// ViewGroup.LayoutParams layouts=new ViewGroup.LayoutParams(margins);
// row.setLayoutParams(layouts);
// ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(row);
return row;
}
}
code.java
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="one"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
firsypagerowitems.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
tools:context=".Firstpage">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/mylinear">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
>
</ListView>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/fabs"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:fabSize="normal"
app:backgroundTint="#color/design_default_color_error"
app:elevation="6dp"
android:src="#android:drawable/ic_input_add"
>
</com.google.android.material.floatingactionbutton.FloatingActionButton>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
firstpage.xml
Try whether this helps...
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_marginBottom="5dp"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
Hope this helps. Feel free to ask for clarifications...

Some Recyclerview Items Affect Each Other

I have recyclerview with many items. And this is a screenshot of my application, showing the first item of my recyclerview.
pict of my page
but when I change the choice of the fifth item, the choice of the first item is changing too. Any idea why this is happening? And what should I do?
this is my adapter:
public class SkoringAdapter extends RecyclerView.Adapter<SkoringAdapter.SkoringViewHolder>{
private List<Skoring> skoringList;
private Context context;
public SkoringAdapter(final Context context) {
this.skoringList = new ArrayList<>();
this.context = context;
}
public void setSkoringList(List<Skoring> skoringList){
this.skoringList.clear();
this.skoringList.addAll(skoringList);
notifyDataSetChanged();
}
static class SkoringViewHolder extends RecyclerView.ViewHolder{
Context context;
TextView tvSoal, tvKe, tvTotal, tvProses;
RadioGroup rgPilihan;
RadioButton rb1, rb2, rb3, rb4, rb5, rb6;
SkoringViewHolder(View itemView, final Context context) {
super(itemView);
this.context = context;
tvSoal = itemView.findViewById(R.id.tvSoal);
...etc
}
}
#NonNull
#Override
public SkoringViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_skoring, parent, false);
return new SkoringViewHolder(itemView,context);
}
#Override
public void onBindViewHolder(#NonNull final SkoringViewHolder holder, final int position) {
final Skoring tg = skoringList.get(position);
holder.tvSoal.setText(tg.getSoal());
holder.tvKe.setText(Integer.toString(position + 1));
holder.tvTotal.setText(Integer.toString(skoringList.size()));
holder.rgPilihan.setVisibility(View.GONE);
holder.rb1.setVisibility(View.GONE);
holder.rb2.setVisibility(View.GONE);
holder.rb3.setVisibility(View.GONE);
holder.rb4.setVisibility(View.GONE);
holder.rb5.setVisibility(View.GONE);
holder.rb6.setVisibility(View.GONE);
String[] kunci = tg.getKunci().split("`");
String[] pilihan = new String[kunci.length];
String[] isi = new String[kunci.length];
String[] skor = new String[kunci.length];
for (int i=0; i<kunci.length; i++){
String[] pisah = kunci[i].split("~");
if(pisah.length == 3){
pilihan[i] = pisah[0]; isi[i] = pisah[1]; skor[i] = pisah[2];
}
}
holder.rgPilihan.setVisibility(View.VISIBLE);
if(kunci.length > 0){
holder.rb1.setText(isi[0]);
holder.rb1.setVisibility(View.VISIBLE);
if(kunci.length > 1){
holder.rb2.setText(isi[1]);
holder.rb2.setVisibility(View.VISIBLE);
if(kunci.length > 2){
holder.rb3.setText(isi[2]);
holder.rb3.setVisibility(View.VISIBLE);
if(kunci.length > 3){
holder.rb4.setText(isi[3]);
holder.rb4.setVisibility(View.VISIBLE);
if(kunci.length > 4){
holder.rb5.setText(isi[4]);
holder.rb5.setVisibility(View.VISIBLE);
if(kunci.length > 5){
holder.rb6.setText(isi[5]);
holder.rb6.setVisibility(View.VISIBLE);
}}}}}}}}
And this is my item_skoring.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="16dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="16dp">
<TextView
android:id="#+id/tvSoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:layout_gravity="center"
android:textAlignment="center"
android:textColor="#color/colorBlack"
android:textSize="17sp"
android:layout_marginBottom="16dp"/>
<RadioGroup
android:visibility="visible"
android:id="#+id/rgPilihan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<RadioButton
android:id="#+id/rb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:visibility="gone"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:textSize="16sp"
android:visibility="gone"
android:id="#+id/rb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:textSize="16sp"
android:visibility="gone"
android:id="#+id/rb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:textSize="16sp"
android:visibility="gone"
android:id="#+id/rb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:textSize="16sp"
android:visibility="gone"
android:id="#+id/rb5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:textSize="16sp"
android:visibility="gone"
android:id="#+id/rb6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="#color/colorPrimary"/>
</RadioGroup>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tiTelp"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:hint="No. Telepon"
android:id="#+id/etTelp"
android:drawablePadding="5dp"
android:layout_weight="0.85" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tiHP"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:hint="No. Handphone"
android:id="#+id/etHP"
android:drawablePadding="5dp"
android:layout_weight="0.85"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tiEmail"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:hint="Alamat Email"
android:id="#+id/etEmail"
android:drawablePadding="5dp"
android:layout_weight="0.85"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tiNPWP"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:hint="No. NPWP"
android:id="#+id/etNPWP"
android:drawablePadding="5dp"
android:layout_weight="0.85"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="10dp"
android:background="#color/colorGreyUp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data ke "
android:textStyle="italic"
android:textColor="#color/colorBlack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvKe"
android:textStyle="italic|bold"
android:textColor="#color/colorBlack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" dari "
android:textStyle="italic"
android:textColor="#color/colorBlack"/>
<TextView
android:id="#+id/tvTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic|bold"
android:textColor="#color/colorBlack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" data"
android:textStyle="italic"
android:textColor="#color/colorBlack"/>
</LinearLayout>
<TextView
android:background="#color/colorPrimary"
android:textColor="#color/colorWhite"
android:visibility="gone"
android:padding="14dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SELANJUTNYA"
android:textSize="18sp"
android:clickable="true"
android:id="#+id/bProses"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceSmall"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Solved with adding this on my java class:
MyrecyclerView.setItemViewCacheSize(100);

ExpandableListView children width

I have an ExpandableListView in my app. I want to make the child layout a bit narrower than the group header and also keep it centered. Due to some visual effects i must set layout params from code however i can't seem to find the best solutions for this tried changing layout width from xml with no luck. Any advice will be helpful.
Thanks
adapter getChild() method:
#Override
public View getChildView(final int groupPosition, final int
childPosition,
boolean isLastChild, View convertView,
ViewGroup parent) {
final Item expandedListitem = (Item) getChild(groupPosition, childPosition);
Drawable drawable= ContextCompat.getDrawable(context,R.drawable.background_border);
GradientDrawable gradientDrawable= (GradientDrawable) drawable;
gradientDrawable.setStroke(5,expandedListitem.getColor());
if (Build.VERSION.SDK_INT >= 19) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
if (!isLastChild) {
View divider = convertView.findViewById(R.id.linearfaq);
divider.setVisibility(View.INVISIBLE);
ViewGroup.LayoutParams params = convertView.getLayoutParams();
int width = (MainActivity.display.getWidth());
params.height = 110;
params.width=width-40;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
convertView.setLayoutParams(params);
View padder = convertView.findViewById(R.id.padder);
padder.setVisibility(View.INVISIBLE);
} else {
View divider = convertView.findViewById(R.id.linearfaq);
RelativeLayout.LayoutParams params = new
[![enter image description here][1]][1]RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int width = (MainActivity.display.getWidth());
params.height = 140;
params.width=width-40;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
convertView.setLayoutParams(params);
divider.setVisibility(View.VISIBLE);
View padder = convertView.findViewById(R.id.padder);
padder.setBackgroundColor(expandedListitem.getColor());
padder.setVisibility(View.VISIBLE);
}
}else{
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_low, null);
}
}
Item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/back" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
>
<CheckBox
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusableInTouchMode="false"
android:layout_marginRight="8dp"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_height="wrap_content"
android:id="#+id/checkbox"
/>
<TextView
android:inputType="text"
android:paddingRight="8dp"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_marginTop="6dp"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/checkbox"
android:layout_toEndOf="#+id/checkbox"
android:id="#+id/list_text"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/del"
android:src="#android:drawable/ic_menu_delete"
android:layout_marginTop="3dp"
android:background="#null"
android:visibility="gone"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkbox"
android:layout_alignBottom="#+id/checkbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:orientation="horizontal"
android:id="#+id/relativeLayout">
<Button
android:id="#+id/plus"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="false"
android:text="+" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/plus"
android:layout_toRightOf="#+id/plus" />
<Button
android:id="#+id/minus"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/quantity"
android:layout_toRightOf="#+id/quantity"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="false"
android:text="-" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/linearfaq"
android:layout_marginTop="35dp"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/back"
android:orientation="vertical" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/padder"
android:layout_width="match_parent"
android:background="#color/blue"
android:visibility="gone"
android:layout_height="2dp"
android:layout_alignTop="#+id/linearfaq"
>
</RelativeLayout>
category 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"
android:orientation="horizontal"
android:background="#drawable/parent_border">
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:focusable="false"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cat_text"/>
I'd put all Views in one single ConstraintLayout. By attaching the background to the parent left and right, the View is default centered. By adding margins left and right, you achieve the View being smaller.
Like in this example:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:background="#android:color/darker_gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:buttonTint="#android:color/background_light"
android:text="CheckBox"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:text="Stuff"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/checkBox"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
which looks like this:
the
android:layout_width="0dp"
actually means the view will be stretched to match the constraints. A bit like "match_parent", but within the bounds you set via its constraints.
Play around with the ConstraintLayout a bit. Once you get the hang of it, you will not want to miss it anymore :)

Andrioid listview only shows latest item

I am populating the listview with items from parse.com. It was working fine not long ago and then for some reason once i changed from eclipse to android studio it started only showing the latest item.
Adapter
public class MyCustomAdapter extends ParseQueryAdapter<ParseObject> {
public MyCustomAdapter(Context context) {
super(context, "Comments", R.layout.comments_listview_item);
}
#Override
public View getItemView(ParseObject comment, View view, ViewGroup parent) {
view = View.inflate(getApplicationContext(), R.layout.comments_listview_item, null);
mListViewReferences(view);
loadComments();
commenttext.setText(comment.getString("Comment"));
commentersUserName.setText(comment.getString("Commenter"));
return super.getItemView(comment, view, parent);
}
/**
* Set References
*/
private void mListViewReferences(final View view) {
commentersUserName = (TextView) view.findViewById(R.id.commentersUserName);
commenttext = (TextView) view.findViewById(R.id.commenttext);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:padding="0dp">
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="0dp"
android:background="#drawable/actionmenubackground">
<TextView
android:id="#+id/commenttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="11dp"
android:layout_toRightOf="#+id/comentFullName"
android:text="COMMENTS"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/backButton"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="0dp"
android:background="#null"
android:src="#drawable/back_arrow" />
<TextView
android:id="#+id/comentFullName"
android:layout_width="2dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/backButton"
android:background="#E0E0E0" />
</RelativeLayout>
<EditText
android:id="#+id/setComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/submitComment"
android:layout_toLeftOf="#+id/submitComment"
android:ems="10"
android:hint="Add a comment" />
<ImageButton
android:id="#+id/submitComment"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/actionmenubackground"
android:src="#drawable/commentsubmitarrow" />
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/setComment"
android:background="#color/green" />
<ListView
android:id="#+id/commentsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:divider="#null"
android:dividerHeight="5dp"
android:clickable="false"
android:listSelector="#android:color/transparent"
android:layout_below="#+id/relativeLayout1"
android:layout_above="#+id/setComment"
android:choiceMode="none" />
</RelativeLayout>
Here is my loadComments method ();
void loadComments() {
runOnUiThread(new Runnable() {
#Override
public void run() {
MyCustomAdapter adapter = new MyCustomAdapter(getApplicationContext());
commentsListView.setAdapter(adapter);
}
});
}
you should return view
public View getItemView(ParseObject comment, View view, ViewGroup parent) {
view = View.inflate(getApplicationContext(), R.layout.comments_listview_item, null);
mListViewReferences(view);
//loadComments();
commenttext.setText(comment.getString("Comment"));
commentersUserName.setText(comment.getString("Commenter"));
return view;
}

Moving imageview in XML causes crash

I am making some changes to an XML layout side of my application. There is a ImageView I am trying to bring to the front yet when I do the app crashes and I can seem to tell why.
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
</LinearLayout>
<TextView
android:id="#+id/place_name"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_margin="2dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:src="#drawable/offline" />
XML that crashes:
<?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" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<TextView
android:id="#+id/place_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
Im just trying to bring the #place_img to the front of the #wrapper.
java.lang.NullPointerException
at com.peekatucorp.peekatu.DiscussArrayAdapter.getView(DiscussArrayAdapter.java:172)
DiscussArrayAdapter.java:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type==1 || type==3)
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
else if(type==4 || type==5)
row = inflater.inflate(R.layout.listitem_users, parent, false);
else
row = inflater.inflate(R.layout.listitem_messages, parent, false);
}
//
final OneComment coment = getItem(position);
userComment = (TextView) row.findViewById(R.id.comment);
userImage = (ImageView) row.findViewById(R.id.place_img);
userName = (TextView) row.findViewById(R.id.place_name);
userOnlineImage = (ImageView) row.findViewById(R.id.pe_profile_pic);
commentDate = (TextView) row.findViewById(R.id.place_distance);
userComment.setText(coment.comment);
ImageLoader imageLoader = ImageLoader.getInstance();
// imageLoader = ImageLoader.getInstance();
// imageLoader.init(ImageLoaderConfiguration.createDefault(convertView.getContext()));
imageLoader.displayImage(coment.image, userImage);
userImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
final TabInfo tab = navact.getCurrentTabInfo();
final ProfileFragment fragment = new ProfileFragment().setUser(coment.userid).setNAV(navact);
// fragment.setText(characters[position]);
// second, you push the fragment. It becomes visible and the up button is
// shown
navact.pushFragment(tab, fragment);
/*
Intent i = new Intent(context, ProfileActivity.class);
i.putExtra("userID", coment.userid);
// Create the view using FirstGroup's LocalActivityManager
View view = ChatTabGroup.group.getLocalActivityManager()
.startActivity("show profile", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
ChatTabGroup.group.replaceView(view);*/
}
});
if(coment.online.equalsIgnoreCase("1"))
userOnlineImage.setImageResource(R.drawable.online);
else
userOnlineImage.setImageResource(R.drawable.offline);
if(coment.gender.equalsIgnoreCase("M"))
userName.setTextColor(Color.parseColor("#878ff4"));
else if(coment.gender.equalsIgnoreCase("F"))
userName.setTextColor(Color.parseColor("#f487d6"));
else
userName.setTextColor(Color.parseColor("#969696"));
userName.setText(coment.username);
commentDate.setText(coment.time);
if(type==4){
commentDate = (TextView) row.findViewById(R.id.textView1);
SharedPreferences preferences = getContext().getSharedPreferences("MyPreferences", getContext().MODE_PRIVATE);
double distance = distFrom(Double.parseDouble(getItem(position).time.split(",")[0]),Double.parseDouble(getItem(position).time.split(",")[1]),
Double.parseDouble(preferences.getString("user_lat", "0.0")),Double.parseDouble(preferences.getString("user_lng", "0.0"))
);
commentDate.setText(""+String.format("%.2f", (distance*0.62))+"miles");
}
if(type==1 || type==3){
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
}else{
}
//
return row;
}
Thank you.
<?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" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true"
android:typeface="serif"
android:autoLink="web|all"
/>
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
<TextView
android:id="#+id/place_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/wrapper"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="normal" />

Categories

Resources