I am trying to add an xml layout programmatically. It is giving error at time.setText(timeFormat); When I remove this line I get the default text value of TextView. Also I have checked that timeFormat is not null. Why am I getting this error?
Here's my code:
In onCreate(),
new GetContacts().execute();
addPost(timeFormat,message);
addPost() function:
public void addPost(String timef,String postmessage){
LayoutInflater i = getLayoutInflater();
View v = i.inflate(R.layout.list_item, null);
TextView time = (TextView)findViewById(R.id.time);
Log.d("TIME HAS COME", timeFormat);
time.setText(timeFormat);
TextView message1 = (TextView)findViewById(R.id.message);
message1.setText(message);
LinearLayout ll = (LinearLayout)findViewById(R.id.feedsLayout);
ll.addView(v);
}
Related
So I have a messaging app where I read my database and render all the messages programatically. The thing is, when I am inside a different layout, I need to inflate those messages and edit them at will. For example,
val myView = inflater.inflate(R.layout.female_messaging_fragment, container, false)
...
val convLayout = myView.findViewById<LinearLayout>(R.id.conversations_layout)
val profileLayout = LinearLayout(context)
val recentMessage = TextView(context)
recentMessage.id = 5
val recentParams = LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)
recentParams.setMargins(10, 0, 0, 0)
recentMessage.layoutParams = recentParams
recentMessage.textSize = 16f
if (didIsendLastMessage) {
val arrowIcon = ImageView(context)
Picasso.get().load(R.drawable.right_arrow).resize(50, 50).into(arrowIcon)
val imageParams = LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)
imageParams.gravity = Gravity.CENTER
arrowIcon.layoutParams = imageParams
recentMessage.setText(mostRecentMessage)
arrowAndMessageLayout.addView(arrowIcon)
arrowAndMessageLayout.addView(recentMessage)
} else {
recentMessage.setText(mostRecentMessage)
arrowAndMessageLayout.addView(recentMessage)
}
nameLastmessageLayout.addView(arrowAndMessageLayout)
// Add namne and message to profile
profileLayout.addView(nameLastmessageLayout)
convLayout.addView(profileLayout)
And then in a different layout...
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.female_messaging_fragment, null);
TextView text = (TextView) v.findViewById(5);
text.setText("Something");
The thing is, text is always null, and I believe it is because I added the views programatically. What else can I do here? Thanks!
inflating female_messaging_fragment will return fresh new Layout view object of your xml that might be blank layout without any TextViews.
To get TextView object you need the object of Layout in which you had added the views.
For example as per your code you added recentMessage in arrowAndMessageLayout so to get the object of recentMessage you need the object of arrowAndMessageLayout and can get like below -
TextView text = arrowAndMessageLayout.findViewById(5);
OR
TextView text = convLayout.findViewById(5);
Inflating again will return fresh new container without your textview.
Hope this will help you.
I'm attempting to null check an EditText (which should be simple enough) however each time I enter text into my two ET's - they always return as empty: "" and they should not (if they have text in them).
I've looked over several other SO articles related to this and none of the fixes seem to resolve the issue.
Can anyone spot what I might be doing wrong in this instance?
Source Snippet:
Java class:
...
public void doneClicked(View v) throws JSONException {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v1 = li.inflate(R.layout.profile_notification_settings_list_edit, null);
final EditText label = (EditText) v1.findViewById(R.id.emaillabel);
final EditText value = (EditText) v1.findViewById(R.id.emailvalue);
String sValue = value.getText().toString();
if (sValue.matches("")) {
Toast.makeText(this, "You did not enter a value", Toast.LENGTH_SHORT).show();
return;
}
String sLabel = label.getText().toString();
if (sLabel.matches("")) {
Toast.makeText(this, "You did not enter a label", Toast.LENGTH_SHORT).show();
return;
}
...
Adapter:
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.profile_notification_settings_list_edit, null);
EditText value = (EditText) view.findViewById(R.id.emailvalue);
value.setHint("Value");
value.setText(valueList[i]);
EditText label = (EditText) view.findViewById(R.id.emaillabel);
if (labelList != null)
label.setHint("Label");
label.setText(labelList[i]);
Full source:
Java Class:
https://pastebin.com/y1e9J1yE
Adapter:
https://pastebin.com/Zqh3eCAk
Because everytime you click you are creating a new view which has nothing to do with the views currently being displayed on the screen
public void doneClicked(View v) throws JSONException {
// don't inflate new views
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v1 = li.inflate(R.layout.profile_notification_settings_list_edit, null);
final EditText label = (EditText) v1.findViewById(R.id.emaillabel);
final EditText value = (EditText) v1.findViewById(R.id.emailvalue);
// ... code
Note : you cannot directly fetch the data from rowItemVies from ListView , so you need to add method in your adapter class to get the selected position through which you can fetch the rowView using yourListView.getChildAt(position) and further fetch data from children in rowView
use
sValue.trim().equals("") instead of sValue.matches("")
I have created a dynamic view which repeats for a specific times. The view has 2 textViews and 2 radio buttons with default values. I want to change the Text values from program but getting nullpointer exception, Here is my code:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout scrollViewLinearlayout = (LinearLayout)findViewById(R.id.parent); // The layout inside scroll view
//int count=50;
for(int i = 0; i < Studentlist.length; i++){
String data = Studentlist[i];
String RollNum = data.split("--")[0];
String Name = data.split("--")[1];
LinearLayout layout2 = new LinearLayout(context);
layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
View item = inflater.inflate(R.layout.item_layout, null, false);
layout2.addView(item);
layout2.setId(i);
TextView trollnum = (TextView)findViewById(R.id.RollNum);
trollnum.setText(RollNum);
TextView tname = (TextView)findViewById(R.id.Name);
tname.setText(Name);
scrollViewLinearlayout.addView(layout2);
Log.d("Id is: ", trollnum.getText().toString());
I am getting nullpointer at 'trollnum.setText(RollNum);'
How do i change the text in the View?
You are trying to find the view in the activity's layout where it doesn't exist instead of finding it in the layout you just inflated.
Replace
TextView trollnum = (TextView)findViewById(R.id.RollNum);
TextView tname = (TextView)findViewById(R.id.Name);
With
TextView trollnum = (TextView) item.findViewById(R.id.RollNum);
TextView tname = (TextView) item.findViewById(R.id.Name);
I created the following CursorAdapter which shows messages from my SQL database, everything is added well until I scroll the list, I know that the objects are recycled, but in a wrong way. Here is my CursorAdapter class:
public class ChatAdapter extends CursorAdapter {
public ChatAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.chat_item, parent,
false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView left = (TextView) view.findViewById(R.id.lefttext);
TextView right = (TextView) view.findViewById(R.id.righttext);
LinearLayout rightBubble = (LinearLayout) view
.findViewById(R.id.right_bubble);
LinearLayout leftBubble = (LinearLayout) view
.findViewById(R.id.left_bubble);
TextView leftDate = (TextView) view.findViewById(R.id.leftdate);
TextView rightDate = (TextView) view.findViewById(R.id.rightdate);
// Extract properties from cursor
String from = cursor.getString(cursor.getColumnIndexOrThrow("from"));
String txt = cursor.getString(cursor.getColumnIndexOrThrow("message"));
String date = cursor.getString(cursor.getColumnIndexOrThrow("t"));
String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
// Parse time
long datevalue = Long.valueOf(date) * 1000;
Date dateformat = new java.util.Date(datevalue);
String convert = new SimpleDateFormat("HH:mm").format(dateformat);
// Populate fields with extracted properties
if (from.equals("me")) {
right.setText(txt);
left.setText("");
rightBubble
.setBackgroundResource(R.drawable.balloon_outgoing_normal);
leftBubble.setBackgroundDrawable(null);
rightDate.setText(convert);
leftDate.setVisibility(View.GONE);
}
else {
left.setText(txt);
right.setText("");
leftBubble
.setBackgroundResource(R.drawable.balloon_incoming_normal);
rightBubble.setBackgroundDrawable(null);
leftDate.setText(convert);
rightDate.setVisibility(View.GONE);
}
}
}
Unfortenately, after scrolling the list, dates from the rightDate and leftDate dissapears after moving back. I think it't due the .setVisibility(View.GONE)
Any suggestions to fix this?
when the view is recycled, it is in the previous state, android did not clear the status for you.
To fix your problem, you have to set the view in question to VISIBLE when needed
Edit:
like this, add the 2 lines
if (from.equals("me")) {
// your original code
rightDate.setVisibility(View.VISIBLE); //add this
}
else {
// your original code
leftDate.setVisibility(View.VISIBLE); //add this
}
I get a null pointer exception at line five/seven of my code:
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.footer_row, (ViewGroup) findViewById(R.id.header_layout_root));
TextView tv_footer = (TextView) findViewById(R.id.tv_footertext);
if(getListView().getCount()<=8) {
tv_footer.setText("You have no more assignments");
} else {
tv_footer.setText(Integer.toString(getListView().getCount()) + " assignments shown");
}
getListView().addFooterView(header, null, false);
I'm not too sure why, so could you tell me? List view's aren't my thing.
I'll tick the right answer!
Instead of using the findViewById() method from your Activity's View you should assign (which I assume is the footer TextView in your R.layout.footer_row) the TextView from the inflated View.
Sample code:
TextView tv_footer = (TextView) header.findViewById(R.id.tv_footertext);
Change third line of your code as specified below. as your inflated view contains the textView you specified.
TextView tv_footer = (TextView) header.findViewById(R.id.tv_footertext);
Thanks.
can you post the logcat information,it seems that tv_footer value may not set,It may be R.id.tv_footertext is not the part of layout which you set in the setContentView(....) method check it once.It is better if you provide code in detail(since your posted code seems fine)