I am beginner level in android.
how to create multidimensional array in android at runtime.
i want to create multidimensional array of EditText box dynamically (at runtime) and all should be disables except the first one.
if any one knows about this please answer.
examples will be appreciated.
this working ok..
TextView textView[][] = new TextView[2][2];
after this when i tried to assign data it throws nullpointor exception..
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
editText[i][j].setText("data");
}
}
this is the code for multidimensional array of edittext and to display it as well as to disable it...
layout = new LinearLayout(this);
layout.setOrientation(1);
layout.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
EditText editText[][] = new EditText[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
editText[i][j] = new EditText(this);
editText[i][j].setText("1");
editText[i][j].setWidth(50);
layout.addView(editText[i][j]);
}
}
for (int i = 1; i < 2; i++) {
for (int j = 0; j < 2; j++) {
// editText[i][j].setEnabled(false);
editText[i][j].setClickable(false);
editText[i][j].setEnabled(false);
}
}
setContentView(layout);
Related
float[] confidences = outputFeature0.getFloatArray();
int maxPos = 0;
float maxConfidence = 0;
for (int i = 0; i < confidences; i++){
if (confidences[(int) i] > maxConfidence){
maxConfidence = confidences[i];
maxPos = i;
}
}
My expected output
I tried the following code, but it prints only one *. I am confused where the code has went wrong.
int rows = 6, k = 0;
for(int i = 1; i <= rows; ++i, k = 0) {
for(int space = 1; space <= rows - i; ++space) {
t2.setText(" ");
}
while(k != 2 * i - 1) {
t2.setText("* ");
++k;
}
t2.setText();
}
This will work like a charm...
int y = 5,x = 5;
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x-i; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i+1; k++)
{
System.out.print("* ");
}
System.out.print("\n");
}
Try This:
```
Scanner sc = new Scanner(System.in);
char s;
int l;
System.out.println("Enter the Letter : ");
s = sc.next().charAt(0);
l = Character.getNumericValue(s)-9;
for (int i = 0; i < l; i++)
{
for (int j = 0; j < (l - i); j++)
System.out.print(" ");
for (int j = 0; j <= i; j++)
System.out.printf("%c%s",(char)j+65,"");
for (int j = i; j > 0; j--)
System.out.printf("%c%s",(char)j+64,"");
System.out.println();
}
```
The OP could call this:
String s = createCharPyramid(5, "*");
t2.setText(s);
and define this method:
private String createCharPyramid(int numberOfRows, String mySymbol){
String s = "";
String sep = System.lineSeparator();
for(int i = 0; i < numberOfRows; i++){
for(int j = 0; j < (numberOfRows - i); j++){
s = s + " ";
}
for(int j = 0; j <= i; j++){
s = s + mySymbol;
}
for(int j = i; j > 0; j--){
s = s + mySymbol;
}
s = s + sep;
}
return s;
}
This answer is based on MrRobot's answer. His answer is a pure java solution and would correctly show a pyramid in the console. So, if you like this answer give him a +1.
The following is my code but it doesn't generate unique random numbers
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = rand.nextInt(n)+1;
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
Random r = new Random();
StringBuffer temp =new StringBuffer("Random numbers:");
for(int i=0;i<10;i++) {
int i1 = r.nextInt(100 - 0) + 0;
temp.append(String.valueOf(i1));
temp.append(String.valueOf(" "));
}
tv.setText(temp.toString());
}
//here ,,I generate 10 random numbers and save it in to stringBuffer and dispaly it in to textView..here range is up to 0 to 100...you can take your own Range ...I hope ,,It will help you...
check this out!... it works for me
Random rand = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = (int) (rand.nextDouble() * n + 1);
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int piece: array) {
System.out.println(piece);
}
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
I am new to Android and Googled a lot but not getting the context. I am performing matrix multiplication of very large number using AsyncTask but getting error. Any help will be appreciated.
Error:(77, 13) error: method does not override or implement a method from a supertype
add the onPostExecute inside the AsyncTask Class.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
}
you can add the method onPostExecute() inside the AsyncTask.
I have this issue with the xlabels in aChartEngine when there is one value in an ArrayList. I generate my own XLabels for a line chart, but when there is one data array value, i get an unwanted TimeStamp on top of my generated XLabels.
my Code:
private XYMultipleSeriesDataset getDemoDataset() {
Calendar cal = Calendar.getInstance();
Date date = new Date();
date_value = new String[list.size()];
value_value = new String[list.size()];
for(int k = 0; k < list.size(); k++){
date_value[k] = list.get(k).get(DATE);
value_value[k] = list.get(k).get(VALUE);
}
TimeSeries series = new TimeSeries("Line Graph");
for(int j=0; j < date_value.length; j++) {
series.add(formatter.stringToDate(date_value[j]), Integer.parseInt());
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
return dataset;
}
setChartSetting:
private void setChartSetting(XYMultipleSeriesRenderer renderer){
renderer.setXRoundedLabels(true);
renderer.setXTitle("DATES");
renderer.setYTitle(kpiname);
renderer.setApplyBackgroundColor(true);
renderer.setFitLegend(false);
renderer.setAxesColor(Color.DKGRAY);
renderer.setShowGrid(true);
renderer.setZoomEnabled(false);
renderer.setXLabels(0);
renderer.setYLabels(10);
renderer.setZoomEnabled(false, false);
renderer.setPanEnabled(false, false);
//String[] date_value = new String[list.size()];
//String[] value_value = new String[list.size()];
if(list.isEmpty() || list.size() == 0 || list == null ){
return;
}
if(name.equals(RECOVERED) || name.equals(CONVERSION)){
largest_size = (int)Double.parseDouble(value_value[0]);
}else{
if(!(value_value.length == 0)){
largest_size = Integer.parseInt(value_value[0]);
}else{
return;
}
}
//used to determine the maximum value for the y-axis
for(int x =0; x < value_value.length; x++){
if(Integer.parseInt(value_value[x]) > largest_size){
largest_size = Integer.parseInt(value_value[x]);
}
}
renderer.setYAxisMax((double)largest_size);
renderer.setYAxisMin(0);
int value_size = value_value.length;
int m = 0;
//int add = value_size/10;
int add = largest_size/10;
/*instance variables for setting the labels on the X-Axis based on weather the values in the array are
* more than 10 items or less*/
double d;
Long l;
Long l1;
double d1;
if(date_value.length <= 1){
int last = date_value.length;
int mod = 0;
//int add_mod = Math.round(last/10);
int add_mod = (int)Math.round((double)Math.round(last));
l = Long.valueOf(formatter.stringToDateReport(date_value[0]).getTime());
d = l.doubleValue();
/*tried this to see if i can remove the timestamp*/
//renderer.addXTextLabel(0.0, "");
//renderer.removeXTextLabel(0.0);
//renderer.clearXTextLabels();
renderer.addXTextLabel(d, date_value[0]);
}
else if(date_value.length < 10){
for(int i = 0; i < date_value.length; i++){
if(i >= date_value.length){
break;
}
l = Long.valueOf(formatter.stringToDateReport(date_value[i]).getTime());
d = l.doubleValue();
renderer.addXTextLabel(d, date_value[i]);
}
} else if(date_value.length >= 10){
int last = date_value.length;
//int last = 28;
//Log.d(TAG, "last is " + last);
int mod = 0;
//int add_mod = Math.round(last/10);
int add_mod = (int)Math.round((double)Math.round(last) /10); // do this to get the decimal value with a double and then round to get nearset whole with int
for(int i =0; i < date_value.length; i++){
if(mod >= date_value.length){
break;
}
l1 = Long.valueOf(formatter.stringToDateReport(date_value[mod]).getTime());
d1 = l1.doubleValue();
renderer.addXTextLabel(d1, date_value[mod]);
mod+=add_mod;
}
}
}
}
and then:
chartView = ChartFactory.getLineChartView(context, getDemoDataset(), setChartSettings(renderer));
When there are multiple values, it plots the graph well with my custom XLabels(format is MMM-yyyy), but when there is only one value in the ArrayList, it generates thats time stamp.
here is an image:
This was a bug, indeed. You can download a version including a fix for it feature here.