I'm very new to android development. Hopefully you all can help me.
I'm doing a modified version of asqlmanager from sourceforge. In the code, it can only display 1 data in toast at a time. However, I want to display more than one piece of data at a time. The data is something like name, identification no., age. Whenever the user clicks at the row, it will display name, identification no., age in the toast.
the code as copied is as below:-
private void appendRows(TableLayout table, String[][] data) {
if (data == null)
return;
int rowSize=data.length;
int colSize=(data.length>0)?data[0].length:0;
for(int i=0; i<rowSize; i++){
TableRow row = new TableRow(this);
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// button 1 was clicked!
Utils.logD("OnClick: " + v.getId());
}
});
if (i%2 == 1)
row.setBackgroundColor(Color.BLUE);
for(int j=0; j<colSize; j++){
TextView c = new TextView(this);
c.setText(data[i][j]);
c.setPadding(3, 3, 11, 11);
if (j%2 == 1)
// if (i%2 == 1)
// c.setBackgroundColor(Color.BLUE);
// else
c.setBackgroundColor(Color.LTGRAY & Color.MAGENTA);
c.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// button 1 was clicked!
Utils.logD("OnClick: " + v.getId());
String text = (String)((TextView)v).getText();
EditText Displayname;
Displayname=(EditText)findViewById(R.id.SQLStm);
Displayname.setText(text);
Utils.toastMsg(_cont, "Text copied to clip board" + Displayname.getText()+ v.getId());
}
});
row.addView(c);
}
table.addView(row, new TableLayout.LayoutParams());
}
}
end of the code
Thank you.
Related
I am trying to code a "next" button for my Sqlite db. The way I want it to work is upon entering a number it one of the entries from the db if pressed again it brings up the entry on the list for the same given number entered. However I cant get it to bring up the next entry. So far my button code looks like this. However I have tried countless way to configure it with no luck. Please help. Thanks in advance!!!
public void onClick(View view) {
if (view == btnNext) {
Cursor c = db.rawQuery("SELECT * FROM number WHERE meter='" + editText6.getText() + "'", null);
if (editText6.getText().toString().trim().length() >= 0) {
c.getPosition();
c.moveToPosition(+1);
idNumber.setText(c.getString(0));
editText5.setText(c.getString(6));
editText4.setText(c.getString(5));
editText3.setText(c.getString(4));
editText2.setText(c.getString(3));
editText.setText(c.getString(2));
textView4.setText(c.getString(7));
status.setText(c.getString(8));
while (c.moveToNext()) ;
}
}
I had to add this separate method to keep cursor open while scrolling to the next entry:
final Button nextBtn = (Button) findViewById(R.id.btnNext);
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (editText6.getText().toString().trim().length() == 0) {
Toast ToastMessage1 = Toast.makeText(getApplicationContext(), "Please" +
" Enter Meter Number\nThen Press Lookup Meter", Toast.LENGTH_LONG);
ToastMessage1.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
View toastView1 = ToastMessage1.getView();
toastView1.setBackgroundResource(R.drawable.shape9);
ToastMessage1.show();
} else {
if (c.getPosition() >= 1) {
c.moveToPrevious();
idNumber.setText(c.getString(0));
//Must maintain this order listed below in order for clear field on focus to work properly.
editText6.setText(c.getString(1));
editText5.setText(c.getString(6));
editText4.setText(c.getString(5));
editText3.setText(c.getString(4));
editText2.setText(c.getString(3));
editText.setText(c.getString(2));
//To here-list order.
date.setText(c.getString(7));
status.setText(c.getString(8));
etAddress.setText(c.getString(9));
}
if (c.getPosition() == 0) {
c.close();
Toast ToastMessage = Toast.makeText(getApplicationContext(), " Last Entry Recorded ", Toast.LENGTH_LONG);
ToastMessage.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
View toastView = ToastMessage.getView();
toastView.setBackgroundResource(R.drawable.shape9);
ToastMessage.show();
}
final Button clearBtn = (Button) findViewById(R.id.btnClear);
clearBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
if (v2 == clearBtn) {
c.close();
clearText();
}
}
});
}
}
});
I have a dynamically created radio buttons inside a RadioGroup since the values are coming from the database. I want to be able to implement setOnClickListener so I can get the whatever the selected value is. Whenever I try to click on the radio button, nothing shows up.
public void viewAll() {
Cursor res = myDb.getAllData();
LinearLayout bg = (LinearLayout) findViewById(R.id.backgroundSM);
LinearLayout display = (LinearLayout) findViewById(R.id.viewAllMsg);
final RadioButton[] rb = new RadioButton[5];
rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
res.moveToFirst();
for(int i=0; i<res.getCount(); i++){
rb[i] = new RadioButton(this);
//Toast.makeText(getApplicationContext(),res.getString(1) + " ", Toast.LENGTH_LONG).show();
rb[i].setText(" " + res.getString(1)
+ " " + res.getInt(0));
rb[i].setId(i + 100);
rg.addView(rb[i]);
res.moveToNext();
}
display.addView(rg); // add the whole RadioGroup to the layout
rg.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]); // now the RadioButtons are in the RadioGroup
}
int id = rg.getCheckedRadioButtonId();
Toast.makeText(getApplicationContext(),id + " worked", Toast.LENGTH_LONG).show();
}
});
}
What is wrong with my code? Thank you so much for your help.
Selected position will be saved in position variable
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
if (radioGroup.getChildAt(i).getId() == id) {
position = i + 1; //+1 is used to have a start value of 1 like your example
break;
}
}
}
});
Am trying to create edittext and textview widgets dynamically into program. it works fine. now i want to access the textvalue with its corresponding edittext value. how to do it?
Here is what i have tried.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_callreport);
dcname = ProductdetailsEnd.doctor;
resultArr1 = ProductdetailsEnd.resultArr;
callreportbtn = (Button) findViewById(R.id.callreportbtn);
arraysize = resultArr1.length;
TableLayout tb = (TableLayout) findViewById(R.id.tablelayout);
for (int i = 0; i < arraysize; i++) {
res = resultArr1[i];
TableRow tr = new TableRow(this);
TableRow.LayoutParams pl = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT);
tr.setLayoutParams(pl);
tr.setWeightSum(1.0f);
product = new TextView(this);
product.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.7f));
product.setId(i);
qty = new EditText(this);
qty.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.3f));
qty.setId(i);
qty.setWidth(50);
product.setText(res);
tr.addView(product);
tr.addView(qty);
tb.addView(tr, i);
Log.d("Call Report name : ", "" + dcname);
Log.d("Call Report prod :", "" + res);
}
Log.d("res length : ", "" + arraysize);
for (int i = 0; i < arraysize; i++) {
String product1 = resultArr1[i];
String qty1 = qty.getText().toString();
Log.d("products &&: ", "" + product1 + ":" + qty1);
}
callreportbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello",
Toast.LENGTH_LONG).show();
for (int i = 0; i < arraysize; i++) {
String product1 = resultArr1[i];
String qty1 = qty.getText().toString();
Log.d("products &&: ", "" + product1 + ":" + qty1);
}
}
});
}
after entering the value, when i say submit it should display the textview value along with edittext value.
For more convenience i have added a snap of my output.
first image to enter the value
second image is output in logcat.
There are many ways to do what you want.
Place your views inside a List object, and read the list when you submit.
Implement TextWatcher interface and record your String values as they change.
Assign an Id to your views, and later retrieve your views by Id.
Third option required you to carefully assign id's, because I think they must be unique. Second option, I think, is best one in many cases, since you may also want to provide some feedback to user.
EDIT:
TextWatcher is interface that receives callbacks from TextView. Simply call addTextChangedListener method and provide a TextWatcher implementation.
aTextView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Finally i got the answer. referred this post - Creation of EditText Dynamically and get their text from each of EditText
Created an array of the EditText and accessed it in the button onclick method.
this is my code and its working.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_callreport);
dcname = ProductdetailsEnd.doctor;
resultArr1 = ProductdetailsEnd.resultArr;
callreportbtn = (Button) findViewById(R.id.callreportbtn);
quantity = new ArrayList<EditText>();
arraysize = resultArr1.length;
TableLayout tb = (TableLayout) findViewById(R.id.tablelayout);
for (int i = 0; i < arraysize; i++) {
res = resultArr1[i];
TableRow tr = new TableRow(this);
TableRow.LayoutParams pl = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT);
tr.setLayoutParams(pl);
tr.setWeightSum(1.0f);
product = new TextView(this);
product.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.7f));
product.setId(i);
qty = new EditText(this);
qty.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.3f));
//qty.setId(i);
qty.setWidth(50);
product.setText(res);
tr.addView(product);
tr.addView(qty);
tb.addView(tr, i);
quantity.add(qty);
Log.d("Call Report name : ", "" + dcname);
Log.d("Call Report prod :", "" + res);
}
Log.d("res length : ", "" + arraysize);
Log.d("qty length : ", "" + qty.length());
String[] items1 = new String[quantity.size()];
for (int i = 0; i < arraysize; i++) {
String product1 = resultArr1[i];
items1[i] = quantity.get(i).getText().toString();
Log.d("qty id ", "" +qty.toString());
Log.d("products &&: ", "" + product1 + ":" + items1);
}
callreportbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello",
Toast.LENGTH_LONG).show();
String[] items = new String[quantity.size()];
for (int i = 0; i < arraysize; i++) {
String product1 = resultArr1[i];
items[i] = quantity.get(i).getText().toString();
Log.d("products &&: ", "" + product1 + ":" + items[i]);
}
}
});
}
I have 3 row in table, where every row has 3 checkbox. I want to check appropriate CheckBox according to its id.
I have to check following ids CheckBox.
tempArray =[1-2-3,3-2,null];
i am already splitting these data and putting in string array(setopts).more see my code.
Null for no check any CheckBox with this row.
I am doing this code in my project.
Vector<CheckBox> chkBoxList = new Vector<CheckBox>();
Vector<TextView> txtViewList = new Vector<TextView>();
// mat_elemItemSetChk.get(0).size() OF VALUE IS 3
for (int n = 0; n < mat_elemItemSetChk.get(0).size(); n++) {
LinearLayout llayout = new LinearLayout(getContext());
ll.setOrientation(android.widget.LinearLayout.VERTICAL);
cb = new CheckBox(getContext());
tv = new TextView(getContext());
for (int r = 0; r < tempArray.size() ; r++) {
isContains=tempArray.get(r).contains(individualValueSeparator);
if(isContains){
setOpts = tempArray.get(r).split("\\-");
for (int k = 0; k < setOpts.length; k++)
{
ItemValue iv = (ItemValue) mat_elemItemSetChk.get(0).get(n);
if (((int) iv.getId()) == Integer.parseInt(setOpts[k]))
{
mat_elemItemSetChk.get(n).get(Integer.parseInt(setOpts[k])-1);
cb.setChecked(true);
}
}
}else{
if(tempArray.get(r).toString().equalsIgnoreCase("null")||tempArray.get(r).toString()!=null){
String temp;
String px[]= null;
temp = tempArray.get(r).toString();
ItemValue iv = (ItemValue) mat_elemItemSetChk.get(0).get(n);
if (((int) IV.getId()) == Integer.parseInt(temp))
{
mat_elemItemSetChk.get(0).get(Integer.parseInt(temp) - 1);
cb.setChecked(true);
}
}else{
cb.setChecked(false);
}
}
}
tv.setText(mat_elemItemSetChk.get(0).get(n) .toString());
llayout.addView(cb);
llayout.addView(tv);
chkBoxList.add(cb);
txtViewList.add(tv);
ll.addView(llayout);
// this.addView(tableLayout);
}
Thank you in advance.
make an array selchkboxlist and store the selected checkbox id in it and check condition like selchkboxlist.isEmpty() if true then then show error msg other wise go ahed...
selchkboxlist=new ArrayList<String>();
cbs = new CheckBox[8];
// generate dynamic item check box code
for(int k = 0; k<8; k++)
{
cbs[k] = new CheckBox(getApplicationContext());
#SuppressWarnings("deprecation")
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(cbs[k], params);
int j =k+1;
cbs[k].setText("ITEM" + j);
cbs[k].setTextColor(Color.parseColor("#000000"));
cbs[k].setId(k+1);
cbs[k].setPadding(70, 20, 10, 10);
// check box on click listener for add in to produduct array with quntity
cbs[k].setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
String chk = null;
if (((CheckBox) v).isChecked()) {
chk = Integer.toString(v.getId());
selchkboxlist.add(chk);
} else {
selchkboxlist.remove(chk);
}
}
});
}
// button on click listener code
b.setOnClickListener(new OnClickListener() {
#SuppressLint("SdCardPath")
public void onClick(View v) {
if (! selchkboxlist.isEmpty()) {
shoe erroe msg here
}else{
go ahed
}
}
});
I am working on quiz application. For each question there are multiple answers and so for that I have used checkboxes. In each page I am displaying single question with options and when next button is clicked next question will be displayed. So when multiple answers are selected by the user I need to store them in an arraylist and need to compare the selected answers with that in database. In my code I am storing the selected value id's one by one but at a time it is storing only one value. How to store all the selected checkbox id's in my arraylist?
My Code:
ArrayList<String> selchkboxlist;
CheckBox[] cbs;
cbs = new CheckBox[20];
for(k=0; k<List3.size(); k++)
{
arr = List3.get(k);
cbs[k] = new CheckBox(getContext());
Rl.addView(cbs[k]);
cbs[k].setText((CharSequence) arr.get(2));
cbs[k].setTextColor(Color.parseColor("#000000"));
cbs[k].setId(k);
cbs[k].setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v ;
if (((CheckBox) v).isChecked()) {
chk = Integer.toString(v.getId());
selchkboxlist = new ArrayList<String>();
selchkboxlist.add(chk);
Toast.makeText(Select1.this, "Selected CheckBox ID" + v.getId(), Toast.LENGTH_SHORT).show();
} else {
selchkboxlist.remove(chk);
Toast.makeText(Select1.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
}
Please help me regarding this....Thanks in Advance
Initialize the selchkboxlist before for loop....
ArrayList<String> selchkboxlist=new ArrayList<String>();
CheckBox[] cbs;
cbs = new CheckBox[20];
for(k=0; k<List3.size(); k++)
{
arr = List3.get(k);
cbs[k] = new CheckBox(getContext());
Rl.addView(cbs[k]);
cbs[k].setText((CharSequence) arr.get(2));
cbs[k].setTextColor(Color.parseColor("#000000"));
cbs[k].setId(k);
cbs[k].setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v ;
if (((CheckBox) v).isChecked()) {
chk = Integer.toString(v.getId());
selchkboxlist.add(chk);
Toast.makeText(Select1.this, "Selected CheckBox ID" + v.getId(), Toast.LENGTH_SHORT).show();
} else {
selchkboxlist.remove(chk);
Toast.makeText(Select1.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
}
store the value in sharedPreferences and when you need again just retrieve from sharedPreferences and do your task.
i have done in my last project and its working...try man..