Hi,
I would like to read data from my sqllite database. below my code but i am getting error . "Cannot resolve method maketext java.lang.exception,int)
Code:
package in.whomeninja.android_barcode_scanner;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ShowProduct extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showproduct);
GridView gvproduct=(GridView)findViewById(R.id.gvproduct);
List<String> li=new ArrayList<>();
ArrayAdapter<String> dataAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,li);
dataAdapter.setDropDownViewResource(R.layout.showproduct);
try {
SQLiteDatabase db=openOrCreateDatabase("coke_db",MODE_PRIVATE,null);
Cursor cr=db.rawQuery("SELECT * FROM PRODUCT_TBL",null);
if(cr!=null){
if(cr.moveToFirst()){
do{
String no=cr.getString(cr.getColumnIndex("barcodeResult"));
String name=cr.getString(cr.getColumnIndex("latitude_textview"));
String type=cr.getString(cr.getColumnIndex("longitude_textview"));
String qty=cr.getString(cr.getColumnIndex("textView1"));
String remark=cr.getString(cr.getColumnIndex("textView6"));
//li.add(no);
li.add(no);
li.add(name);
li.add(type);
li.add(qty);
li.add(remark);
gvproduct.setAdapter(dataAdapter);
}while (cr.moveToNext());
}else{
Toast.makeText(getApplicationContext(), "No Data to show", Toast.LENGTH_LONG).show();
}
}
}catch (Exception e){
Toast.makeText(ShowProduct.this, e, Toast.LENGTH_SHORT).show();
}
}
}
Related
Hi everyone,
I'm new in developing and i use android studio for developing my first apps that check if my device is rooted or not and collect information about device like( os version, manufacturer, model ).
My problem is that i need to integrating in my class that already created and successfully worked a http get request to display this information in http locally. ?
How can do this ?
pls note that i do not need anything more complex just a simple way to display the result in HTTP locally.
Thank you in advance
Regadrs
There is my class :
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class Sofiane_Byod_Service_activity_main extends ActionBarActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sofiane__byod__se rvice_activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button = (Button) findViewById(R.id.button);
final TextView Text1 = (TextView) findViewById(R.id.textView5);
final TextView Text2 = (TextView) findViewById(R.id.TextView09);
final TextView Text3 = (TextView) findViewById(R.id.TextView06);
final TextView Text4 = (TextView) findViewById(R.id.TextView05);
button1.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
String command[] = {"su", "-c", "ls", "/data"};
Shell_Byod shell = new Shell_Byod();
String text = shell.sendShellCommand(command);
if ((text.indexOf("app") > -1) || (text.indexOf("anr") > -1)
|| (text.indexOf("user") > -1)
|| (text.indexOf("data") > -1)) {
button2.setText("Rooted");
} else {
button2.setText("Not Rooted");
}
String curModel = Build.MODEL;
String curMan = Build.MANUFACTURER;
String curDevice = Build.DEVICE;
String curVer = Build.VERSION.RELEASE;
Text1.setText(curDevice);
Text2.setText(curModel);
Text3.setText(curMan);
Text4.setText(curVer);
}
});
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
hi
Im new in android studio,
I´m trying to send to a db but it not works and i dont know why. when i click the button it does nothing.
can somebody help?
thanks
my code:
Code:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class WebService extends Activity {
private EditText dni;
private EditText nombre;
private EditText telefono;
private EditText email;
private Button insertar;
private Button mostrar;
private ImageButton mas;
private ImageButton menos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setContentView(R.layout.fragment2);
dni=(EditText)findViewById(R.id.dni);
nombre=(EditText)findViewById(R.id.nombre);
telefono=(EditText)findViewById(R.id.telefono);
email=(EditText)findViewById(R.id.email);
insertar=(Button)findViewById(R.id.insertar);
insertar.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!dni.getText().toString().trim().equalsIgnoreCase("")||
!nombre.getText().toString().trim().equalsIgnoreCase("")||
!telefono.getText().toString().trim().equalsIgnoreCase("")||
!email.getText().toString().trim().equalsIgnoreCase(""))
new Insertar(WebServiceExample.this).execute();
else
Toast.makeText(WebServiceExample.this, "Hay informaci�n por rellenar", Toast.LENGTH_LONG).show();
}
});
}
private boolean insertar(){
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
HttpPost httppost;
httpclient=new DefaultHttpClient();
httppost= new HttpPost("myhost");
nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("dni",dni.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("nombre",nombre.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("telefono",telefono.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString().trim()));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
class Insertar extends AsyncTask<String,String,String>{
private Activity context;
Insertar(Activity context){
this.context=context;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
if(insertar())
context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Persona insertada con �xito", Toast.LENGTH_LONG).show();
nombre.setText("");
dni.setText("");
telefono.setText("");
email.setText("");
}
});
else
context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Persona no insertada con �xito", Toast.LENGTH_LONG).show();
}
});
return null;
}
}
}
Hi everybody,
Im in need of help, Ive been attempting to create an qoute application and ive been coming up with an error when i try to run it.
Here is the error report i get.
Information:Gradle: Executing tasks: [assemble]
Information:24/03/2016, 18:33 - Compilation completed with 2 errors and 0 warnings in 8s 478ms
Error:Gradle: Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
/Users/David/AndroidStudioProjects/David'sMotivation2/app/src/main/java/com/example/david/davidsmotivation/Splash.java
Error: (16, 9) Gradle: error: reached end of file while parsing.
here is the code where it shows that there is an bug/error.
package com.example.david.davidsmotivation;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Splash extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Papyrus.ttc");
TextView myTextview = (TextView) findViewById(R.id.textView1);
myTextview.setTypeface(myTypeface);
{
Im sorry if this isn't clear, please say if i should include more information.:good:
are u sure its is Papyrus.ttc ? usualy typefaces ends with .ttf extension
Sent from my Nexus 4 using Tapatalk
Did you post the complete Splash.java file. Because the code you posted is incomplete
Code:
package com.example.david.davidsmotivation;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Splash extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Papyrus.ttc"); [COLOR="RoyalBlue"]// Is this correct ? Fonts are generally '.ttf' files[/COLOR]
TextView myTextview = (TextView) findViewById(R.id.textView1);
myTextview.setTypeface(myTypeface);
[COLOR="Red"][B]}
}[/B][/COLOR]
hello, im having trouble moving a chathead with a button, no matter how much i try it always crashes my app
here is the main activity
im using MoveHead but i dant make it to work
Code:
package example.com.chatheads;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.sip.SipAudioCall;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.Service;
import example.com.chatheads.ChatHeadService.*;
public class MainActivity extends Activity {
private final int xx = 500;
private final int yy = 500;
Button startService,stopService, moveService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService=(Button)findViewById(R.id.startService);
stopService=(Button)findViewById(R.id.stopService);
moveService=(Button)findViewById(R.id.moveService);
startService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(getApplication(), ChatHeadService.class));
//startService(new Intent(getApplication(), HepUstte.class));
}
});
stopService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(getApplication(), ChatHeadService.class));
}
});
moveService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//System.out.println("Hello");
ChatHeadService inst = new ChatHeadService();
inst.UpdateEmployee();
}
});
}
and here is the chatheadservice class
Code:
package example.com.chatheads;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ChatHeadService extends Service {
public WindowManager windowManager;
public ImageView chatHead;
WindowManager.LayoutParams params;
int xi ;
int yi ;
@Override
public void onCreate() {
super.onCreate();
System.out.println("Hello2");
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.face1);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
chatHead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
params.x = xi;
params.y = yi;
windowManager.updateViewLayout(chatHead, params);
}
});
windowManager.addView(chatHead, params);
}
public void MoveHead(){
System.out.println("Hello");
WindowManager.LayoutParams params = (WindowManager.LayoutParams) chatHead.getLayoutParams();
params.x = 100;
params.y = 300;
windowManager.updateViewLayout(chatHead, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null)
windowManager.removeView(chatHead);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
I'm having a class Mqtt with this code as below :
Code:
package com.example.myhome;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import java.io.UnsupportedEncodingException;
public class Mqtt {
public MqttAndroidClient mqttAndroidClient;
final String serverUri = "tcp://***";
final String clientId = "***";
final String username = "***";
final String password = "***";
public Mqtt(Context context) {
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
mqttAndroidClient.setCallback(new MqttCallback() {
//@Override
public void connectComplete(boolean b, String s) {
Log.w("mqtt", s);
}
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
Log.w("Mqtt", mqttMessage.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
connect();
}
public void setCallback(MqttCallback callback) {
mqttAndroidClient.setCallback(callback);
}
private void connect() {
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
//mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
try {
mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
//DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
// disconnectedBufferOptions.setBufferEnabled(true);
// disconnectedBufferOptions.setBufferSize(100);
//disconnectedBufferOptions.setPersistBuffer(false);
//disconnectedBufferOptions.setDeleteOldestMessages(false);
// mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
subscribeToTopic("esp/test");
//publishToTopic("esp/test","POPdd");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.w("Mqtt", "Failed to connect to: " + serverUri + exception.toString());
}
});
} catch (MqttException ex) {
ex.printStackTrace();
}
}
private void subscribeToTopic(String Topic) {
try {
mqttAndroidClient.subscribe(Topic, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.w("Mqtt", "Subscribed!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.w("Mqtt", "Subscribed fail!");
}
});
} catch (MqttException ex) {
System.err.println("Exceptionst subscribed");
ex.printStackTrace();
}
}
public void publishToTopic(MqttAndroidClient client,String Topic,String MessageForTopic) {
String payload = MessageForTopic ;
byte[] encodedPayload = new byte[0];
try {
encodedPayload = payload.getBytes("UTF-8");
MqttMessage message = new MqttMessage(encodedPayload);
client.publish(Topic, message,null,new IMqttActionListener() {
// @Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.w("Mqtt", "Published!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.w("Mqtt", "Published fail!");
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MqttPersistenceException e) {
e.printStackTrace();
} catch (MqttException e) {
e.printStackTrace();
}
}
}
i have also a class Hoofdmenu (code below) with a layout. Here i want to send a message into the mqtt
=> MqttCommunicatie.publishToTopic(MqttCommunicatie.mqttAndroidClient,"esp/test","Message MQTT");
when i publishToTopic after the subscribe into class Mqtt the i receive the message when i ask into the class Hoofdmenu it doens't work
what do i do wrong ? Or wht can i do to to it work ?
now receive the message "MyHome is stopped Open app again"
Code:
package com.example.myhome;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.myhome.HoofdmenuTabs.PagerAdapter;
import com.google.android.material.tabs.TabLayout;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import java.io.UnsupportedEncodingException;
public class Hoofdmenu extends AppCompatActivity {
public Mqtt MqttCommunicatie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hoofdmenu);
TabLayout tabMenu = (TabLayout) findViewById(R.id.tabMenu);
final ViewPager viewPager = (ViewPager) findViewById(R.id.tabpager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabMenu.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabMenu));
tabMenu.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
startMqtt();
MqttCommunicatie.publishToTopic(MqttCommunicatie.mqttAndroidClient,"esp/test","Message MQTT");
}
private void startMqtt(){
MqttCommunicatie = new Mqtt(getApplicationContext());
MqttCommunicatie.setCallback(new MqttCallback() {
//@Override
public void connectComplete(boolean b, String s) {
}
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
Log.w("Debug", mqttMessage.toString());
//dataReceived.setText(mqttMessage.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
}