This is example from Android for beginners book. I think that problem is with mNoteAdapter but I can't find where it exactly is. I tried to use adapter from other source and it was working(I mean click) so I could see at least log in logcat. How does mNoteAdapter affect on possibility of seeing users click by app? How can I find where problem is?
mNoteAdapter = new NoteAdapter();
ListView listNote = (ListView) findViewById(R.id.listView);
listNote.setAdapter(mNoteAdapter);
listNote.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View view, int
whichItem, long id) {
Log.e("CLICK", "CliCK");
}
});
}
Here is a piece of NoteAdapter class
public class NoteAdapter extends BaseAdapter {
List<Note> noteList = new ArrayList<Note>();
#Override
public int getCount() {
}
#Override
public Note getItem(int whichItem) {
}
#Override
public long getItemId(int whichItem) {
}
#Override
public View getView(int whichItem, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listitem, viewGroup, false);
}
TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
ImageView ivImportant = (ImageView) view.findViewById(R.id.imageViewImportant);
Note tempNote = noteList.get(whichItem);
if (!tempNote.isImportant()) {
ivImportant.setVisibility(View.GONE);
}
txtTitle.setText(tempNote.getTitle());
return view;
}
public void addNote(Note n) {
}
}
There are many ways to display items as list on Android..
--> ListView, RecyclerView etc.
Lists need a source right? Let's assume that your source is an array of 1000 elements. Your ListView has to have as many columns to display all of them right?
Adapter is responsible for taking the array, and assigning each element to every list placeholders
This row was added to the top of .xml file
android:descendantFocusability="blocksDescendants"
Related
I am using a view pager with three tabs (three fragments). In one of these fragments I have a recycler view. The items in this recycler view get updated every 2 seconds from the web. When I first start up the app, the recycler view runs just fine. Even when navigating to different tabs or navigating out of my app with home button, it all works.
However when I close the app by using the backkey and then go into my app again, the recycler view is not updating anymore. It shows the status that it had when the app quit. I monitor the adapter via the console and it keeps on working with the correct data, only the recycler view doesn't show this. I tried all kinds of stuff to "reconnect" adapter and recycler view but it won't work. I am having this issue for days. Any idea for the cause of this problem? See the relevant code for troubleshooting. Thank you!
public class UserAreaFragment extends Fragment implements PopupMenu.OnMenuItemClickListener {
private RecyclerView mRecyclerview;
private UserAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutmanager;
private Handler mainHandler = new Handler();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_user_area, container, false);
...
mAdapter = new UserAdapter(getActivity(), UserDataSingleton.getUserList());
mRecyclerview = root.findViewById(R.id.userListing);
mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerview.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new UserAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
...
}
});
}
//Somewhere in my method that receives the data from an online DB:
UserDataSingleton.getUserList().clear();
UserDataSingleton.getUserList().addAll(serverResponse);
mainHandler.post(updateUi);
//And finally the updateUi method: this is essential just sorting and then notifydatasetchanged
Runnable updateUi = new Runnable() {
#Override
public void run() {
Collections.sort(UserDataSingleton.getUserList(), new Comparator<UserItem>() {
#Override
public int compare(UserItem lhs, UserItem rhs) {
// -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
return Double.parseDouble(lhs.getmDistance()) > Double.parseDouble(rhs.getmDistance()) ? 1 : Double.parseDouble(lhs.getmDistance()) < Double.parseDouble(rhs.getmDistance()) ? -1 : 0;
}
});
mAdapter.notifyDataSetChanged();
}
};
//and this is my Adapter:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
private UserAdapter.OnItemClickListener mListener;
private Context mContext;
private List<UserItem> mUserlist;
public UserAdapter(Context context,List<UserItem> userList){
mUserlist=userList;
mContext = context;
}
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(UserAdapter.OnItemClickListener listener) {
this.mListener = listener;
}
public static class UserViewHolder extends RecyclerView.ViewHolder{
public TextView mUsername;
public TextView mDistance;
public ImageView userIcon;
public UserViewHolder(#NonNull View itemView, final UserAdapter.OnItemClickListener listener) {
super(itemView);
mUsername = itemView.findViewById(R.id.tvNearUsername);
mDistance = itemView.findViewById(R.id.tvDistance);
userIcon = itemView.findViewById(R.id.usericon);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position = getAdapterPosition();
if (position!= RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});
}
}
#NonNull
#Override
public UserAdapter.UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View v= inflater.inflate(R.layout.user_item, parent, false);
UserViewHolder uvh= new UserViewHolder(v,mListener);
return uvh;
}
#Override
public void onBindViewHolder(#NonNull UserAdapter.UserViewHolder holder, int position) {
UserItem currentitem = mUserlist.get(position);
holder.mUsername.setText(currentitem.getmNearUsername());
if (currentitem.isArea()){
holder.mDistance.setVisibility(View.INVISIBLE);
holder.userIcon.setImageResource(R.drawable.ic_placeicon);
}else{
holder.mDistance.setVisibility(View.VISIBLE);
}
int distToNextTen = ((Integer.parseInt(currentitem.getmDistance())+5)/10)*10+10;
holder.mDistance.setText("< "+distToNextTen+"m");
}
#Override
public int getItemCount() {
return mUserlist.size();
}
}
I tried to only display the lines that affect the issue for better readability. If you need to see more code just let me know. Thankful for this great community!
(I think problem might be in connection with the main Handler not pointing to the correct view or maybe an issue with adapter-recyclerview connection but I can't find a solution tried so many things already)
Ok I found the problem. It had something to do with my runnables and handlers not being declared within onCreate. Somehow that messed it up. I made a major reconstruction of my code to solve it so its hard to tell which line exactly was the problem but if you are facing a similar problem check that your runOnUi and handler declarations happen in the right places.
How can I set two text blocks in the listView, of which the first is on the left, the other on the right? I am tried to create a new layout with two textViews. But I don't know how I can connect textViews with listView and how I can set texts on textViews. May anybody help me?
I would like to have a list like this
Assuming you have the list and your layout with those 2 textview is ready just use this adapter and set this adapter to recycler view of the activity. let me know if you face any issues
public class CountryCodeAdapter extends RecyclerView.Adapter<CountryCodeAdapter.ViewHolder> {
private CountryCodeActivity activity;
ArrayList<CountryCodeModel> list = new ArrayList<>();
int selected_pos = 0;
public CountryCodeAdapter(CountryCodeActivity activity, ArrayList<CountryCodeModel> list) {
this.activity = activity;
this.list = list;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_country_listing, parent, false);
return new ViewHolder(rootView);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
holder.tv_row_CountryCodeActivity_countrycode.setText(list.get(holder.getAdapterPosition()).getDial());
holder.tv_row_CountryCodeActivity_countryname.setText(list.get(holder.getAdapterPosition()).getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("country_code", list.get(holder.getAdapterPosition()).getDial());
activity.setResult(activity.RESULT_OK, intent);
activity.finish();
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tv_row_CountryCodeActivity_countryname,
tv_row_CountryCodeActivity_countrycode;
public ViewHolder(View itemView) {
super(itemView);
tv_row_CountryCodeActivity_countryname = itemView.findViewById(R.id.tv_row_CountryCodeActivity_countryname);
tv_row_CountryCodeActivity_countrycode = itemView.findViewById(R.id.tv_row_CountryCodeActivity_countrycode);
tv_row_CountryCodeActivity_countryname.setTypeface(AppClass.Lato_Regular);
tv_row_CountryCodeActivity_countrycode.setTypeface(AppClass.Lato_Regular);
}
}
}
1)You should use RecyclerView insteaad of listView, and for recyclerView you can achieve what you want with item decorator.
2)But if you have to use ListView(which is less possible case) you can do this by checking list item position and set the corresponding margin to the layout which is not recomended.
3)Also there is another way to achieve this, which is to use different layout resource xml files, but I would not use the last two variants. I would prefer the first.
I have created a listview. Each item in list view has two UI elements. One is a textview and other is a number picker. Now the issue is that if i click on first number picker to change value, the fourth one also changes and vice versa. Here is my getview function
private class ViewHolder {
public TextView name;
public NumberPicker numberPicker;
public CustomListener listener;
}
public View getView(final int position, #Nullable View convertView, #NonNull final ViewGroup parent) {
ViewHolder holder;
View listItem = convertView;
currentCell=getItem(position);
currentCell.setPosition(position);
if (listItem == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
listItem = inflater.inflate(R.layout.organ_item, parent, false);
}
holder = new ViewHolder();
holder.name = (TextView) listItem.findViewById(R.id.organName);
holder.numberPicker = (NumberPicker)
listItem.findViewById(R.id.numberPicker);
holder.numberPicker.setMinValue(1);
holder.numberPicker.setMaxValue(10);
holder.numberPicker.setOnValueChangedListener(holder.listener);
holder.numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
currentCell=getItem(position);
View parentRow = (View) picker.getParent();
ListView mListView=(ListView)parentRow.getParent().getParent();
ConstraintLayout constraintLayoutView = (ConstraintLayout) mListView.getChildAt(currentCell.getPosition());
RelativeLayout relativeLayout = (RelativeLayout)constraintLayoutView.getChildAt(0);
NumberPicker p = (NumberPicker) relativeLayout.getChildAt(1);
if(position==currentCell.getPosition())
{
p.setValue(newVal);
}
else
{
p.setValue(oldVal);
}
}
});
//Set the name
TextView organName = (TextView)listItem.findViewById(R.id.organName);
organName.setText(QuickMeditationScreenInfo.getInstance().getScreenNameFromIndex(currentCell.getOrgan()));
return listItem;
}
Also even if i comment out the onValueChangeListener even then the same behaviour occurs which i assume is the default behaviour of number picker in a list. I have spent multiple hours on it but couldn't figure out the solution. I have also debugged the code and when i change a value, the debugger comes into the onValueChange code only once.
You need set numberpicker default value every time
holder = new ViewHolder();
holder.name = (TextView) listItem.findViewById(R.id.organName);
holder.numberPicker = (NumberPicker) ;
holder.numberPicker.setValue(defaultValue);//like this
Try to handle click event into adapter using interface like below
for example make one interface into adapter class ..
private onItemClick onItemClick;
public void setOnItemClick(DisplayAllData.onItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface onItemClick{
void onItemSelected(int position); // pass your data
}
In getView() method like number listner called all logical code into activity or fragment.
holder.mTvName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClick.onItemSelected(position);
}
});
In activity or fragment after adapter set into listview or recyclerview then
adapter not null then called below code..
allDataAdapter.setOnItemClick(new DisplayAllData.onItemClick() {
#Override
public void onItemSelected(int position) {
// here called all logical part
allDataAdapter.notifyDataSetChanged();
}
});
I am using GridView in my alert dialogue with below codes.its giving me error called in title. My codes and more required information is like below.
private void showGotoPageDialog() {
final Dialog mDialog = new Dialog(getActivity());
mDialog.setContentView(R.layout.grid_dialogue);
mDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
GridView mGridView = (GridView) mDialog.findViewById(R.id.grid_dialog);
ArrayList<String> tmp = new ArrayList<>(mPageOptions.length);
for(int i = 0; i < mPageOptions.length; i++)
{
tmp.add(mPageOptions[i].split(" ")[1]);
}
CustomAdapter adapter = new CustomAdapter(getActivity(), tmp, mPageIndx);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, tmp);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int item, long l) {
mDialog.dismiss();
mPageIndx = item + 1;
updateQuotesListServer();
updatePageInfo();
}
});
mDialog.show();
TextView dismiss = (TextView) mDialog.findViewById(R.id.dialog_dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDialog.dismiss();
}
});
}
and Adapter like this
private class CustomAdapter extends BaseAdapter
{
private ArrayList<String> list;
private LayoutInflater inflater;
private int currentPage;
CustomAdapter(Activity activity, ArrayList<String> list, int currentPage)
{
this.list = new ArrayList<>();
this.list.addAll(list);
inflater = (LayoutInflater) activity.getSystemService(AppCompatActivity.LAYOUT_INFLATER_SERVICE);
this.currentPage = currentPage;
}
public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = View.inflate(getActivity(),R.layout.dialogue_item, null);
TextView txt = (TextView) view.findViewById(R.id.textview_dialogue);
txt.setText(list.get(i));
if(i == currentPage-1)
{
txt.setTextColor(Color.BLACK);
}
return view;
}
}
its giving me warning on this line
view = View.inflate(getActivity(),R.layout.dialogue_item, null);
warning is like below
Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling.
My code is working fine as expected and I am not facing any issue. I want know that What changes require for fix this and what is benefit of it ? Let me know if someone can help me for come out from this and solve my doubt.
Thanks
View adapter is built so that instead of creating a new view for each item, views that scroll off screen can be reused (they are passed in via the 2nd argument of getView). You are being warned that you should try to reuse these views instead of always inflating new ones. Change the line to
if (view == null) view = View.inflate(getActivity(),R.layout.dialogue_item, null);
Try this instead
view = View.inflate(R.layout.dialogue_item, viewGroup, null);
If not necessary, in other words if view is not null, then it is not necessary to call the following line in getView() method :
view = inflter.inflate(R.layout.spinner_rect, null);
the problem arises the same wrong source code available on the web:
https://abhiandroid.com/ui/custom-spinner-examples.html
https://demonuts.com/android-custom-spinner-image/
apply my solution for correctness
Thanks for useful answers: I had the same error but I was using view binding. Resolved using this approach.
I used fragment in my app and i'm using SQLite to save local data. But when I finished saving data, and I swipe the page, my listView is not refreshed with new data (Only showing old data). I have tried to provide a method notifyDataSetChanged() on my adapter, but it's not working.
My Base Adapter class :
public class LocalDataAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<LocalDataBean> data;
private static LayoutInflater inflater = null;
public LocalDataAdapter(Activity a, ArrayList<LocalDataBean> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public void setItem(ArrayList<LocalDataBean> data){
this.data = data;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = inflater.inflate(R.layout.list_item, null);
TextView nama_konsumen = (TextView) v.findViewById(R.id.nama_konsumen);
TextView no_telp = (TextView) v.findViewById(R.id.no_telp);
TextView no_hp_cdma = (TextView) v.findViewById(R.id.no_hp_cdma);
TextView no_hp_gsm = (TextView) v.findViewById(R.id.no_hp_gsm);
LocalDataBean obj = (LocalDataBean) getItem(position);
nama_konsumen.setText(obj.getNamaKonsumen());
no_telp.setText(obj.getNoTelp());
no_hp_cdma.setText(obj.getNoCMDA());
no_hp_gsm.setText(obj.getNoGSM());
return v;
}
}
My fragment class :
public class LocalDataFragment extends Fragment {
View view;
Activity act;
SQLHandlerBean utilSql;
ArrayList<LocalDataBean> localdatabean = new ArrayList<LocalDataBean>();
LocalDataAdapter adapter;
ListView list;
public static final String TAG = LocalDataFragment.class.getSimpleName();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_local_data, null);
act = getActivity();
list = (ListView) view.findViewById(R.id.listViewLocalData);
utilSql = new SQLHandlerBean(this.act);
adapter = new LocalDataAdapter(act, localdatabean);
localdatabean = new ArrayList<LocalDataBean>();
list.setAdapter(adapter);
if (utilSql.ReadAllLocalData().size() < 1) {
Toast.makeText(act, "DATA EMPTY!", Toast.LENGTH_LONG).show();
} else {
localdatabean = utilSql.ReadAllLocalData();
Log.e(TAG, "TOTAL DATA : "+localdatabean.size());
adapter.setItem(localdatabean);
adapter.notifyDataSetChanged();
}
return view;
}
}
Is adapter.notifyDataSetChanged() placement correct?
No, the placement is not in the right place.
As you have placed the notifyDataSetChanged() inside of the onCreateView() method. It will be only invoked 1st time the fragment is launched.
Rather you can add a refresh button in your layout (or in you action bar). And along with the insertion/deletion method of the data, place the notifyDataSetChanged() at the bottom of the click event of that button.
By doing this user can refresh the page whenever they want.
And if you want to refresh the page by swipping the view then, SwipeRefreshLaoyout could be a perfect alternative.
You can check this blog.