How to edit view created programmatically - android

So in my application i have a linear layout, to which i'm adding programmatically some CardViews (android L cardview) like this :
//This is my LinearLayout
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
//Here i create my CardView from a prepared xml layout and inflate it to the LinearLayout
View card = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
//Now i change the 'text' value of the Card's text views
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title1");
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description1");
//...
//Now i do the same thing for another card
View card2 = View.inflate(getApplicationContext(), R.layout.account_card, myLayout);
TextView cardTitle2 = (TextView) card2.findViewById(R.id.text_card_title);
cardTitle2.setText("Title2");
TextView cardDecription2 = (TextView) card2.findViewById(R.id.text_card_description);
cardDecription2.setText("Description2");
//...
The two cards are displayed properly, but what happens is than the first card displayed has "Title2" and "Description2" written in the textViews, while the second card has the default values defined in the xml.
It seems to me that by calling card.findViewById() or card2.findViewById() i get always the TextView of the first card.
So my question is : how do i manage to differentiate the cards i create programmatically and then correclty access the view within them?

Try this way,hope this will help you to solve your problem.
LinearLayout myLayout = (LinearLayout) findViewById(R.id.accounts_layout);
for (int i=1;i<=2;i++){
View card = View.inflate(getApplicationContext(), R.layout.account_card, null);
TextView cardTitle = (TextView) card.findViewById(R.id.text_card_title);
cardTitle.setText("Title"+i);
TextView cardDecription = (TextView) card.findViewById(R.id.text_card_description);
cardDecription.setText("Description"+i);
card.setTag(i);
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Toast.makeText(context,pos,Toast.LENGTH_SHORT).show();
}
});
myLayout.addView(card);
}

Related

Differ between elements when XML reused by including?

I got a LinearLayout where I add the Layout of a seperate XML.
In that seperate XML I added a OnClickListener on that Layout.
Now when the Layout is clicked, a new View is added where the User has to type some information.
When ready the additional View is closed and the data has to be put in the right element.
But it does always put it in the first of them.
How can I tell the Listener in which one it has to put it?
Thanks in advance.
editElement is called if one of those multiple included elements is clicked.
The saveEdit when the finish button of the editor is clicked.
public void editElement(View view) {
FrameLayout base = (FrameLayout) findViewById(R.id.base);
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout editor = (LinearLayout) inflater.inflate(R.layout.element_editor, null);
editorID = editor.getId();
base.addView(editor);
currEdit = view.getId();
TextView day = (TextView) view.findViewById(R.id.day);
TextView day_editor = (TextView) editor.findViewById(R.id.day_editor);
day_editor.setText(day.getText());
}
public void saveEdit(View view) {
FrameLayout base = (FrameLayout) findViewById(R.id.base);
GridLayout parent = (GridLayout) view.getParent();
LinearLayout element = (LinearLayout) findViewById(currEdit);
TextView dur_act = (TextView) element.findViewById(R.id.duration_activity);
EditText act = (EditText) parent.findViewById(R.id.de_activity);
EditText dur = (EditText) parent.findViewById(R.id.de_duration);
dur_act.setText(dur.getText()+(!dur.getText().equals("")?"":" min ")+act.getText());
TextView comment = (TextView) diaryelement.findViewById(R.id.comment);
EditText comm = (EditText) parent.findViewById(R.id.e_comment);
comment.setText(comm.getText());
base.removeView(findViewById(editorID));
}

RecycleView Select multiples

How I can select multiples rows in a recycleview correctly ?
I see this example : https://github.com/kiddBubu/RecyclerViewDemo but is so bat because when move up or down screen value disappear or change or assing position to other row, so bad.
I try show more content of the row.
Example :
Row 1 (uncollapsed)
Row 2 (uncollapsed) - > click - > (collapsed) add + padding or margin
Row 3 (uncollapsed)
.
.
Row x (uncollapsed)
But when I try move list up or down the value selected is randomly and others views change automatically, I don't know why, ListView, RecycleView are equals...
I know how do with listviews normals with "tags" (but also is bad)
I don't want "expandablelistview".
Any help ?
Update
I want this efect : http://i.stack.imgur.com/fuFFl.png
I have not found anything, not a complete example in google
Code :
I tried 4 or 5 methods, this is the first
boolean [] check; check = new Boolean[Fragment2.recyclerView.size()];
Arrays.fill(array, Boolean.FALSE);
.
.
.
public class ViewHolder extends RecyclerView.ViewHolder {
// l1
public TextView text1,text2,text3,text4;
LinearLayout imgLayout,l1;
// l2
public TextView text5,text6,text7;
LinearLayout l2;
public ViewHolder(final View itemView) {
super(itemView);
l1 = (LinearLayout) itemView.findViewById(R.id.l1);
imgLayout = (LinearLayout) itemView.findViewById(R.id.imgLayout);
text1 = (TextView) itemView.findViewById(R.id.text1);
text2 = (TextView) itemView.findViewById(R.id.text2);
text3 = (TextView) itemView.findViewById(R.id.text3);
text4 = (TextView) itemView.findViewById(R.id.text4);
l2 = (LinearLayout) itemView.findViewById(R.id.l2);
text5 = (TextView) itemView.findViewById(R.id.text5);
text6 = (TextView) itemView.findViewById(R.id.text6);
text7 = (TextView) itemView.findViewById(R.id.text7);
int itemPosition = Fragment2.recyclerView.getChildPosition(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!check[itemPosition]) {
l1.setVisibility(View.VISIBLE);
l1.setEnabled(true);
check[itemPosition] = true;
l2.setVisibility(View.GONE);
l2.setEnabled(false);
} else {
l1.setVisibility(View.GONE);
l1.setEnabled(false);
check[itemPosition] = false;
l2.setVisibility(View.VISIBLE);
l2.setEnabled(true);
}
}
The RecyclerView connects a data structure like an ArrayList via Adapter. Upon an update of the ArrayList, your RecyclerView's View objects should change state.
If I were to update multiple rows in a RecyclerView, I'd update the value inside the ArrayList and call mAdapter.notifyDataSetChanged(). So when I scroll, the element has been updated properly.

Add TextView dynamically and getTag()

I have one class calls "SplitText" and extends AsyncTask.
There i added a dynamic textview:
TextView masterTV = new TextView(mActivity);
tv.setTag("masterTV");
...
masterTV.setText(Activity_Start.chapterContentList.get(0));
LinearLayout sv = (LinearLayout) mActivity.findViewById(R.id.linearlayout);
sv.removeAllViews();
sv.addView(masterTV);
This works fine.
Now i want to get the text from this TextView from an other class calls Activity_Start which extends Activity..
on there onOptionsItemSelected() i have:
case R.id.menu_previous:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
TextView tv = (TextView) ll.findViewWithTag("masterTV");
Log.e("previs", tv.getText().toString());
return true;
But this returns always a NullPointerException..
What i do wrong?
UPDATE:
I have found an other way to get my TextView:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
TextView tv = (TextView) ll.getChildAt(0);
This isn't the exactly way i want but i runs and do what i want.
(Provided your Textview is child no 0)

Add Views to LinearLayout programmatically

I have an Activity that displays comments. The comments themselves have a layout, so I can't just use a ListView.
I'm adding the comments with a loop, and the program goes through the whole loop (checked via LogCat), but only adds the first View (comment) to the linearlayout.
My code (in reality the fillComments parameter will be something else than String[]):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_layout);
String[] comments = {"kommentaar 1", "kommentaar 2", "kommentaar 3"};
mTitle = (TextView) findViewById(R.id.comments_title);
mTextArea = (EditText) findViewById(R.id.comment_editor);
mAddButton = (Button) findViewById(R.id.add_comment);
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
mTitle.setText(getIntent().getStringExtra("name"));
fillComments(comments);
}
private void fillComments(String[] comments) {
View comment;
TextView commentator;
TextView commentDate;
TextView commentText;
LayoutInflater inflater = getLayoutInflater();
for (String s : comments) {
Log.d("Comment adder", "Adding comment " + s);
comment = inflater.inflate(R.layout.comment_row_layout, null);
commentator = (TextView) comment.findViewById(R.id.commentator);
commentDate = (TextView) comment.findViewById(R.id.comment_date);
commentText = (TextView) comment.findViewById(R.id.comment_text);
commentator.setText("Test commentator");
commentDate.setText("12-12-2012");
commentText.setText(s);
mCommentArea.addView(comment);
}
}
i think
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
this layout orientation is Horizontal so this problems occurs. please if horizontal orientations then please change it to vertical and enjoy
How have you defined the LinearLayout? It could be just a display issue.
Check the size and orientation of LinearLayout.

Linearlayout display only last data in android

i want to integrate horizonal scrollview with custom linear layout inflater so i set below code
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mLinearLayout.removeAllViews();
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
}
in above my array list include 9 data but in screen it is display only one data so any idea how can i solve this ?
You should write this function like this
mLinearLayout.removeAllViews();
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
}
try this one...
for (int i=0;i<mArrayListPersonDataLists.size();i++)
{
View child = getLayoutInflater().inflate(R.layout.row_pager_layout, null);
TextView mTextView1=(TextView)child.findViewById(R.id.row_txt_name);
TextView mTextView2=(TextView)child.findViewById(R.id.row_txt_email);
TextView mTextView3=(TextView)child.findViewById(R.id.row_txt_gender);
mTextView1.setText(mArrayListPersonDataLists.get(i).getName());
mTextView2.setText(mArrayListPersonDataLists.get(i).getEmail());
mTextView3.setText(mArrayListPersonDataLists.get(i).getGender());
mLinearLayout.addView(child);
child.setId(i+1)
child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//your code here
int position=child.getId()-1;
Toast.makeText(HorizontalAcivity.this, mArrayListPersonDataLists.get(position).getName(), Toast.LENGTH_SHORT).show();
}
});
}
Remove a line with mLinearLayout.removeAllViews(); :)

Categories

Resources