Listview add item one at a time - android

I have a listview and a button in my main activity and three layout ressource files (right.xml, mid.xml and left.xml [They're relative layout]).
I want to make an arrayList (with strings and drawable (images)) and each time I push the button in main.xml the first content of the arrayList will appear at the bottom of the screen (either left, mid or right --> depend of the order of the arrayList) and when I click again the next item (string or drawable) will appear beneath it, pushing it in an upward motion.
UPDATE
I made a Model and an Adapter
Here is the model
public class ModelC1 {
public String C1Name;
public String C1Text;
public int id;
public boolean isSend;
public ModelC1(String C1Name, String C1Text, int id, boolean isSend){
this.id = id;
this.C1Name = C1Name;
this.C1Text = C1Text;
this.isSend = isSend;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getC1Name() {
return C1Name;
}
public void setC1Name(String C1Name){
this.C1Name = C1Name;
}
public String getC1Text() {
return C1Text;
}
public void setC1Text (String C1Text){
this.C1Text = C1Text ;
}
public boolean isSend() {
return isSend;
}
public void setIsSend(boolean send){
isSend = send;
}
Here is the Adapter
public class AdapterC1 extends BaseAdapter {
private List<ModelC1> listChat;
private LayoutInflater inflater;
private Context context;
public AdapterC1(List<ModelC1> listChat, Context context){
this.listChat = listChat;
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listChat.size();
}
#Override
public Object getItem(int i) {
return listChat.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View vi = convertView;
if(convertView == null ){
if(listChat.get(i).isSend() == 0)
vi=inflater.inflate(R.layout.list_send,null);
else if ((listChat.get(i).isSend() == 1))
vi=inflater.inflate(R.layout.list_recv,null);
else if ((listChat.get(i).isSend() == 2))
vi=inflater.inflate(R.layout.list_mid,null);
}else{
if(listChat.get(i).isSend() == 0)
vi=inflater.inflate(R.layout.list_send,null);
else if ((listChat.get(i).isSend() == 1))
vi=inflater.inflate(R.layout.list_recv,null);
else if ((listChat.get(i).isSend() == 2))
vi=inflater.inflate(R.layout.list_mid,null);
}
if(listChat.get(i).isSend() !=0 || listChat.get(i).isSend() !=1 || listChat.get(i).isSend() !=2 ){
BubbleTextView bubbleTextView = (BubbleTextView) vi.findViewById(R.id.bubbleChat);
if(bubbleTextView != null)
bubbleTextView.setText(listChat.get(i).C1Text);
TextView nameTextView = (TextView) vi.findViewById(R.id.nameChat);
if(nameTextView != null)
nameTextView.setText(listChat.get(i).C1Name);
}else{
vi=inflater.inflate(R.layout.list_mid,null);
BubbleTextView bubbleTextView = (BubbleTextView) vi.findViewById(R.id.bubbleChat);
bubbleTextView.setText("THE END");
}
return vi;
}
And here is the activity
public class Chat1 extends AppCompatActivity {
private static final String TAG = "Chat1";
private AdapterC1 adapter;
private List<ModelC1> listChat = new ArrayList<>();
private int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat1);
RecyclerView chatContent1 = findViewById(R.id.chatContent1);
}
private ModelC1 setUpMessage(){
Log.d(TAG, "setUpMessage: Exec");
return();
}
///OnClick of the button in the activity_chat1.xml
public void nextClicked1(View view) {
Log.d(TAG, "nextClicked: Is Clicked");
///After the limit of the arraylist is reached
final int limit = 40;
if(count == limit){
Log.d(TAG, "nextClicked: Limit Reached");
Intent i = new Intent(Chat1.this, MainActivity.class);
startActivity(i);
}else{
///Call the list
loadList(null);
}
}
///Load the list of arrays?
public void loadList(View view){
ModelC1 chat = setUpMessage();
listChat.add(chat);
///The ID of the recycleview in the activity_chat1.xml
final RecyclerView recyclerview = findViewById(R.id.chatContent1);
///The adapter
final AdapterC1 adapter = new AdapterC1(listChat, this);
///Make the recyclerview always scroll
///the adapter
///recyclerview.setAdapter(adapter);
}
My questions are now how do I make the ArrayList (containing strings and drawables) and how to link the ArrayList to make it appear one by one when I click on the button ?
As for the ArrayList, will soemthing like that works ?
private List<List<String>> textChat1 = new ArrayList<List<String>>();
ArrayList<String> textChat1 = new ArrayList<String>();
textChat1.add("This is message 1");
textChat1.add("This is message 2");
textChat1.add("This is message 2");
addresses.add(textChat1);
How can I add images and how to say which strings inflate which layout (left, mid or right) ?

You can do your job like this: in your Adapter's getView method ,
#Override
public View getView(int position, View convertView, ViewGroup container) {
if (convertView == null) {
if (position == 1) {
convertView = getLayoutInflater().inflate(R.layout.left, container, false);
} else if (position == 2) {
convertView = getLayoutInflater().inflate(R.layout.mid, container, false);
} else {
convertView = getLayoutInflater().inflate(R.layout.right, container, false);
}
}
//your code here
return convertView;
}
This will do your job, but, I suggest you to use Recyclerview because it's more efficient and better in terms of looks as well as memory management.

Related

Android ListView glitching back to top for a few seconds before it works properly

I am trying to make an application with a ListView that include a Country Flag and name. This is so that the user can click on them and be shown images of the country that they wouldve taken before. However for about 3 seconds when the listview loads if i try to scroll it will sort of glitch and send me back to top. This is the code..
public class CountriesListAdapter extends ArrayAdapter {
private int resource;
private LayoutInflater inflater;
private List<CountryModel> countryModels;
private WeakReference<TextView> selectedCountryIdtxt;
private boolean useFilter;
private WeakReference<ProgressBar> progressBarWeakReference;
public int getSelectedCountryId() {
return selectedCountryId;
}
public void setSelectedCountryId(int selectedCountryId) {
this.selectedCountryId = selectedCountryId;
}
private int selectedCountryId;
public CountriesListAdapter(#NonNull WeakReference<Context> context, int resourceId, WeakReference<TextView> textView, #NonNull List<CountryModel> countryModelList, boolean useFilter, WeakReference<ProgressBar> progressBarWeakReference){
super(context.get(), resourceId, countryModelList);
selectedCountryIdtxt = textView;
resource = resourceId; //the id of the template file
inflater = LayoutInflater.from(context.get());
this.countryModels = countryModelList;
selectedCountryId = -1;
this.useFilter = useFilter;
this.progressBarWeakReference = progressBarWeakReference;
}
public int getCount() {
if (countryModels != null)
return countryModels.size();
return 0;
}
public CountryModel getItem(int position) {
if (countryModels != null)
return countryModels.get(position);
return null;
}
public long getItemId(int position) {
if (countryModels != null)
return countryModels.get(position).hashCode();
return 0;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// this method is automatically called for every object in our list
//basically it's called for every single row before it is generated
// this method is called per row
convertView = (ConstraintLayout) inflater.inflate(resource, null);
//the variable countryModel is fiiled with current object that is being processed
final CountryModel countryModel = countryModels.get(position);
TextView countryName = convertView.findViewById(R.id.countryNamelbl);
final ImageView countryFlag = convertView.findViewById(R.id.countryFlagimg);
final ImageView checked = convertView.findViewById(R.id.countryCheckedimg);
//this is done for every object in the list
assert countryModel != null;
countryName.setText(countryModel.getName());
Picasso.get().load(countryModel.getImage()).fit().into(countryFlag);
if(!useFilter) {
if (selectedCountryId == countryModel.getId()) {
checked.setVisibility(View.VISIBLE);
} else {
checked.setVisibility(View.INVISIBLE);
}
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!useFilter) {
if (checked.getVisibility() == View.VISIBLE) {
checked.setVisibility(View.INVISIBLE);
selectedCountryId = -1;
selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
} else {
if (selectedCountryId == -1) {
checked.setVisibility(View.VISIBLE);
selectedCountryId = countryModel.getId();
} else {
selectedCountryId = countryModel.getId();
notifyDataSetChanged();
}
selectedCountryIdtxt.get().setText(String.valueOf(selectedCountryId));
}
} else {
Intent i = new Intent(getContext(), PicturesActivity.class);
i.putExtra("countryId",countryModel.getId());
i.putExtra("countryName", countryModel.getName());
getContext().startActivity(i);
}
}
});
progressBarWeakReference.get().setVisibility(View.INVISIBLE);
return convertView;
}
public List<CountryModel> getCountryModels() {
return countryModels;
}
public void setCountryModels(List<CountryModel> countryModels) {
this.countryModels = countryModels;
}
}
The problem was actually in another class, i was calling the adapter for every list item instead of just once... oops.
Thanks for the replies though!

How to show sort button on TableFixHeaders

I am using InqBarna's library for showing data in table format. I am facing issue for sort button change (ascending and descending order) when clicked on sort button on table header. Please help.
Here code snippet.
private View getHeader(int row, int column, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.item_table_header, parent, false);
}
convertView.setBackgroundResource(column % 2 == 0 ? R.color.header_dark_gray : R.color.header_light_gray);
((TextView) convertView.findViewById(android.R.id.text1)).setText(headers[column + 1]);
imageButtons[column +1] = ((ImageButton) convertView.findViewById(R.id.sortButton));
imageButtons[column +1].setTag(headers[column + 1]);
imageButtons[column +1].setOnClickListener(onClickListener);
return convertView;
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageButton imageButton =(ImageButton)v.findViewWithTag(v.getTag());
String header = v.getTag().toString();
switch (header) {
// logic for change image
case Query.AQ_NO:
if (AQ_NO_FLAG) {
imageButton.setImageResource(R.drawable.sort_asc);
}
break;
}
}
It could help you.
This is a wrapper for InqBarna's library.
This is a code snippet of how to achieve the sorting headers. Anyway you will have to see the whole example to understand how it works.
Here you render the header view:
public class OriginalHeaderCellViewGroup extends FrameLayout
implements
TableFixHeaderAdapter.HeaderBinder<ItemSortable> {
private Context context;
public TextView textView;
public ImageView iv_order_asc, iv_order_desc;
public OriginalHeaderCellViewGroup(Context context) {
super(context);
this.context = context;
init();
}
private void init() {
LayoutInflater.from(context).inflate(R.layout.text_sotable_view_group, this, true);
textView = (TextView) findViewById(R.id.tv_text);
iv_order_asc = (ImageView) findViewById(R.id.iv_order_asc);
iv_order_desc = (ImageView) findViewById(R.id.iv_order_desc);
}
#Override
public void bindHeader(ItemSortable item, int column) {
textView.setText(item.text.toUpperCase());
drawOrderArrows(item.order);
}
private void drawOrderArrows(int order) {
iv_order_desc.setImageResource(order == -1 ? R.drawable.ic_arrow_drop_up_24dp : R.drawable.ic_arrow_drop_up_24dp_disabled);
iv_order_asc.setImageResource(order == 1 ? R.drawable.ic_arrow_drop_down_24dp : R.drawable.ic_arrow_drop_down_24dp_disabled);
}
and here you attach the listener to the table adapter and perform the sorting:
private void setListeners(final OriginalSortableTableFixHeaderAdapter adapter) {
TableFixHeaderAdapter.ClickListener<ItemSortable, OriginalHeaderCellViewGroup> clickListenerHeader = new TableFixHeaderAdapter.ClickListener<ItemSortable, OriginalHeaderCellViewGroup>() {
#Override
public void onClickItem(ItemSortable item, OriginalHeaderCellViewGroup viewGroup, int row, int column) {
updateOderIndicators(item, column, adapter);
boolean orderAsc = item.order == 1;
boolean orderSectionsAsc =firstHeader.orderSectionsAsc;
applyOrder(orderAsc, orderSectionsAsc, column, adapter);
}
};
adapter.setClickListenerHeader(clickListenerHeader);
}
private void updateOderIndicators(ItemSortable item, final int column, TableFixHeaderAdapter adapter) {
final boolean orderAsc = item.order == 1;
firstHeader.order = 0;
for (ItemSortable itemAux : header) itemAux.order = 0;
item.order = !orderAsc ? 1 : -1;
}
private void applyOrder(final boolean orderAsc, final boolean orderSectionsAsc, final int column, TableFixHeaderAdapter adapter) {
Collections.sort(body, new Comparator<NexusWithImage>() {
#Override
public int compare(NexusWithImage nexus1, NexusWithImage nexus2) {
if (nexus1.isSection() || nexus2.isSection()) return 1;
else if (orderAsc)
return nexus1.data[column + 1].compareToIgnoreCase(nexus2.data[column + 1]);
else
return nexus2.data[column + 1].compareToIgnoreCase(nexus1.data[column + 1]);
}
});
adapter.setBody(body);
}
Hope it helps you.

getView in base adapter is not working

I'm trying to create base adapter for Favorites.class so I can display items in it.
The getView method is not being called and the Activity won't show any view.
BaseAdapter:
public class fYtAdapter extends BaseAdapter {
private List<SearchResult> mfVideoList = null;
private LayoutInflater mfLayoutInflater = null;
private Favorites fActivity = null;
public fYtAdapter(Favorites iActivity){
fActivity = iActivity;
mfLayoutInflater = LayoutInflater.from(fActivity);
}
public void setmVideoList(List<SearchResult> mVideoList){
this.mfVideoList = mVideoList;
}
#Override
public int getCount() {
return (mfVideoList == null)? (0):(mfVideoList.size());
}
#Override
public Object getItem(int position) {
return (mfVideoList != null && mfVideoList.size()>position)? (mfVideoList.get(position)):(null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
if(convertView != null){
mHolder = (ViewHolder)convertView.getTag();
}else{
mHolder = new ViewHolder();
convertView = mfLayoutInflater.inflate(R.layout.view_video_item,null);
mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);
convertView.setTag(mHolder);
}
//Setting the data
final SearchResult result = mfVideoList.get(position);
mHolder.mVideoTitle.setText(result.getSnippet().getTitle());
//Loading the image
Picasso.with(fActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
return convertView;
}
private class ViewHolder{
private TextView mVideoTitle = null;
private ImageView mVideoThumbnail = null;
}
}
Favorites.class
public class Favorites extends ActionBarActivity
implements View.OnClickListener, AdapterView.OnItemClickListener, ServerResponseListener{
private LayoutInflater mLayoutInflater = null;
private ProgressDialog mLoadingDialog = null;
private fYtAdapter mYtadapter = null;
String vidID = null, vidTitle = null, vidThumbnail = null;
ListView mFavLsv;
Bundle extras;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_layout);
getSupportActionBar().setTitle("Favorites");
initializeViews();
extras = getIntent().getExtras();
this.vidID = extras.getString("id");
this.vidTitle = extras.getString("title");
this.vidThumbnail = extras.getString("thumbnail");
}
public void initializeViews(){
mLayoutInflater = LayoutInflater.from(this);
mFavLsv = (ListView)findViewById(R.id.yt_fav_lst);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
//Not needed in this Activity
#Override
public void onClick(View v) {
}
//To start video
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this, AppConstants.KEY, vidID);
startActivity(videoIntent);
}
#Override
public void prepareRequest(Object... objects) {
//Parse the response based on type of request
Integer reqCode = (Integer) objects[0];
if(reqCode == null || reqCode == 0)
throw new NullPointerException("Request Code's value is Invalid.");
String dialogMsg = null;
switch(reqCode){
case SEARCH_VIDEO:
dialogMsg = SEARCH_VIDEO_MSG;
break;
}
if(mLoadingDialog != null && !mLoadingDialog.isShowing())
mLoadingDialog = ProgressDialog.show(this, DIALOG_TITLE, dialogMsg, true, true);
}
#Override
public void goBackground(Object... objects) {
}
#Override
public void completedRequest(Object... objects) {
//Dismiss the dialog
if(mLoadingDialog != null && mLoadingDialog.isShowing())
mLoadingDialog.dismiss();
//Parse the response based on type of request
Integer reqCode = (Integer) objects[0];
if(reqCode == null || reqCode == 0)
throw new NullPointerException("Request Code's value is Invalid.");
switch (reqCode){
case SEARCH_VIDEO:
if(mYtadapter == null){
mYtadapter = new fYtAdapter(this);
mYtadapter.setmVideoList((List<SearchResult>)objects[1]);
mFavLsv.setAdapter(mYtadapter);
}else{
mYtadapter.setmVideoList((List<SearchResult>) objects[1]);
mYtadapter.notifyDataSetChanged();
}
break;
}
}
}
I got another base adapter for another Activity and it is working fine.
I can't figure out why this is not working in here.
I need to display the view (with thumbnail(vidThumbnail) and title(vidTitle) in Favorites.class
*It seems that it won't got into "completedRequest" at all.
I faced this same issue. And I got the solution from here.. Link
Solution:
The only reasons getView() is not called are:
1) getCount() returns 0
2) You forget to call setListAdapter on the ListView.
3) If the ListView's visibility (or its container's visibility) is GONE.
Initialize your adapter and then set it to your ListView inside your initializeViews() method using mFavLsv.setAdapter(youradapter);
you have to add one more line -
mFavLsv.setAdapter(mYtadapter );
You missed set adapter in listview.

Android fragments and creating widgets dynamically

This is perhaps one of the simpler problems on here, but i am fairly new to android,so still having some challenges.I am downloading some data from a mysql server using JSON.-a list of questions.Each question is then created on its own fragment depending on the total number of questions.I display different widgets-edittext,radiobutton,spinner etc depending on what is specified for that question.
Problem 1
My challenge is that,i cannot seem to get the text values entered into/selected from these widgets.This works well in different activities of the application,but it is just this fragment class where i get issues.
Problem 2
I am also trying to get the positions of each question,from so that i can properly display the next/back buttons appropriately.e.g if i am at the 1st que,hide back button,if at the end,hide next and display done.something like that.My current implementation only works if i don't press button back to view a previous question.if i do,then button done is't displayed when i get to the last item,but it still shows 'next'
Problem 3
I also tried inserting checks for the input fields.e.g if user clicks next without entering anything,display toast and do not move to next item.But mine still goes to next page,and displays the toast msg "response is required"
Kindly help me see what i am doing wrong in my implementation.Thanks
My relevant code below
**fragmentStatePagerAdapter class**
public class PagerAdapter extends FragmentStatePagerAdapter {
public SQLiteDatabase db;
public PagerAdapter adapter;
Question question;
Context context2;
public PagerAdapter(Context c, FragmentManager fm) {
super(fm);
this.context2 = c;
}
#Override
public int getCount() {
System.out.println("inside get count");
return questions.size(); // get number of pages to be displayed
}
#Override
public Fragment getItem(int i) {
Log.i("at PagerAdapter", "yaaay!!At pager adapter");
Fragment fragment = null;
Question que = questions.get(i); //questions is the arraylist that has been populated with question objects from the server.
// pass value to be displayed in inflated view
fragment = new FragmentRadioButton(context2, que);
return fragment;
}
public CharSequence getPageTitle(int position) {
position++;
String s = "Question " + position;// setting the title
return s;
}
}
The Fragment class
public class FragmentRadioButton extends Fragment {
Context context3;
private Question question;
private RadioButton[] radioButton;
private RelativeLayout relativeLayout1;
static ViewPager mViewPager = FragmentStatePagerActivity.mViewPager;
private TextView question_txtView;
private RadioGroup radiogroup;
private Button btnNext;
private Spinner spinner;
private RatingBar ratingBar;
private Button btnBack;
private EditText editText;
private Button btnDone;
private TextView countRating_txt;
private RelativeLayout relativeLayout2;
private static final int radio_button = 1;
private static final int m_spinner = 2;
private static final int edit_text = 3;
private static final int rating_bar = 4;
public static ArrayList<Integer> num = new ArrayList<Integer>();
private List<RadioGroup> allRadioGroups = new ArrayList<RadioGroup>();
private List<Spinner> allSpinners = new ArrayList<Spinner>();
private List<EditText> allEds = new ArrayList<EditText>();
List<RatingBar> allRatingBars = new ArrayList<RatingBar>();
public FragmentRadioButton() {
}
public FragmentRadioButton(Context c, Question que) {
this.context3 = c;
this.question = que;
}
//#SuppressLint("NewApi")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = getActivity().getLayoutInflater().inflate(
R.layout.fragment_radio_button, null);
ScrollView scrollView = (ScrollView) view
.findViewById(R.id.scrollViewRadioButton);
relativeLayout1 = (RelativeLayout) scrollView
.findViewById(R.id.relativeLayout1);
relativeLayout2 = (RelativeLayout) relativeLayout1
.findViewById(R.id.relativeLayout2);
context3 = getActivity().getApplicationContext();
try {
question_txtView = (TextView) relativeLayout1
.findViewById(R.id.question_text_radio_button);
question_txtView.setText(question.getDescription());
num.add(question.getQuestionId());
if (question.getWidgetId() == radio_button) {
radiogroup.setVisibility(View.VISIBLE);
radioButton = new RadioButton[question.getAnswers().size()];
Typeface font4 = Typeface.createFromAsset(getActivity()
.getAssets(), "fonts/enriqueta/Enriqueta-Regular.otf");
for (int i = 0; i < question.getAnswers().size(); i++) {
radioButton[i] = new RadioButton(context3);
radioButton[i].setLines(3);
radioButton[i].setText(question.getAnswers().get(i)
.getAnswer_Text());
radioButton[i].setTag(question.getAnswers().get(i)
.getAnswer_Id());
radioButton[i].setTextColor(Color.BLACK);
radioButton[i].setTypeface(font4);
radioButton[i].setTextSize(30);
radiogroup.addView(radioButton[i]);
radiogroup.isClickable();
}
allRadioGroups.add(radiogroup);
} else if (question.getWidgetId() == m_spinner) {
spinner.setVisibility(View.VISIBLE);
List<String> list = new ArrayList<String>();
for (int b = 0; b < question.getAnswers().size(); b++) {
String data = question.getAnswers().get(b).getAnswer_Text()
.toString();
spinner.setTag(question.getAnswers().get(b).getAnswer_Id());
list.add(data);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
context3, R.layout.my_style, list) {
public View getView(int position, View convertView,
ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(22);
return v;
}
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View v = super.getDropDownView(position, convertView,
parent);
((TextView) v).setTextColor(Color.BLACK);
return v;
}
};
spinner.setAdapter(dataAdapter);
allSpinners.add(spinner);
} else if (question.getWidgetId() == edit_text) {
editText.setVisibility(View.VISIBLE);
allEds.add(editText);
} else if (question.getWidgetId() == rating_bar) {
ratingBar.setVisibility(View.VISIBLE);
countRating_txt.setVisibility(View.VISIBLE);
ratingBar
.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar rating_bar,
float rating, boolean fromUser) {
rating = rating_bar.getRating();
countRating_txt.setText(String.valueOf(rating)
+ "Ratings");
}
});
allRatingBars.add(ratingBar);
}
// }
} catch (Exception e) {
Log.e("Log error :",
"could not write the text views in radioButton fragment");
}
ItemPosition();
return view;
}
OnClickListener btnNextOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (question.getWidgetId() == radio_button) {
validatedRadioGroup();
} else if (question.getWidgetId() == m_spinner) {
validatedSpinner();
} else if (question.getWidgetId() == edit_text) {
validatedEditText();
} else if (question.getWidgetId() == rating_bar) {
validatedRatingBar();
}
mViewPager.setCurrentItem(getItem(+1), true);
}
};
OnClickListener btnBackOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
mViewPager.setCurrentItem(getItem(-1), true);
}
};
OnClickListener btnDoneOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int lastItem = question.getQuestionIds().size();
if (lastItem == question.getQuestionIds().size()) {
if (question.getWidgetId() == radio_button) {
radioText();
} else if (question.getWidgetId() == m_spinner) {
spinnerText();
} else if (question.getWidgetId() == edit_text) {
EditText_Value();
} else if (question.getWidgetId() == rating_bar) {
ratingText();
}
Intent a = new Intent(getActivity(), FinishedSurvey.class);
startActivity(a);
}
}
};
}
public void ItemPosition() {
for (int j = 0; j < num.size(); j++) {
// int item = num.indexOf(j);
if (j == 0) {
btnBack.setVisibility(View.GONE);
btnNext.setVisibility(View.VISIBLE);
btnDone.setVisibility(View.GONE);
System.out.println("the first part");
} else if (!(j == 0 || (j == question.getQuestionIds().size() - 1))) {
btnBack.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.VISIBLE);
btnDone.setVisibility(View.GONE);
System.out.println("the second part");
} else {
if (j == question.getQuestionIds().size() - 1) {
btnNext.setVisibility(View.INVISIBLE);
btnDone.setVisibility(View.VISIBLE);
System.out.println("the last part");
}
}
}
}

ListView does not render all items until interacted with

I have a very strange problem while using my ListView.
Only a part of my adapter items are renderd in the listview on screen but when I interact with the listview (ie tries to scroll it) all items are renderd properly.
This fenonemon only occurs if i have less items than the screen can show. Take a look at these screenshots below.
Before interaction:
After interaction:
Source code of activity where adding items:
String[] jRests = getResources().getStringArray(R.array.j_restaurants);
String[] lRests = getResources().getStringArray(R.array.l_restaurants);
items = new ArrayList<Object>();
items.add(getString(R.string.campus_j));
for(String item : jRests){
String[] val = item.split(",,,");
items.add(new FoodSectionListItem(new Restaurant(val[0], val[1], val[2], "")));
}
items.add(getString(R.string.campus_l));
for(String item : lRests){
String[] val = item.split(",,,");
items.add(new FoodSectionListItem(new Restaurant(val[0], val[1], val[2], "")));
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new BaseSectionAdapter(this, R.layout.list_item_fragment_header);
if(!isTabletView()){
adapter.setSelectedItem(-1);
}
adapter.setItems(items);
Code of adapter:
public class BaseSectionAdapter extends AmazingAdapter {
private LayoutInflater inflater;
private int selectedItem = 0;
private List<Object> items;
private List<SectionItem> sections = new ArrayList<SectionItem>(10);
private List<Class> itemTypes = new ArrayList<Class>();
private List<Integer> sectionPositions = new ArrayList<Integer>();
private int listHeaderLayoutId;
private View headerView;
public static interface ISectionListItem {
public void setProps(View convertView, int position, int selectedItem);
public View getLayout(LayoutInflater inflater);
}
private class SectionItem implements Serializable {
private static final long serialVersionUID = -8930010937740160935L;
String text;
int position;
public SectionItem(String text, int position) {
this.text = text;
this.position = position;
}
}
public BaseSectionAdapter(Context context, int listHeaderLayoutId) {
this.listHeaderLayoutId = listHeaderLayoutId;
init(context);
}
public BaseSectionAdapter(Context context, int listHeaderLayoutId, List<Object> listItems) {
this.listHeaderLayoutId = listHeaderLayoutId;
init(context);
initListItems(listItems);
}
private void init(Context context) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedItem(int position) {
selectedItem = position;
}
// public List<ListItem> getItems() {
// return items;
// }
private void initListItems(List<Object> itemList) {
int curSection = -1;
//int curPosition = 0;
//curSection = 0;
this.items = itemList;
itemTypes.clear();
sections.clear();
sectionPositions.clear();
int listSize = itemList.size();
for(int i = 0; i < listSize; i++){
Object currentItem = items.get(i);
if(currentItem instanceof String){
sections.add(new SectionItem((String) currentItem,i));
curSection++;
}
if(!itemTypes.contains(currentItem.getClass())){
itemTypes.add(currentItem.getClass());
}
sectionPositions.add(curSection);
}
Log.d("test", "No of items = "+items.size());
Log.d("test", "No of itemtypes = "+itemTypes.size());
Log.d("test", "View type count = "+getViewTypeCount());
}
public void setItems(List<Object> itemList) {
initListItems(itemList);
}
public int getCount() {
return items==null?0:items.size();
}
#Override
public int getViewTypeCount(){
return (itemTypes.size() == 0)?1:itemTypes.size();
}
#Override
public int getItemViewType(int position){
return itemTypes.indexOf(items.get(position).getClass());
}
#Override
public boolean isEnabled(int position){
return !(items.get(position) instanceof String || items.get(position) instanceof EmptySectionListItem);
}
#Override
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
#Override
protected void onNextPageRequested(int page) {
// TODO Auto-generated method stub
}
#Override
protected void bindSectionHeader(View view, int position,
boolean displaySectionHeader) {
// TextView lSectionTitle = (TextView) view
// .findViewById(R.id.txt_list_header);
// if (displaySectionHeader) {
// lSectionTitle.setVisibility(View.VISIBLE);
// lSectionTitle
// .setText(getSections()[getSectionForPosition(position)]);
// } else {
// lSectionTitle.setVisibility(View.GONE);
// }
}
#Override
public View getAmazingView(int position, View convertView, ViewGroup parent) {
Object curItemObject = items.get(position);
boolean isHeader = (curItemObject instanceof String);
if(convertView == null){
if(isHeader && headerView != null){
convertView = headerView;
}else if(isHeader){
convertView = inflater.inflate(listHeaderLayoutId, null);
headerView = convertView;
}else{
convertView = ((ISectionListItem) curItemObject).getLayout(inflater);
}
}
if(isHeader){
TextView header = ((TextView)convertView.findViewById(R.id.txt_list_header));
header.setText((String)curItemObject);
}else{
((ISectionListItem)curItemObject).setProps(convertView, position, selectedItem);
}
return convertView;
}
#Override
public void configurePinnedHeader(View header, int position, int alpha) {
TextView textView = ((TextView)header.findViewById(R.id.txt_list_header));
textView.setText(getSections()[getSectionForPosition(position)]);
}
#Override
public int getPositionForSection(int section) {
if(section >= sections.size()){
return 0;
}
return sections.get(section).position;
}
#Override
public int getSectionForPosition(int position) {
return sectionPositions.get(position);
}
#Override
public String[] getSections() {
String[] res = new String[sections.size()];
for (int i = 0; i < res.length; i++) {
res[i] = sections.get(i).text;
}
return res;
}
}
Code of layout:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout listLayout = new FrameLayout(this);
LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT);
listParams.weight = 1;
listLayout.setId(LIST_FRAGMENT_VIEW_ID);
FrameLayout detailLayout = new FrameLayout(this);
LinearLayout.LayoutParams detailParams = new LinearLayout.LayoutParams(0, FrameLayout.LayoutParams.MATCH_PARENT);
detailParams.weight = 2;
detailLayout.setId(DETAIL_FRAGMENT_VIEW_ID);
layout.addView(listLayout, listParams);
layout.addView(detailLayout, detailParams);
if(savedInstanceState == null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(listLayout.getId(), (Fragment) listFragment, TWO_PANEL_LIST_FRAGMENT_TAG);
ft.add(detailLayout.getId(), detailFragment);
ft.commit();
}
setContentView(layout);
try calling notifyDataSetChanged() in runOnUIThread() method like I have shown below and it will work like a charm. :)
runOnUiThread(new Runnable() {
#Override
public void run() {
messageAdapter.notifyDataSetChanged();
}
});
i dont know what causes the problem, but if you don't find a logical solution to it you could try something like this:
trigger an onTouchEvent() programmatically whenever you launch the ListView.
scroll down and back up programmatically as soon as the ListView is launched.
etc..
Add ListView widget to layout.xml and add content of list to that. Do not use FrameLayout as it probably is the cause of the problem. It is updating content after touch so the Layout it is on is no implementing the correct onCreate() setup as the ListView widget has.
Are you calling the method notifyDataSetChanged() on your adapter after adding new items? This causes the listview to refresh its view when the underlying dataset is changed.
If it still doesn't work, try notifyDataSetInvalidated() that causes the listview to redraw completely.
Solved it!!!
Problem was with the adapter trying to reuse the same section item. Not good!!!
Changed it to inflate the section item each time we hit a section!

Categories

Resources