I am creating a table with dynamic custom table rows. I need to get the id in the activity class.
main_Activity-->MyTableLayoutView-->MyTableRow
My Question is, how do i get the id of the cell that was clicked in the table (TextViews) to the main_Activiy.
MyTableRowView:
public void addRow(String[] data, int[] rowId) {
for (int i = 0; i < data.length; i++) {
TextView tv = parseTextView(data[i]);
tv.setId(rowId[i]);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv2 = (TextView) v;
clickedText = tv2.getText().toString();
Debug.debugMsg(clickedText + " " + tv2.getId());
}
});
this.addView(tv);
}
}
As far as I can get is getting the id from the TablerowView class, but I need it to be in Activity class, Please if anyone can enlighten me. Thanks in advance.
Activity class:
private void showTables() {
db.open();
db.importDb();// TODO
table = new TableLayoutView(this,Converter.toArrayListStringArray(db.getDbTablesForChoose()));
table.addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true);
llChooseSQLTable.addView(table);
}
TableLayoutView:
public void addDataListArray(ArrayList<String[]> data, boolean header) {
for (int i = 0; i < ROW_NUMBERS; i++) {
TableRowView tableRow = new TableRowView(context);
tableRow.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tableRow.addRow(data.get(i),idHandler.getNextIdsRow());//idHandles passes unique id.
this.addView(tableRow);
}
}
It seems like you are able to get the ID from your code above. If your primary concern is propagating that id from the view back up to the parent class then you could do it in multiple ways. One way is to get the context of the view then cast it to the specific activity and call a function within that activity, e.g. within the onClick() operation:
public void onClick(View v) {
((MyActivity)v.getContext()).insertNotificationMethod(v.getId());
}
Where your activity is called MyActivity and you implement a method called insertNotificationmethod which takes an integer. This would work only if this view is always within this function, and even then it is a rather crude way to do it.
You could also just directly call it within the onClick() method using:
MyActivity.this.insertNotificationMethod(v.getId());
However, this way of doing things would limit this view to only be useful within this particular activity.
For a more generic way, you could BroadcastReceivers which may be a little more complicated that you would like. For more information about those you could check out the Android documentation here: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Hopefully that helps.
The only solution that I can think of is to move your addRow() and addDataListArray() methods to the Activity with some modefication:
public void addRow(String[] data, int[] rowId, TableRowView tableRow) {
for (int i = 0; i < data.length; i++) {
TextView tv = parseTextView(data[i]);
tv.setId(rowId[i]);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv2 = (TextView) v;
clickedText = tv2.getText().toString();
clickedId = tv2.getId());
}
});
tableRow.addView(tv);
}
}
public void addDataListArray(ArrayList<String[]> data, boolean header, TableLayoutView table) {
for (int i = 0; i < ROW_NUMBERS; i++) {
TableRowView tableRow = new TableRowView(context);
tableRow.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addRow(data.get(i),idHandler.getNextIdsRow(), tableRow);//idHandles passes unique id.
table.addView(tableRow);
}
}
And in your Activity change this line:
//table.addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true);
addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true, table);
Although I don't know if other part of your app will be affected by this huge modification I made.
Good luck!
Related
I have created an ImageView dynamically like below image.It works fine. Now I want to remove view when top cross ImageView is clicked. When I click, it crashes .Please help how to achieve it.
here is what i have done
private void postImage(List<Uri> urilist) {
for(int i=0; i< urilist.size(); i++) {
imgView = new ImageView(getActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(150, 150);
lp.setMargins(20,10,20,10);
imgView.setLayoutParams(lp);
imgView.setId(i);
Log.d("uri list in loop",""+urilist.get(0));
Glide.with(getActivity())
.load(urilist.get(i))
.into(imgView);
layout.addView(imgView);
imgView1 = new ImageView(getActivity());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(50, 50);
lp1.setMargins(0,5,1,80);
imgView1.setLayoutParams(lp1);
imgView1.setId(i);
Log.d("uri list in loop",""+urilist.get(0));
Glide.with(getActivity())
.load(R.drawable.ic_action_cross)
.into(imgView1);
layout.addView(imgView1);
}
imgView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.removeViewAt(v.getId());
}
});
}
If it is because of the index (which definitely will crash in deletion of the 2nd item) then you can try below
imgView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewGroup parentView = (ViewGroup) v.getParent();
parentView.removeView(v);
}
});
Note: You should not set id of two views as same. Rather use some mathematical formula.
I mean #shakac, you can try like that;
for(int i = 0; i<layout.getChildCount(); i++)
{
if (layout.getChildAt(i).getId() == v.getId()){
layout.removeView(layout.getChildAt(i));
break;
}
}
But as I said on the comment, you will remove the cross button like that.
I'm really strugling to figure this out and had not found an answer or a way to do it.
What I'm trying to do is having questions that are saved in a SQLite colum to be displayed in a TextView one after another until they finish.
What I have done so far on the main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabaseHelper = new DatabaseHelper(this);
btn1star = (Button) findViewById(R.id.btn1star);
btn2star = (Button) findViewById(R.id.btn2star);
btn3star = (Button) findViewById(R.id.btn3star);
mListview = (ListView) findViewById(R.id.qlistview);
qTextView = (TextView) findViewById(R.id.qTextView);
questionsListView();
}
private void questionsListView() {
Cursor data = mDatabaseHelper.getData();
data.moveToFirst();
qTextView.setText(data.getString(1));
}
public void VoteClick(View view){
mDatabaseHelper.getReadableDatabase();
Cursor data = mDatabaseHelper.getData();
if (data.getCount() >=1){
for (int i = 0; i< data.getCount(); i++) {
data.moveToNext();
Log.i("Counted Questions are: ", String.valueOf(data.getCount()));
qTextView.setText(data.getString(1));
}
}
}
private void toastmessage (String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
I'm able to display the first and when i click the button the last question
I'm unable to display the questions between.
Any tip or suggestion?
In your VoteClick method you do your getReadableDatabase, this is too late, move this line into onCreate method. You must open the database before reading from it (in questionsListView).
mDatabaseHelper.getReadableDatabase();//move to onCreate
add this:
startManagingCursor(data);
data.moveToFirst();
BEFORE the line:
if (data.getCount() >=1){
Hello found a solution to my problem
It turns out that i have to make a new int variable
Get its value from the first id
and incrise this by one at the end of each button press until i reach the last cursor place whhere i move the cursor to the first position
public void VoteClick(View view) {
Cursor data = mDatabaseHelper.getData();
data.moveToPosition(q);
data.moveToNext();
qTextView.setText(data.getString(1));
while (data.isLast()) {
data.moveToFirst();
q = data.getPosition();
}
q = q+1;
}
I am building an android application where I am creating dynamic EdittextView. I need to display the sum of integer enter in it by the user. Below is my code to create Dynamic EdittextView:
for (int i = 1; i < ZipRunApplication.ConfigLeg; i++){
LayoutInflater inflater = null;
inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.drop_money, null);
TextView droxTextView = (TextView) mLinearView.findViewById(R.id.dropTextView);
final TextView position = (TextView) mLinearView.findViewById(R.id.position);
final TextView Amount = (TextView) mLinearView.findViewById(R.id.Amount);
final EditText dropEditTextView = (EditText) mLinearView.findViewById(R.id.dropEditext);
dropEditTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
Amount.setText(dropEditTextView.getText().toString()); //
}
});
droxTextView.setText("Amount to be pick From Drop " + String.valueOf(i));
position.setText(String.valueOf(i));
droxTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
container.addView(mLinearView);
}
Could any one help me getting the sum of the all the EdittextView created Dynamicly.
Answer for:
Could any one help me getting the sum of the all the EdittextView
created Dynamically.
You can maintain an ArrayList of EditText and then can iterate through them and get the text entered in each of them and find the sum.
As an example I have the following snippet:
LinearLayout layout;
List<EditText> concernedEditTexts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout)findViewById(R.id.base_layout); // Base layout defined in xml
concernedEditTexts = new ArrayList<EditText>();
// Creating five EditTexts
for(int i= 0; i< 5; i++){
EditText text = new EditText(getApplicationContext());
layout.addView(text);
concernedEditTexts.add(text); // Adding dynamically created EditText in the ArrayList
}
Button button = new Button(getApplicationContext());
button.setText("Get Sum");
layout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int sum = 0;
// Iterate through the List and find the sum
for(EditText editText : concernedEditTexts){
sum+= Integer.parseInt(editText.getText().toString());
}
Log.d("SUM","Sum is "+sum);
}
});
}
Note: This code is kind of raw and needs a lot more validations, but should be enough to explain the concept.
In your example you will need to store every dropEditTextView in the List and then iterate through the list as shown in my example will give you the desired result.
you can use
Integer.parseInt(dropEditTextView.getText().toString())
setTag() to each EditText as i variable and take an arrarylist of size ZipRunApplication. when you write in edittext then in textchnage listener get tag value and convert to int. This value will tell you for which position in arraylist you are writing . then in that position of arraylist set Integer.parseInt(editTextString).
Four tips:
1 - Here we get the childs by the parent:
for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
if (mLinearLayout.getChildAt(i) instanceof LinearLayout) {
LinearLayout ll = (LinearLayout) mLinearLayout.getChildAt(i);
for (int j = 0; j < ll.getChildCount(); j++) {
if (ll.getChildAt(j) instanceof EditText) {
ll.getChildAt(j).setOnFocusChangeListener(this);
}
}
}
}
2 - Don't forget to parse the data you try to receive:
Integer.parseInt(mFocusedEditText.getText().toString());
3 - note:
view.setTag() <-> View.getTag()
4 - And last but not least: In terms of readability, maintainability and performance you will get to a point soon, where a ListView- or RecyclerView will fit your needs MUCH better (so keep in mind: the above coding isn't a proper solution, even if it works).
ListView and
RecyclerView
In my android app, I have a activity that contain a listview. And within the each row of the listview, i have another listview let called it lv. Inside this lv, it contain some textview. My question is how can i get the text from the listview (A)?
I know that if there is only one listview and in order to get the text of each row when i click on a button, i can use the following code
textview.getText().toString();
However, when i try with this code to get the text in lv, it always give me the last value (n row) in lv. I am not able to get the text of 1st row to n-1 row of lv.
UPDATE:
The follow are the code where i want to get the text, the ordered[] is passed from a custom adapter to another custom adapter (which is the following code)
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=((Activity) context).getLayoutInflater();
View rowView=inflater.inflate(layoutResourceId, null,true);
txt1 = (TextView)rowView.findViewById(R.id.textView1);
txt2 = (TextView)rowView.findViewById(R.id.textView2);
txt3 = (TextView)rowView.findViewById(R.id.textView3);
txt4 = (TextView)rowView.findViewById(R.id.textView4);
txt5 = (TextView)rowView.findViewById(R.id.textView5);
txt6 = (TextView)rowView.findViewById(R.id.textView6);
txtoid = (TextView)rowView.findViewById(R.id.textView7);
final Button btnGet = (Button)rowView.findViewById(R.id.btnGet);
tmp = position;
String tmp2 = String.valueOf(ordered[position]);
txtoid.setText(tmp2);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnconfirm.getText().toString().equals("Get")){
String tmp1 = txtq.getText().toString();
System.out.println("tmp1: "+tmp1);
}
}
});
....
Anyone help will be nice!
Thanks
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View rootView) {
// TODO Auto-generated method stub
TextView myText = (TextView)rootView.findViewById(R.id.textView7);
Log.d("","myText:" + myText.getText()); //you can get each row values.
}
});
Can anyone please guide me on how to setOnClickListener for the TextViews that are generated dynamically.
I have few buttons in my app.Now when a particular button is clicked than some specific text elements are retrieved from the database based on the the number of elements returned corresponding TextViews are generated in order to display the retrieved text elements.
Now the problem that i'm having is that i don't know how to set onClickListner for the dynamically generated TextViews.Following is the piece of code where i'm creating my TextViews.Please guide me on what should i do.thank you
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
catagory = "General";
IndependentDB genData = IndependentDB.getInstance();
genData.open(this);
ArrayList<TextHolder> genList = new ArrayList<TextHolder>();
genList = genData.getAllTextFromGenT();
genData.close();
x = genList.size();
String xstr = new StringBuilder().append(x).toString();
System.out.println(xstr);
mainText = new TextView[x];
textLayout = new LinearLayout[x];
llseperator = new LinearLayout[x];
textLayoutContainer
.setBackgroundResource(R.color.dark_navy_blue);
if (!genList.isEmpty()) {
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
mainText[i] = new TextView(this);
mainText[i].setId(i);
mainText[i].setText("Text");
mainText[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mainText[i].setTextSize(25);
mainText[i].setGravity(Gravity.CENTER);
mainText[i].setText(text);
mainText[i].setTextColor(0xFFFFFFFF);
mainText[i].setClickable(true);
mainText[i].setOnClickListener(this);
llseperator[i] = new LinearLayout(this);
llseperator[i].setId(i);
llseperator[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
llseperator[i].setGravity(Gravity.CENTER);
llseperator[i]
.setBackgroundResource(R.drawable.tv_seperator);
textLayout[i] = new LinearLayout(this);
textLayout[i].setId(i);
textLayout[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout[i].setGravity(Gravity.CENTER);
textLayout[i].addView(mainText[i]);
textLayoutContainer.addView(textLayout[i]);
textLayoutContainer.addView(llseperator[i]);
}// End of for
}// End of If
break;}
You already have onClick() method in Activity class (implemented View.setOnClickListener) so I suggest you to create a new class which implements View.setOnClickListener interface to handle click of TextView.
Try,
class TextClick implements View.setOnClickListener {
#Override
public void onClick(View view) {
TextView tx=(TextView) view;
...
}
}
and set click listener,
TextClick click=new TextClick();
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
mainText[i] = new TextView(this);
mainText[i].setOnClickListener(click);
...
}
It's the same setting on click listeners regardless of if the view was dynamically generated.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dosomething();
}
});
For example. you have a List tvs to store your TextView Objects created by the db result. What you should do is like this:
for(int i =0;i<tvs.count;i++){
tvs.get(i).setOnClickListener(new OnClickListener(){
...
})
}