FrameLayout cannot be cast to AbsListView Layout Params - java

I have an strange issue I have the nexts code files:
result_suggestion.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="wrap_content"
android:id="#+id/result_suggestion_container"
android:orientation="vertical"
android:background="#fff">
<!--android:visibility="gone"-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/result_list"
android:scrollbars="vertical" />
</LinearLayout>
results_suggestions_footer.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#e6e9ea">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="250dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/results_suggestion_footer"
app:srcCompat="#drawable/ic_baseline_search_24px" />
<Button
android:id="#+id/keep_searching"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/search_button"
android:text="#string/quotes_suggestion_footer" />
<!--android:drawableEnd="#drawable/ic_baseline_search_24px"-->
</RelativeLayout>
bidAdapter.java:
package com.my.app.ui.adapters;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.my.app.R;
import com.my.app.api.models.sonnet;
import com.my.app.api.models.sonnetbid;
import com.my.app.api.interfaces.SuggestionListItem;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.login.LoginException;
/**
* Class of the custom adapter for the suggestions list of bids.
*
* #author Dennis Mostajo on 1/18/18
*/
public class bidAdapter extends BaseAdapter {
private static final int sonnet = 0;
private static final int bid = 1;
private List<SuggestionListItem> mSuggestions;
private LayoutInflater mInflater;
//===========================================================================
// CONSTRUCTOR
//===========================================================================
/**
* Custom constructor implementation in charge of the initialization of internal members
*
* #param context application context for the layout inflater initialization
*/
public bidAdapter(Context context) {
this.mSuggestions = new ArrayList<>();
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//===========================================================================
// OVERRIDE METHODS
//===========================================================================
#Override
public int getCount() {
return mSuggestions.size();
}
#Override
public Object getItem(int position) {
return mSuggestions.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if(mSuggestions.get(position).getInstanceType() == SuggestionListItem.InstanceType.sonnet) {
return sonnet;
} else {
return bid;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int cellType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch(cellType) {
case sonnet:
convertView = mInflater.inflate(R.layout.source_cell, null);
holder.mSource = convertView.findViewById(R.id.source);
holder.mSourceInformation = convertView.findViewById(R.id.source_information);
break;
case bid:
convertView = mInflater.inflate(R.layout.bid_cell, null);
holder.mbid = convertView.findViewById(R.id.bid);
holder.mbidInformation = convertView.findViewById(R.id.bid_information);
break;
}
if (convertView != null) {
convertView.setTag(holder);
}
} else {
holder = (ViewHolder)convertView.getTag();
}
switch (cellType) {
case sonnet:
holder.mSource.setText(((sonnet) mSuggestions.get(position)).getContent());
holder.mSourceInformation.setText("");
break;
case bid:
holder.mbid.setText(((sonnetbid) mSuggestions.get(position)).getText());
sonnet sonnet = ((sonnetbid) mSuggestions.get(position)).getsonnet();
String text = String.format("%s%s%s%s%s%s%s", " (", sonnet.getBook(), " ", sonnet
.getChapter(), ":", sonnet.getsonnetNumber(), ").");
holder.mbidInformation.setText(text);
break;
}
return convertView;
}
//===========================================================================
// CUSTOM METHODS
//===========================================================================
/**
* Method used to update the list of suggestions to be given to the user
*
* #param suggestions list containing suggested bids and stanzas
*/
public void updateSuggestions(List<SuggestionListItem> suggestions) {
if(suggestions != null) {
this.mSuggestions.clear();
this.mSuggestions = suggestions;
this.notifyDataSetChanged();
}
}
//===========================================================================
// PRIVATE CLASSES
//===========================================================================
/**
* Class for the implementation of the ViewHolder design pattern used to represent the bids
* and stanzas cells.
*/
private static class ViewHolder {
private TextView mbid;
private TextView mbidInformation;
private TextView mSource;
private TextView mSourceInformation;
}
}
and when I using my keyboardIME:
private View initInputView() {
mCurrentTheme = ThemeManager.getInstance(this).getTheme();
mKeyboardView.setKeyboard(mKeyboard);
mKeyboardView.setOnKeyboardActionListener(this);
mInflater = (LayoutInflater) this.getSystemService(Context
.LAYOUT_INFLATER_SERVICE);
mSuggestionsView = mInflater.inflate(R.layout.result_suggestion, null);
mResultListFooter = mInflater.inflate(R.layout.results_suggestion_footer, null);
initAlternativeCandidatesView();
mResultList = mSuggestionsView.findViewById(R.id.result_list);
mResultList.addFooterView(mResultListFooter);
mResultsAdapter = new bidAdapter(this);
mResultList.setAdapter(mResultsAdapter);
mResultList.setOnItemClickListener((AdapterView<?> parent, View view, int position,
long id) -> {
SuggestionListItem item = (SuggestionListItem) mResultsAdapter
.getItem(position);
});
return mKeyboardView;
}
#Override
protected void onPostExecute(SearchRequestResult result) {
switch (result) {
case SUCCESS:
if(mSonnetBidResult.size() > 0) {
List<SuggestionListItem> bidSonnetList = new ArrayList<>(
mSonnetBidResult);
mQuotesIME.get().mResultsAdapter.updateSuggestions(bisSonnetList);
// the crash is here
mQuotesIME.get().mQuoteList.setAdapter(mQuotesIME.get().mResultsAdapter);// IMEKeyboard.java:2305
mQuotesIME.get().mResultsAdapter);
// Added by Dennis Try to fix Height of suggestions lists
if (mSonnetBidResult.size() > 2) {
mQuotesIME.get().maximize_quote_lists();
} else {
mQuotesIME.get().minimize_quote_lists();
}
mQuotesIME.get().setCandidatesView(mQuotesIME.get().mSuggestionsView);
} else {
mQuotesIME.get().updateWaitingCandidateView(mQuotesIME.get().getString(R
.string.no_results_label));
}
break;
case ERROR:
try {
JSONObject jsonObject = new JSONObject(this.mErrorMessage);
int minLength = jsonObject.getInt("min_phrase_len");
if(minLength > 0) {
mQuotesIME.get().updateWaitingCandidateView(mQuotesIME.get().getString(
R.string.length_error).replace("{int}", String.valueOf(
minLength)));
} else {
mQuotesIME.get().updateWaitingCandidateView(mQuotesIME.get().getString(
R.string.no_phrase_error));
}
} catch (JSONException e) {
Crashlytics.log("Error extracting message from /search call: "
+ e.getMessage());
e.printStackTrace();
}
break;
}
}
}
}
ERROR CRASH:
07-16 14:41:09.488 21099-21099/com.my.app D/AndroidRuntime: Shutting down VM
07-16 14:41:09.514 21099-21099/com.my.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 21099
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.clearRecycledState(ListView.java:627)
at android.widget.ListView.resetList(ListView.java:614)
at android.widget.ListView.setAdapter(ListView.java:557)
at com.my.app.services.IMEKeyboard$resultSearchAsyncTask.onPostExecute(IMEKeyboard.java:2305)
at com.my.app.services.IMEKeyboard$resultSearchAsyncTask.onPostExecute(IMEKeyboard.java:2175)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
07-16 14:41:11.786 21099-21151/com.my.app I/CrashlyticsCore: Crashlytics report upload complete: 5D2E1A3601BE-0001-526B-29AD77A5B0D7
I have a strange issue setting up an adapter for a ListView, the ListView is inside the CandidateView of a custom keyboard, I use a class that inherit of AsyncTask class to execute an API call that returns me a list of items to be displayed on a ListView, in the method onPostExecute() of AsyncTask I take the search results and add to an ArrayList, then this array is being passed to the adapter using the method updateResults(), and finally set the ListView's adapter, everything is fine until here, the code compiles, but during runtime I get the next error:
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
ANY HELP PLEASE?, how can I fix it?
Thank you very much for your time

As I see, this issue can come from the wrong LayoutParams. ListView implementation expects AbsListView.LayoutParams and not FrameLayout.LayoutParams. I guess, one of your layout (source_cell.xml or bid_cell.xml) has a FrameLayout as root view. This is not a problem but you would fix the inflation method to get proper LayoutParams.
Change this in bidAdapter.java inside of getView() method:
convertView = mInflater.inflate(R.layout.source_cell, null);
To this:
convertView = mInflater.inflate(R.layout.source_cell, parent, false);
This inflation procedure will know the candidate parent where inflated view would be placed. This allows it to inflate with proper LayoutParam. The next false parameter are going to prevent to add child view automatically. (Because ListView implementation will add later)

For all I can see the error occurs a few calls after setting the adapter and happen inside the ListView, this is a complicated issue to fix taking into account the line of code where is crashing, also is hard to get this wrong without a warning from Android Studio or the compiler, so my suggestion is to use RecyclerView instead of the ListView, RecyclerView is a more advanced and flexible version of ListView, is easy to implement and I'm pretty sure that will fix your issue in the app. Follow this link and you will find a good example on how to implement the RecyclerView.

Related

Null pointer exception on button inside fragment layout android 9 [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I have a button placed inside a layout frag_contacts.xml that is intended for a fragment which is FragmentContacts.java . There is also row layout for the recyclerview which is items_contacts.xml . The button allows a user to save stuff after they are done picking recyclerview items. The app works without any errors on android Nougat .
However When I run the app on android 9 it crushes giving exception java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
The line throwing exceptions is the one below
buttonCreateGroup.setOnClickListener(this);
the error message
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ex_contactapp, PID: 3931
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.ex_contactapp.fragments.FragmentContacts.onCreateView(FragmentContacts.java:99)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:25056)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7745)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:25056)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7745)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:25056)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7745)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:25056)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7745)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:25056)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7745)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:25056)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7745)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:1051)
at android.view.View.measure(View.java:25056)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3382)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2403)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1964)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8721)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:988)
at android.view.Choreographer.doCallbacks(Choreographer.java:765)
at android.view.Choreographer.doFrame(Choreographer.java:700)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7285)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
layout for fragment - frag_contacts.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="100dp">
<EditText
android:id="#+id/group_name"
android:layout_width="263dp"
android:layout_height="70dp"/>
<Button
android:id="#+id/buttonCreateGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create_group"/>
</GridLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
Row layout -> items_contacts.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_margin="10dp"
android:weightSum="10">
<Button
android:layout_gravity="center"
android:background="#drawable/ic_person"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="32dp"/>
<LinearLayout
android:padding="10dp"
android:layout_width="0dp"
android:layout_weight="8"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/contact_name"
android:text="name"
android:textSize="20sp"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/contact_number"
android:text="number"
android:textSize="16sp"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<CheckBox
android:id="#+id/contact_checkbox"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="32dp"
android:layout_weight="1"/>
</LinearLayout>
Recycler View Adapter class -ContactsRvAdapter.java
public class ContactsRvAdapter extends RecyclerView.Adapter<ContactsRvAdapter.ViewHolder> {
private Context mContext;
private LayoutInflater inflater;
private List<ModelContacts> mlistContacts;
private CheckedStatusListener mcheckedStatusListener;
public interface CheckedStatusListener{
//String firstName, String lastName, String middleName, #NonNull String phoneNumber,#NonNull int groupid
void onItemChecked(String name, String phonenumber);
void onItemUnchecked(String name, String phonenumber);
}
public ContactsRvAdapter(Context context, List<ModelContacts> listContacts,CheckedStatusListener checkedStatusListener){
mlistContacts = listContacts;
mContext = context;
mcheckedStatusListener = checkedStatusListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.items_contacts,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
final TextView contact_name,contact_number;
final CheckBox contact_checkbox;
contact_name = holder.contact_name;
contact_number = holder.contact_number;
contact_name.setText(mlistContacts.get(position).getName());
contact_number.setText(mlistContacts.get(position).getNumber());
contact_checkbox = holder.contactSelectedCheckBox;
contact_checkbox.setOnClickListener(v -> {
if (contact_checkbox.isChecked()){
mcheckedStatusListener.onItemChecked(mlistContacts.get(position).getName(),mlistContacts.get(position).getNumber());
}else{
mcheckedStatusListener.onItemUnchecked(mlistContacts.get(position).getName(),mlistContacts.get(position).getNumber());
}
});
}
#Override
public int getItemCount() {
return mlistContacts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView contact_name,contact_number;
CheckBox contactSelectedCheckBox;
public ViewHolder(View itemView){
super(itemView);
contact_name = itemView.findViewById(R.id.contact_name);
contact_number = itemView.findViewById(R.id.contact_number);
contactSelectedCheckBox = itemView.findViewById(R.id.contact_checkbox);
}
}
}
Fragment class - FragmentContacts.java
public class FragmentContacts extends Fragment implements ContactsRvAdapter.CheckedStatusListener, View.OnClickListener{
private View v;
private RecyclerView recyclerView;
private List<ModelContactsBuffer> currentSelectedContacts = new ArrayList<>();
ContactsRvAdapter adapter;
private EditText editTextGroupName;
private ContactGroupViewModel contactGroupViewModel;
private ModelContactsBuffer modelContactsBuffer;
private GroupListViewModel groupListViewModel;
public FragmentContacts() {
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.frag_contacts,container,false);
recyclerView = v.findViewById(R.id.rv_contacts);
editTextGroupName = v.findViewById(R.id.group_name);
Button buttonCreateGroup = v.findViewById(R.id.buttonCreateGroup);
buttonCreateGroup.setOnClickListener(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
RecyclerView.LayoutManager layoutManager= linearLayoutManager;
recyclerView.setLayoutManager(layoutManager);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M &&
ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_CONTACTS},1);
}else {
adapter = new ContactsRvAdapter(getContext(), getContacts(),this);
recyclerView.setItemViewCacheSize(getContacts().size());
recyclerView.setAdapter(adapter);
// adapter.notifyDataSetChanged();
}
contactGroupViewModel = ViewModelProviders.of(this, new ContactGroupViewModel.Factory(Objects.requireNonNull(getActivity()).getApplicationContext())).get(ContactGroupViewModel.class);
groupListViewModel = ViewModelProviders.of(this,new GroupListViewModel.Factory(getActivity().getApplicationContext())).get(GroupListViewModel.class);
return v;
}
#Override
public void onClick(View v) {
Log.i("editTextGroupNamelength", String.valueOf(editTextGroupName.getText().toString().length()));
if (editTextGroupName.getText().toString().length() > 2) {
if(currentSelectedContacts.size() > 4){
contactGroupViewModel.createGroup(editTextGroupName.getText().toString(),String.valueOf(currentSelectedContacts.size()));
Integer groupId = contactGroupViewModel.readGroupId(editTextGroupName.getText().toString());
insertGrouplist(groupId);
//clearFields();
}else{
new AlertDialog.Builder(Objects.requireNonNull(this.getActivity()))
.setIcon(R.drawable.ic_error)
.setTitle("Insufficient group members")
.setMessage("A group should have at least 5 members")
.setNeutralButton("Ok",null)
.show();
}
}else{
new AlertDialog.Builder(Objects.requireNonNull(this.getActivity()))
.setIcon(R.drawable.ic_error)
.setTitle("Group name is too short")
.setMessage("A group should have atleast be 3 characters long")
.setNeutralButton("Ok",null)
.show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == 1){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
//now permission is granted call function again
adapter = new ContactsRvAdapter(getContext(), getContacts(),this);
recyclerView.setItemViewCacheSize(getContacts().size());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
private List<ModelContacts> getContacts(){
List<ModelContacts> list = new ArrayList<>();
try{
String[] PROJECTION = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String selectionFields = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + ContactsContract.CommonDataKinds.Phone.TYPE +"=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
String[] selectionArgs = new String[]{"com.google"};
Cursor cursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,selectionFields,null,ContactsContract.Contacts.DISPLAY_NAME + " ASC");
cursor.moveToFirst();
while(cursor.moveToNext()){
list.add(new ModelContacts(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)),
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
}
}catch (Exception e){
Toast.makeText(getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
return list;
}
#Override
public void onItemChecked(String name, String phonenumber) {
String firstName = " ";
String lastName = " ";
if(name.split("\\w+").length>1){
lastName = name.substring(name.lastIndexOf(" ")+1);
firstName = name.substring(0,name.lastIndexOf(' '));
}else{
firstName = name;
lastName = " ";
}
modelContactsBuffer = new ModelContactsBuffer(firstName,lastName,phonenumber);
currentSelectedContacts.add(modelContactsBuffer);
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onItemUnchecked(String name, String phonenumber) {
String firstName = " ";
String lastName = " ";
if(name.split("\\w+").length>1){
lastName = name.substring(name.lastIndexOf(" ")+1);
firstName = name.substring(0,name.lastIndexOf(' '));
}else{
firstName = name;
lastName = " ";
}
currentSelectedContacts.removeIf( currentSelectedContact -> currentSelectedContact.getPhoneNumber().equals(phonenumber));
}
public void insertGrouplist(int groupId){
for (ModelContactsBuffer currentSelectedContact : currentSelectedContacts) {
groupListViewModel.createGroupList(currentSelectedContact.getFirstName(), currentSelectedContact.getLastName(), " ", currentSelectedContact.getPhoneNumber(), groupId);
}
clearFields();
}
}
I commented the line throwing an exception and the app runs pretty well. Hence the problem is from buttonCreateGroup.setOnClickListener(this);
I have tried many solutions here in stack overflow which didnt work.
I have checked the id of the button and it is correct
I don't think it is possible initializing the onclicklistener inside ContactsRvAdapter because its not even in the row layout. It is in the other layout wwhich is intended for the fragment
I tried passing the normal new OnViewClicklistener to the button instead of passing the whole activity but nothong changed. Still getting an error
I later on moved the clicklistener call from public View onCreateView() to public void onViewCreated() but there was no change. I am stil getting the same error.
I tried removing #RequiresApi(api = Build.VERSION_CODES.N) Nothing changed
Kindly note I am not a professional on android. I added some things like #RequiresApi(api = Build.VERSION_CODES.N) because android lint would show an error and that was the only way I could deal with it at that time. I apprectiate any help offered on solving this
You probably get this error(null object reference) because you didn't define the button. You have to define like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_contacts, container, false);
Button buttonCreateGroup = view.findViewById(R.id.buttonCreateGroup);
buttonCreateGroup.setOnClickListener(this);
return view;
}
Or this func:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button buttonCreateGroup = view.findViewById(R.id.buttonCreateGroup);
buttonCreateGroup.setOnClickListener(this);
}
edit func.
So for some apparent reason I didn't think about the fact that I had two xml files for frag_contacts, one in res/layouts and another in res/layout-v26
I admit I am not a pro on android and I kinda auto generated the latter xml file without knowing its use but I still continued working on the on in res/layouts & fully abandoned the one in res/layout-v26 because I didn't know it's use.
However it did have a button and a couple of other widgets but with no ID. Since it was in res/layout-v26 meaning it would show for devices compatible with v-26 and totally ignore the one on res/layouts, that's the layout file that would show on android 9 device. & since the button didn't have an ID, a null pointer exception would be thrown.
I have deleted the layout-v-26 file so that devices will fallback to the default layout. The app now works fine.

Glide overlays screen with loaded image parts

Since i could not find anything related in the internet, this is gonna be my first SO question:
Problem Description
I am experiencing the following issue:
In my app i have a RecyclerView with a GridLayoutManager which is displaying images loaded by Glide.
When scrolling slowly everything will work alright.
After scrolling fast up and down (to an extend one could arguably call it "abuse of scrolling") some image parts or whole images will randomly overlay my screen, so that the content below is no longer visible. This is especially the case on the edges of the screen.
The testing device where the described error occurs is a OnePlus One running CM12.1.1 (Android 5.1.1). I didn't test on other devices.
Any hints / help / explanation on why this is happening or how to fix it are appreciated.
CODE
Below is the code for the Adapter with an inner ViewHolder class:
public class PhotoListAdapter extends RecyclerView.Adapter<PhotoListAdapter.PhotoHolder>{
private ArrayList<PhotoDetail> mPhotos;
private String mAccessToken;
private int mUserId;
private int mPageCounter = 1;
private boolean mIsLoading = false;
private boolean mEndReached = false;
public PhotoListAdapter (String accessToken, int userId){
mPhotos = new ArrayList<>();
mAccessToken = accessToken;
mUserId = userId;
fetchPhotos();
}
#Override
public PhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_photo_list_item, parent, false);
PhotoHolder photoHolder = new PhotoHolder(itemView);
return photoHolder;
}
#Override
public void onBindViewHolder(PhotoHolder holder, int position) {
holder.bind(mPhotos.get(position));
if (position == mPhotos.size() - 6) fetchPhotos();
}
#Override
public int getItemCount() {
return mPhotos.size();
}
public void fetchPhotos () {
if (mIsLoading || mEndReached) return;
else {
mIsLoading = true;
//Retrofit call which will return a JSON Object with the Photo Details, like the filename
// With this information i can build the URL of the image
ApiProvider.getInstance().getApi().getPhotos(mUserId, mAccessToken, "time", "all", mPageCounter, mUserId, 32).enqueue(new Callback<PhotoResponseWrapper>() {
#Override
public void onResponse(Call<PhotoResponseWrapper> call, Response<PhotoResponseWrapper> response) {
if (response.code() == HttpURLConnection.HTTP_OK){
int photoCount = response.body().getPhotoResponse().getPhotosCount();
if (photoCount != 32) mEndReached = true;
Collections.addAll(mPhotos, response.body().getPhotoResponse().getPhotos());
notifyItemRangeInserted(mPhotos.size() - photoCount,photoCount);
mPageCounter++;
}
mIsLoading = false;
}
#Override
public void onFailure(Call<PhotoResponseWrapper> call, Throwable t) {
mIsLoading = false;
}
});
}
}
class PhotoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mPhotoView;
private PhotoDetail mPhotoDetail;
public PhotoHolder(View itemView) {
super(itemView);
mPhotoView = (ImageView) itemView.findViewById(R.id.img_photo);
mPhotoView.setOnClickListener(this);
}
public void bind (PhotoDetail photoDetail){
mPhotoDetail = photoDetail;
String photoUrl = Api.Endpoints.PHOTOS_SMALL + photoDetail.getUserId() + '/' + photoDetail.getFileName();
mPhotoView.setContentDescription(photoDetail.getDescription());
// First stop all pending image loads
Glide.clear(mPhotoView);
// Then load new url
Glide.with(mPhotoView.getContext())
.load(photoUrl)
.into(mPhotoView);
}
#Override
public void onClick(View v) {
}
}
}
Below is the XML of a single Item:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_photo"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="#dimen/height_photo_item"
android:padding="#dimen/padding_photo_item"
android:scaleType="centerCrop"/>
Below is the XML of the RecyclerView:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/padding_photo_item"
android:paddingLeft="#dimen/padding_photo_item"
android:paddingTop="#dimen/padding_photo_item"
tools:context=".fragments.PhotoListFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
EXAMPLES
Some visual examples of the bug are these screenshots:
You have to use placeholder image.
Glide.with(mPhotoView.getContext())
.load(photoUrl)
.placeholder(R.drawable.image_placeholder) // Your placeholder image
.into(mPhotoView);
I had the same problem and setting the placeholder eliminated it for me.
Since I couldn't find any information on how to fix this, i tried out the major alternative image loading & caching library - Picasso.
With this library the bug mentioned above wasn't reproducible for me, so i guess it is really Glide specific.

Android - cannot scroll up

First off, I first laid eyes on Java three weeks ago so bear with me if this code is terrible. It's an assignment for school that I am to build on a prototyped app and give it a UI, so the Adapter is basically all I've done to this.
My problem being that as soon as I touch the scroll, I get thrown to the bottom of the list and can't scroll back up without getting pushed back down.
/**
* VaxjoWeather.java
* Created: May 9, 2010
* Jonas Lundberg, LnU
*/
package dv106.weather;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
* This is a first prototype for a weather app. It is currently
* only downloading weather data for Växjo.
*
* This activity downloads weather data and constructs a WeatherReport,
* a data structure containing weather data for a number of periods ahead.
*
* The WeatherHandler is a SAX parser for the weather reports
* (forecast.xml) produced by www.yr.no. The handler constructs
* a WeatherReport containing meta data for a given location
* (e.g. city, country, last updated, next update) and a sequence
* of WeatherForecasts.
* Each WeatherForecast represents a forecast (weather, rain, wind, etc)
* for a given time period.
*
* The next task is to construct a list based GUI where each row
* displays the weather data for a single period.
*
*
* #author jlnmsi
*
*/
public class VaxjoWeather extends ListActivity {
//private InputStream input;
private WeatherReport report = null;
//private ArrayList<WeatherForecast> forecastList = new ArrayList<WeatherForecast>();
private WeatherAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new WeatherAdapter(this);
setListAdapter(adapter);
//getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);
try {
URL url = new URL("http://www.yr.no/sted/Sverige/Kronoberg/V%E4xj%F6/forecast.xml");
AsyncTask task = new WeatherRetriever().execute(url);
} catch (IOException ioe ) {
ioe.printStackTrace();
}
//adapter.notifyDataSetChanged();
}
private void PrintReportToConsole() {
if (this.report != null) {
/* Print location meta data */
//System.out.println(report);
/* Print forecasts */
int count = 0;
for (WeatherForecast forecast : report) {
count++;
adapter.add(forecast);
}
}
else {
System.out.println("Weather report has not been loaded.");
}
//adapter.notifyDataSetChanged();
}
private class WeatherRetriever extends AsyncTask<URL, Void, WeatherReport> {
protected WeatherReport doInBackground(URL... urls) {
try {
return WeatherHandler.getWeatherReport(urls[0]);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void onProgressUpdate(Void... progress) {
}
protected void onPostExecute(WeatherReport result) {
report = result;
PrintReportToConsole();
}
}
// custom ArrayAdpater to show, weather icon, temperature, and precipation.
class WeatherAdapter extends ArrayAdapter<WeatherForecast> {
public WeatherAdapter(Context context) {
super(context,R.layout.forecast);
}
#Override // Called when updating the ListView
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) { // Create new row view object
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.forecast,parent,false);
}
else // reuse old row view to save time/battery
row = convertView;
// TextView for Temperature
TextView temperature = (TextView)row.findViewById(R.id.temperature);
temperature.setText(Integer.toString(this.getItem(position).getTemp())+" °C");
// TextView for out Precipation.
TextView precipation = (TextView)row.findViewById(R.id.rain);
precipation.setText(String.valueOf(this.getItem(position).getRain())+" mm");
// Image Icon for forecast.
ImageView icon = (ImageView)row.findViewById(R.id.icon);
String iconPath = "ic_";
if (this.getItem(position).getWeatherCode() <= 9){
iconPath = iconPath+"0"+(Integer.toString(this.getItem(position).getWeatherCode()));
}
else {
iconPath = iconPath+(Integer.toString(this.getItem(position).getWeatherCode()));
}
int resId = getResources().getIdentifier(iconPath, "drawable", getPackageName());
// If the resource ID is invalid, as in the image not existing, we'll add the postfix for periods.
if (resId == 0){
// Set the icon image source dependent on period code given.
if(this.getItem(position).getPeriodCode() == 3){
iconPath = iconPath +"n";
}
else if (this.getItem(position).getPeriodCode() == 2){
iconPath = iconPath +"d";
}
else {
iconPath = iconPath +"m";
}
resId = getResources().getIdentifier(iconPath, "drawable", getPackageName());
icon.setImageResource(resId);
}
// Or if everything checked out, we'll just run with the resource ID and find our Icon.
else {
icon.setImageResource(resId);
}
return row;
}
}
}
I tried applying another standard arrayadapter and actually got the same unwanted scrolling results, so I got no idea what part it is I got issues with.
Way to do it is:
1: Place ListView in main.xml
2: In your Activity Make object of ListView like so -> private ListView listView; in onCreate connect it to the main.xml like this, listView = (ListView) R.id.listView1; // whatever it is called
3: next create an ArrayList:-> private ArrayList arrayWeather = new ArrayList();
4: fill the arraylist with weather data then finally create object of class you created and make it use your arraylist to display data.
Example:
public class ListUser extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
return arraylistData.size(); // the arraylist u created
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arraylistData.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return arraylistData.size();
}
#Override
public View getView(final int position, View v, ViewGroup parent) {
if(v == null){
LayoutInflater lf = (LayoutInflater) BuyPets.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE);
v = lf.inflate(R.layout.forecast, null);
}
// setup here, done
return v;
}
}
Solution was found and apparently it had nothing to do with my code. Class suggested we'd use Intel x86 instead of ARM for our emulators. Running it with ARM scrolling work just as expected.

ListView list is reversed when selection made

So I had my listview working perfectly then I decided to add a context menu. As soon as I did that whenever I normal clicked an item in my listview, the entire list gets inverted on the first click. Subsequent clicks do nothing to the order, but when the first item is de-selected again the list returns to normal. When I take out the context menu logic that I added, the list view problem does not go away.
I've attached a debugger and the elements in my list adapter are never reordered, and the ListView itself is never set to reverse with .setStackFromBottom()
Here is my onClick listener registered to handle the click events of the list view items:
public void onClick(View v) {
ViewHolder holder = (ViewHolder) v.getTag();
CheckBox b = holder.box;
Boolean check = b.isChecked();
b.setChecked(!check);
if (!check) {
mChecked.add(holder.fu);
if (mChecked.size() == 1) {
buttonLayout.setVisibility(View.VISIBLE);
}
} else {
mChecked.remove(holder.fu);
if (mChecked.size() == 0) {
buttonLayout.setVisibility(View.GONE);
}
}
}
The viewholder class just holds references to objects I use in the listview for optimizations. I cannot figure out why this is causing my list to invert when displayed, I've tried moving the listener to a different view in the layout, I've tried re-writing the listener, nothing seems to work! Any advice would be appreciated.
Edit: here is the code for the view holder
/** Class to provide a holder for ListViews. Used for optimization */
private class ViewHolder {
TextView date;
TextView gallons;
TextView cost;
TextView cpg;
TextView mpg;
CheckBox box;
FillUp fu;
}
as well as the adapter:
public class FillUpAdapter extends BaseAdapter {
ArrayList<FillUp> mElements;
ArrayList<FillUp> mChecked;
Context mContext;
public FillUpAdapter(Context c, ArrayList<FillUp> data) {
mContext = c;
mElements = data;
mChecked = new ArrayList<FillUp>();
}
public void clearChecked() {
mChecked.clear();
}
public ArrayList<FillUp> getChecked() {
return mChecked;
}
public boolean remove(FillUp f) {
mChecked.remove(f);
return mElements.remove(f);
}
#Override
public int getCount() {
return mElements.size();
}
#Override
public Object getItem(int arg0) {
return mElements.get(arg0);
}
#Override
public long getItemId(int arg0) {
return mElements.get(arg0).getId();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout;
ViewHolder holder;
if (convertView != null) {
layout = (LinearLayout) convertView;
holder = (ViewHolder) layout.getTag();
} else {
layout = (LinearLayout) LayoutInflater.from(mContext).inflate(
R.layout.fillup_list_item, parent, false);
holder = new ViewHolder();
holder.cost = (TextView) layout
.findViewById(R.id.fillUpListTotalValue);
holder.cpg = (TextView) layout
.findViewById(R.id.fillUpListCostPerGal);
holder.gallons = (TextView) layout
.findViewById(R.id.fillUpListGalValue);
holder.mpg = (TextView) layout
.findViewById(R.id.fillUpMPGText);
holder.date = (TextView) layout
.findViewById(R.id.fillUpListDate);
holder.box = (CheckBox) layout
.findViewById(R.id.fillUpListCheckBox);
holder.fu = (FillUp) getItem(position);
layout.setTag(holder);
}
holder.date.setText(holder.fu.getDate());
holder.gallons.setText(holder.fu.getGallonsText());
holder.cpg.setText(holder.fu.getCostText());
holder.cost.setText(holder.fu.getTotalText());
holder.mpg.setText(String.format("%03.1f MPG",holder.fu.getMPG()));
if (convertView != null) {
holder.box.setChecked(mChecked.contains(holder.fu));
}
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder holder = (ViewHolder) v.getTag();
CheckBox b = holder.box;
Boolean check = b.isChecked();
b.setChecked(!check);
if (!check) {
mChecked.add(holder.fu);
if (mChecked.size() == 1) {
buttonLayout.setVisibility(View.VISIBLE);
}
} else {
mChecked.remove(holder.fu);
if (mChecked.size() == 0) {
buttonLayout.setVisibility(View.GONE);
}
}
}
});
return layout;
}
}
UPDATE:
Ok, so I've narrowed it down to the visibility change on the buttonLayout view, which is a linear layout of buttons on the bottom of the Activity's layout, underneath the ListView. Whenever I change that view's visibility to View.VISIBLE (which happens when the first item is checked) the list's order is reversed. The order is restored when the view's visibility is set to View.GONE
I have no idea what would cause that though :(
After narrowing the scope a bit more, I discovered the problem was not the changing of the visibility of my button bar, but actually the passing around of FillUp objects in holder.fu of my ViewHolder class. By changing that to instead reference the adapter's getItem(position) method, everything seemed to work out. Quite an odd bug, since the adapter itself was not having the order of the elements changed, but passing around a reference to the object made it very unhappy.
If your listview background color changes when you click on it, I think it is about your theme. Just play with the cache color parameters of your listview, here is an example:
<ListView
android:layout_width="fill_parent"
android:id="#android:id/list"
android:scrollingCache="true"
android:persistentDrawingCache="all"
android:cacheColorHint="#0000"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:stackFromBottom="true"
android:smoothScrollbar="true"
android:paddingTop="115dip">
</ListView>

Cannot display image correctly using custom item layout in ListView

I am using a ListView to display my custom item layout, which may contain some TextViews and an ImageView.
This is the item layout I made (post_item.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="60dip"
android:padding="5dip"
>
<TextView android:id="#+id/listItem_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
/>
<FrameLayout android:id="#+id/listItem_frameContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/userStream_listItem_title"
android:layout_weight="1"
>
</FrameLayout>
</RelativeLayout>
I don't put the ImageView initially in the xml layout, but I will insert it when I need to the FrameLayout programmatically (I also put another views in it when needed). So, there will be some items which has ImageView in it and which don't.
I get the image to fill the ImageView from the Internet (through URL), decode it as Bitmap, and keep it as a Bitmap variable in a class represents the custom item layout (class PostItem).
When the activity shows for the first time it looks fine, but as I scrolled through the items then a problem showed up, the items which shouldn't show any image show the image from the other item which should, although they don't have any ImageView (because I didn't insert it).
I am using SDK 1.6 and the emulator. Haven't tried it in real device because I don't have it.
Here is my code for the Adapter:
private class PostItemAdapter extends ArrayAdapter<PostItem> {
private List<PostItem> mPostItems;
public PostItemAdapter(Context context, int textViewResourceId, List<PostItem> postItems) {
super(context, textViewResourceId, postItems);
mPostItems = postItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.post_item, null);
}
PostItem postItem = mPostItems.get(position);
if (postItem != null) {
final FrameLayout contentFrame = (FrameLayout) view.findViewById(R.id.listItem_frameContent);
final TextView titleTextView = (TextView) view.findViewById(R.id.listItem_title);
if (titleTextView != null) {
titleTextView.setText(postItem.getTitle());
}
if (contentFrame != null) {
if (postItem.hasImagePreview()) {
final ImageView previewImageView = new ImageView(getApplicationContext());
previewImageView.setId(LIST_ITEM_IMAGEVIEW_ID);
previewImageView.setAdjustViewBounds(true);
previewImageView.setMaxHeight(75);
previewImageView.setMaxWidth(75);
previewImageView.setImageBitmap(postItem.getImagePreview());
final RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
contentFrame.addView(previewImageView, layoutParams);
}
if (postItem.hasContent()) {
final TextView contentTextView = new TextView(getApplicationContext());
contentTextView.setText(postItem.getContent());
final RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, LIST_ITEM_IMAGEVIEW_ID);
layoutParams.addRule(RelativeLayout.ALIGN_LEFT, LIST_ITEM_IMAGEVIEW_ID);
layoutParams.alignWithParent = true;
contentFrame.addView(contentTextView, layoutParams);
}
}
}
return view;
}
}
And this is the code to prepare the items:
/* inside a method to prepare the items */
mPostItems = new ArrayList<PostItem>();
for (int i=0; i<total); i++) {
/* operation to generate titleToDisplay and contentToDisplay */
/* contentToDisplay might be null */
mPostItems.add(new PostItem(
titleToDisplay,
contentToDisplay,
generateImagePreview()));
}
/* another method */
private Bitmap generateImagePreview(ActivityObject object) {
Bitmap imagePreview = null;
if (/*some condition*/) {
try {
InputStream inStream = (InputStream) (new URL("http://a1.typepad.com/6a010535617444970b0133ecc20b29970b-120si")).getContent();
imagePreview = Drawable.createFromStream(inStream, "linkHref");
}
catch (MalformedURLException ex) {
Log.e("INSIDE generateImagePreview()", ex.getMessage());
}
catch (IOException ex) {
Log.e("INSIDE generateImagePreview()", ex.getMessage());
}
catch (Exception ex) {
Log.e("INSIDE generateImagePreview()", ex.getMessage());
}
}
return imagePreview;
}
Is this the bug in the emulator or there is some mistake in my code (probably memory problem for the Bitmap)? Please help me, I'm really stuck in this. Any help or advice will be very appreciated. Thanks in advance :)
Cells get reused by lists and it looks like you are only altering your contentframe image if there is an image associated with the item. This means when there is no image and you are reusing a cell, it will just show whatever was passed in in the convert view. If there is no image then you should remove all children views of contentframe.
That said, you will likely get better responses in the future by being more concise. Putting up a bunch of code and taking a while to get to the issue makes it more of a chore to read and so fewer people will.

Categories

Resources