I'm trying to populate a listview with data passed into the activity via a the intent that created it. The println statement you see confirms that the data is passed in correctly (i.e. the expected content is printed, meaning that the ArrayList referenced in the adapter is properly initialized). However, I keep getting a NullPointerException on the line
content.setText(Html.fromHtml(cmts.get(position).content));
There must be something wrong in the adapter - maybe in the getItem(), or perhaps my calls to cmts.get(position) isn't doing what I think it is, but at this point I can't figure it out.
public class CommentsView extends Activity {
ArrayList<Comment> cmts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments_view);
cmts = (ArrayList<Comment>) getIntent().getExtras().getSerializable("clist");
for (Comment c : cmts) {
System.out.println("CMTinCV: " + c.content);
}
ListView lv = (ListView)findViewById(R.id.commentsList);
CommentAdapter ca = new CommentAdapter();
lv.setAdapter(ca);
}
class CommentAdapter extends BaseAdapter {
public CommentAdapter(){
}
#Override
public int getCount() {
return cmts.size()-1;
}
#Override
public Object getItem(int position) {
return cmts.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.commentbox, null);
TextView content = (TextView)findViewById(R.id.commentText);
TextView author = (TextView)findViewById(R.id.commentAuthor);
TextView date = (TextView)findViewById(R.id.commentDate);
content.setText(Html.fromHtml(cmts.get(position).content));
author.setText(cmts.get(position).author.name);
date.setText(cmts.get(position).date);
}
return convertView;
}
}
}
You need to access the textview's in your getview method as below:
convertView.findViewById(R.id.commentText); access it like this.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.commentbox, null);
TextView content = (TextView)convertView.findViewById(R.id.commentText);
TextView author = (TextView)convertView.findViewById(R.id.commentAuthor);
TextView date = (TextView)convertView.findViewById(R.id.commentDate);
content.setText(Html.fromHtml(cmts.get(position).content));
author.setText(cmts.get(position).author.name);
date.setText(cmts.get(position).date);
}
return convertView;
}
Change your Adapter's constructor to this (if it's not an inner class for your activity) :
ArrayList<Comment> cmts;
public CommentAdapter(ArrayList<Comment> mComments){
this.cmts = mComments;
}
and these lines :
TextView content = (TextView)findViewById(R.id.commentText);
TextView author = (TextView)findViewById(R.id.commentAuthor);
TextView date = (TextView)findViewById(R.id.commentDate);
should be like :
TextView content = (TextView) convertView.findViewById(R.id.commentText);
TextView author = (TextView) convertView.findViewById(R.id.commentAuthor);
TextView date = (TextView) convertView.findViewById(R.id.commentDate);
Check if the variable is null or not first:
if(cmts.get(position) != null) {
content.setText(Html.fromHtml(cmts.get(position).content));
author.setText(cmts.get(position).author.name);
date.setText(cmts.get(position).date);
}
To create the adapter like this
CommentAdapter ca = new CommentAdapter(cmts);
And CommentAdapter class to create constructor like this
public CommentAdapter(ArrayList<Comment> cmts){
this.cmts = cmts;
}
And create local variable in CommentAdapter class
private ArrayList<Comment> cmts;
Related
I'm having a problem understanding how to finish this part of my code.
It's an app that searches a list of games with the help of an API.
Everything is working so far so good right now, but one final thing.
In the code, first of all I have a simple activity with an edit_text, a button and an empty list view that it is called "lv_listofgames".
Then, when I press the "search" button, I fill the "lv_listofgames" with a series of rows formed by an imageview, a listView called "list_item_text" and a button.
To this point everything is okay it seems.
Then I should just fill the "list_item_text" inside the "lv_listofgames" with the contents of an arraylist but I just can't make it happen. I tried in many ways but I'm stuck. I even tried using 2 adapters but the app crashed everytime or the "list_item_text" remained empty.
The arrayList is something like: [game_title='Name', release_date='date', platform=platform]
I seem so close to the solution but I just can't figure it out how to accomplish that. Im going crazy :(
tl;dr: problem: when I press the "search" button the arrayList content doesn't appear in the ListView "list_item_text".
Here is the code, tell me if something is wrong, thanks:
public class MainActovity extends AppCompatActivity {
EditText et_searchName;
Button btn_search;
ListView lv_listofgames;
ListView lv;
final GamesDataService gameDataService = new GamesDataService(MainActovity.this);
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
et_searchName = findViewById(R.id.et_searchName);
btn_search = findViewById(R.id.btn_search);
lv_listofgames= findViewById(R.id.lv_listofgames);
btn_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gameDataService.getGameName(et_searchName.getText().toString(), new GamesDataService.searchGamesResponse() {
#Override
public void onError(String message) {
Toast.makeText(MainActovity.this, "Error", Toast.LENGTH_SHORT).show();
}
#Override
public void onResponse(List<GamesReportModel> gamesReportModels) {
List<GamesReportModel> newName = gamesReportModels;
List<String> stringsList = new ArrayList<>(newName.size());
for (Object object : newName) {
stringsList.add(Objects.toString(object, null));
}
System.out.println("stringsList:" + stringsList);
lv = (ListView) findViewById(R.id.lv_listofnames);
MyListAdapter adapter = new MyListAdapter(MainActovity.this, R.layout.details, stringsList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
});
}
class MyListAdapter extends ArrayAdapter<String> {
private int layout;
public MyListAdapter(#NonNull Context context, int resource, #NonNull List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
MainActovity.ViewHolder mainViewHolder = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
MainActovity.ViewHolder viewHolder = new MainActovity.ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
viewHolder.title = (ListView) convertView.findViewById(R.id.list_item_text);
viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
convertView.setTag(viewHolder);
viewHolder.button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
}
else {
mainViewHolder = (MainActovity.ViewHolder) convertView.getTag();
}
return convertView;
}
}
public class ViewHolder {
ImageView thumbnail;
ListView title;
Button button;
}
}
GamesReportModel:
public class GamesReportModel {
private String game_title;
private String release_date;
private String platform;
public GamesReportModel(String game_title, String release_date, String platform) {
this.game_title = game_title;
this.release_date = release_date;
this.platform = platform;
}
public GamesReportModel() {
}
#Override
public String toString() {
return "game_title='" + game_title + '\'' +
", release_date='" + release_date + '\'' +
", platform=" + platform;
}
public String getGame_title() {
return game_title;
}
public void setGame_title(String game_title) {
this.game_title = game_title;
}
public String getRelease_date() {
return release_date;
}
public void setRelease_date(String release_date) {
this.release_date = release_date;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
There are two things you need to change in your code to get the desired effect.
In your row view layout (R.layout.details), replace the ListView with a TextView since you are just trying to show text for a given row (not a nested list inside each row). Then update the view holder to hold the correct view type as well
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
//...
public class ViewHolder {
ImageView thumbnail;
TextView title;
Button button;
}
In the adapter's getView method you have to actually set the text to show for that row. You never set the text to show anywhere, which is why your rows are blank. That should look like this:
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
MainActovity.ViewHolder mainViewHolder = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
MainActovity.ViewHolder viewHolder = new MainActovity.ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_text);
viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
convertView.setTag(viewHolder);
}
else {
mainViewHolder = (MainActovity.ViewHolder) convertView.getTag();
}
// Here, you need to set what values to show for this row - this
// is why your list is empty/blank
mainViewHolder.title.setText((String)getItem(position));
return convertView;
}
I was having some problem when trying to refresh the items in list view after the button was triggered. Here is how I populate the listview onCreate:
public class EventChat extends Fragment {
Context context;
View eventChat;
String userID, eventID;
private ListView listview;
public ArrayList<EventComment> _commentlist = new ArrayList<EventComment>();
TextView txtDisplayCommentBy, txtDisplayDateTime, txtDisplayCommentDesc,
txtEventChat;
Button btnChatSubmit;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
eventChat = inflater.inflate(R.layout.event_chat, container, false);
context = getActivity();
listview = (ListView) eventChat.findViewById(R.id.listview);
txtEventChat = (TextView) eventChat.findViewById(R.id.txtEventChat);
btnChatSubmit = (Button) eventChat.findViewById(R.id.btnChatSubmit);
btnChatSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onSubmitChatClicked();
}
});
Intent i = getActivity().getIntent();
_commentlist = (ArrayList<EventComment>) i
.getSerializableExtra("eventCommentObj");
Event eventModel = (Event) i.getSerializableExtra("eventObj");
userID = "Gab";
eventID = eventModel.getEventID();
listview.setAdapter(new ListAdapter(getActivity()));
return eventChat;
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public int getCount() {
return _commentlist.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.eventchat_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_dcommentBy = (TextView) convertView
.findViewById(R.id.txtDisplayCommentBy);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_dcommentBy.setText(_commentlist.get(position)
.getCommentBy().trim());
return convertView;
}
}
private class ViewHolder {
TextView txt_dcommentBy;
TextView txt_ddateTime;
TextView txt_dcommentDesc;
}
}
When my button was triggered and insert a new record into database, at the same time, I wanted the list view items to be refreshed:
public void onSubmitChatClicked() {
EventComment commentModel = new EventComment();
String currentDate = EventDateTime.getCurrentDate();
String currentTime = EventDateTime.getCurrentTime();
String commentDesc = String.valueOf(txtEventChat.getText());
commentModel.setCommentBy(userID);
commentModel.setEventID(eventID);
commentModel.setCommentDate(currentDate);
commentModel.setCommentTime(currentTime);
commentModel.setCommentDesc(commentDesc);
new CreateCommentAsyncTask(context).execute(commentModel);
txtEventChat.setText("");
}
However, it does not refresh. Any ideas?
Thanks in advance.
Well this looks pretty straightforward.
First make your adapter a class member, so you can access it later:
private ListAdapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
mAdapter = new ListAdapter(getActivity();
listview.setAdapter(mAdapter);
...
}
Then, when you submit the item, also add it to your ArrayList and update your adapter:
public void onSubmitChatClicked() {
EventComment commentModel = new EventComment();
String currentDate = EventDateTime.getCurrentDate();
String currentTime = EventDateTime.getCurrentTime();
String commentDesc = String.valueOf(txtEventChat.getText());
commentModel.setCommentBy(userID);
commentModel.setEventID(eventID);
commentModel.setCommentDate(currentDate);
commentModel.setCommentTime(currentTime);
commentModel.setCommentDesc(commentDesc);
// Add the new element to your DB
new CreateCommentAsyncTask(context).execute(commentModel);
// Add the new element to your current ArrayList
_commentlist.add(commentModel)
// Update theListView, by updating the adapter
mAdapter.notifyDataSetChanged();
txtEventChat.setText("");
}
EDIT:
Little more explanation:
When your fragment is created you are passed an array of EventComment items. I guess these are the elements from your DB. When you update the database, however, your ArrayList won't get updated, unless you reload the whole fragment. That's why you add the item to the DB, and to the list manually, and with notifyDataSetChanged, you force your adapter to update the ListView.
You cant do listview.setAdapter(new ListAdapter(getActivity())); to achieve this. You should create an instance of adapter and call adapter.add + adapter.notifyDataSetChanged
This is a very good tutorial :
http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html
I'm trying to inflate a list using baseadapter within an activity. The list just doesn't inflate. From the logs implemented within the class, the getView() function doesn't even execute. Here's the code. -
public class CallLog extends Activity {
ListView logList;
List mList;
Context mCtx;
ArrayList<String> logName;
ArrayList<String> logNumber;
ArrayList<String> logTime;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.reject_call_log);
mCtx = getApplicationContext();
ListView logList = (ListView) findViewById(R.id.log_list);
mList = new List(mCtx, R.layout.log_row);
logList.setAdapter(mList);
SharedPreferences savedLogName = PreferenceManager.getDefaultSharedPreferences(mCtx);
SharedPreferences savedLogNumber = PreferenceManager.getDefaultSharedPreferences(mCtx);
SharedPreferences savedLogTime = PreferenceManager.getDefaultSharedPreferences(mCtx);
try{
logName = new ArrayList(Arrays.asList(TextUtils.split(savedLogName.getString("logName", null), ",")));
logNumber = new ArrayList(Arrays.asList(TextUtils.split(savedLogNumber.getString("logNumber", null), ",")));
logTime = new ArrayList(Arrays.asList(TextUtils.split(savedLogTime.getString("logTime", null), ",")));
Collections.reverse(logName);
Collections.reverse(logNumber);
Collections.reverse(logTime);
}catch(NullPointerException e){
e.printStackTrace();
//TextView noLog = (TextView)findViewById(R.id.no_log);
}
}
public class List extends BaseAdapter {
LayoutInflater mInflater;
TextView nameText;
TextView numberText;
TextView timeText;
int timePos = 1;
public List(Context context, int resource) {
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
v = mInflater.inflate(R.layout.row, null);
}
nameText = (TextView) v.findViewById(R.id.log_name);
numberText = (TextView) v.findViewById(R.id.log_number);
timeText = (TextView) v.findViewById(R.id.log_time);
nameText.setText(logName.get(position));
numberText.setText(logNumber.get(position));
timeText.setText(logTime.get(timePos) + logTime.get(timePos+1));
Log.d("RejectCall", "ListView");
timePos+=2;
return v;
}
}
}
Where is it all going wrong? Also, is there a better way to do what I'm trying to do?
Please replace the following code :
#Override
public int getCount() {
return 0;
}
with
#Override
public int getCount() {
return logName.size();
}
As list view only show the numbers of rows that is returned by this method and right now you are returning 0;
And after fetching the data in arraylist please use adapter.notifyDataSetChanged() to notify the list view.
You have to call notifyDataSetChanged() as you are filling data in array list after setting the adapter. so to notify the list view that data has been changed you have to call notify method(as above)
Your getItem() and getCount() haven't been implemented. If you want any kind of adapter to work for the list, these need to be implemented. Your list is also not holding any actual data, so getItem() has nothing to set.
Don't forget to call notifiyDataSetChanged() in your adapter after you set appropriate implementations for the above two functions.
I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from "Accept" to "Accepted". But when i click it on first listview item, another textview listview items below are changing.
Here's some photos to descibe you better
this is the adapter class
public class CustomListAdapter extends BaseAdapter {
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.sex);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("Accepted");
}
}
);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
return convertView;
}
static class ViewHolder {
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
}
}
Remember that views can be recycled via convertView.
In your onClick method you set the approve text to "Accepted" but when the view is recycled, you never set it back to "Accept"
Actually you need to update (something in) the list in response to an click and have the Accept/Accepted value toggle based on that value rather than simply updating what is currently visible on the screen.
-- to answer the "how" question (asked below)--
Add a new field to ViewHolder
static class ViewHolder {
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
Change the onClick method:
public void onClick(View argView)
{
// note that holder no longer needs to be final in the parent class
// because it is not used here.
View parent = (View)argView.getParent();
ViewHolder clickedHolder = (ViewHolder)parent.getTag();
clickedHolder .newsItem.setAccepted(true); /// a new method
clickedHolder .approve.setText ("Accepted");
Log.d(TAG, "Accepted item #" + position);
}
After you have convertView created (if necessary)
FeedItem newsItem = (FeedItem) listData.get(position);
holder.newsItem = newsItem; // populate the new field.
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
if(newsItem.isAccepted ()){ // another new method!
holder.approve.setText ("Accepted");
Log.d(TAG, "Set text to Accepted for item #" + position);
}else{
holder.approve.setText("Accept");
Log.d(TAG, "Set text to Accept for item #" + position);
}
Once it is working you should consider removing the Log.d() lines to cut down on the noise in LogCat.
I have a ListView in which I display some products. I use an object that extends the BaseAdapter class to populate the ListView, more exacly using the getView(..) method. I have a TextView "link" on every itemView on which if the user taps it will go to a web page. In my base adapter I set a listener on the TextView only if my product contains a link.
I have done debugging in my getView(..) method and it all works just fine, but after it exits the getView method, if there is an item that doesn`t have a link it will take the link/listener from another item from the listView.
Adapter Class:
public class MatchListBaseAdapter extends BaseAdapter {
private static ArrayList<Match> matchesArrayList;
private LayoutInflater l_Inflater;
private OnClickListener onClickListener;
public MatchListBaseAdapter(Context context, View.OnClickListener listener, ArrayList<Match> results,Activity a) {
matchesArrayList = results;
onClickListener = listener;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return matchesArrayList.size();
}
public Object getItem(int position) {
return matchesArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.itemlist_match, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.oferNameMLI2);
holder.expireDate = (TextView) convertView.findViewById(R.id.expireDateMLI);
holder.price = (TextView) convertView.findViewById(R.id.priceMLI);
holder.companyName = (TextView) convertView.findViewById(R.id.compNameMLI);
holder.productImage = (ImageView) convertView.findViewById(R.id.productImageMLI);
holder.companyImage = (ImageView) convertView.findViewById(R.id.companyImageMLI);
holder.description = (TextView) convertView.findViewById(R.id.moreDetailsMLI);
holder.digitalySigned = (ImageView) convertView.findViewById(R.id.digitalSignatureImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//populating the holder.. doesn`t have any relevance..
if(matchesArrayList.get(position).getCompanyLink() != null){
holder.companyImage.setOnClickListener(onClickListener);
holder.companyImage.setTag(position);
}
return convertView;
}
static class ViewHolder {
TextView name;
TextView expireDate;
TextView price;
TextView companyName;
TextView description;
ImageView productImage;
ImageView companyImage;
ImageView digitalySigned;
}
}
Activity onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
matches = DataManager.getInstance().getListSubscription().get(DataManager.getInstance().getSubscriptionPosition()).getMatchesList();
ListView lv1 = (ListView) findViewById(R.id.listView_layout);
DataManager.getInstance().setAdapterMatch(new MatchListBaseAdapter(this, this, matches,this));
lv1.setAdapter(DataManager.getInstance().getAdapterMatch());
}
Just want to mention once again that i ve done debugging in the getView(..) method and it was ok, the flow was the right one, but after the items in the listView that doesn't supposed to have a listener on the TextView it had from the other items.
Also this happens always for the first item in the listView .. and it is populated with the link from the last item in the listView that contains a link.
I've searched a lot for this issue but didn't find anything relevant, but i think that there is a problem on my convertView, but i can`t figure it out..
Thx a lot
The problem is that when you're reusing the view and not setting an explicit onClickListener to it, it still contains the old listener from another product - from the view that was reused. Try making a change as following:
if(matchesArrayList.get(position).getCompanyLink() != null){
holder.companyImage.setOnClickListener(onClickListener);
holder.companyImage.setTag(position);
}
else {
holder.companyImage.setOnClickListener(null);
}