i want to affect data to my textview from my database in sql server but i don't arrive to do it and this is my code
SpinCom.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
try {
List<Model> list = new ArrayList<Model>();
String descript = "";
String query = "select * from centre where commune = '" + SpinCom + "'";
PreparedStatement stmt;
ResultSet rs;
Connection con = connectionClass.CONN();
stmt = con.prepareStatement(query);
rs = stmt.executeQuery();
// ResultSetMetaData rsmd = rs.getMetaData();
ArrayList<String> data = new ArrayList<String>();
// ListView adapter = new ListView(this, list);
// listView.setAdapter(adapter);
while (rs.next()) {
String id2 = rs.getString("commune");
data.add(id2);
Model obj = new Model();
obj.Ncentre(rs.getInt(1));
obj.Ndescript(rs.getString(2));
obj.Nrespons(rs.getString(3));
list.add(obj);
Showdescription.setText(list.get(position).Ncentre());
Showresponsable.setText(list.get(position).Ndescript());
Showquartier.setText(list.get(position).Nrespons());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
please i want your help
it is advisible to fetch data from any remote server using web service or else you cant fetch data from server
Related
I am using a spinner in my program and it gets data from my azure SQL database, but when i clicked the drop down nothing happens. Here is my code
try {
ConnectionHelper conStr = new ConnectionHelper();
connect = conStr.connectionclass();
String query = "select * from fabric";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
ArrayList<String> data = new ArrayList<String>();
while (rs.next()) {
String id = rs.getString("FABRIC_NAME");
data.add(id);
}
String[] array = data.toArray(new String[0]);
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, data);
Fabric.setAdapter(NoCoreAdapter);
} catch (SQLException e) {
e.printStackTrace();
}
Fabric.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String name = Fabric.getSelectedItem().toString();
Toast.makeText(Calc_140_plain.this, name, Toast.LENGTH_SHORT)
.show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
i cant seem to find an error in LogCat as well
Make sure your data array has some element
while (rs.next()) {
String id = rs.getString("FABRIC_NAME");
data.add(id);
}
Log.d("data","size:"+data.size());
print size after while to check data is not empty.
Try this:
ArrayAdapter NoCoreAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, data);
instead of:
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, data);
I'm thinking how to show data at the customize listview page. can you guys help me to find out where is the mistakes?
here is the assignment
Model txn;
public SQLiteHelper mSQLiteHelper;
ListView mListView;
ArrayList<Model> mList;
RecordListAdapter mAdapter = null;
this is the initialization
mListView = findViewById(R.id.listView);
mList = new ArrayList<>();
mAdapter = new RecordListAdapter(this, R.layout.row, mList);
mListView.setAdapter(mAdapter);
mSQLiteHelper = new SQLiteHelper(this);
mList = new ArrayList<>();
This is my page which showing all data, I put a debugger to debug. It can get my all data which have 15 columns, But it can't show at the listview page. is there any mistake at this code?
try {
SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from Table1", null);
mList.clear();
while (cursor.moveToNext()) {
txn = new Model();
txn.setId(cursor.getInt(cursor.getColumnIndex("id")));
txn.setName(cursor.getString(cursor.getColumnIndex("name")));
txn.setAddress(cursor.getString(cursor.getColumnIndex("address")));
txn.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
mList.add(new Model());
}
mAdapter.notifyDataSetChanged();
if (mList.size() == 0) {
Toast.makeText(this, "No record found...", Toast.LENGTH_SHORT).show();
}
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
Please check below code
mListView = findViewById(R.id.listView);
mSQLiteHelper = new SQLiteHelper(this);
mList = new ArrayList<>();
mAdapter = new RecordListAdapter(this, R.layout.row, mList);
mListView.setAdapter(mAdapter);
Data binding function code
try {
SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from Table1", null);
mList.clear();
while (cursor.moveToNext()) {
txn = new Model();
txn.setId(cursor.getInt(cursor.getColumnIndex("id")));
txn.setName(cursor.getString(cursor.getColumnIndex("name")));
txn.setAddress(cursor.getString(cursor.getColumnIndex("address")));
txn.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
mList.add(txn);
}
mAdapter.notifyDataSetChanged();
if (mList.size() == 0) {
Toast.makeText(this, "No record found...", Toast.LENGTH_SHORT).show();
}
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
and I have one more suggestion, do not add listview click listener inside of your data binding function.
I need to fetch data from my database here is my code for fetching data
Connect.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
try{
Connection con = connectionClass.CONN();
String query = "SELECT * FROM Driver WHERE UID='" + UID + "'";
Statement stmt = con.prepareStatement(query);
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
UDI = rs.getString("FirstName");
TextView Name = (TextView) findViewById(R.id.name);
Name.setText(UDI);
}
}
catch (Exception ex)
{
}
}
});
The problem is, whenever I click the button, nothing is being fetched.
I am populating spinner from below code and its working perfect. When user select any value from spinner then I am saving stateCodeId in sqlite. Now what I want that when user come again then I want to show the seleted value from ID which already saved in sqlite. How can I show value selected in spinner ?
public void fillStateData() {
try {
State_data = new ArrayList<Map<String, String>>();
State_data.clear();
Cursor cursor_State = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);
int i = 0;
if (cursor_State.moveToFirst()) {
do {
Map<String, String> datanum = new HashMap<String, String>();
if (i == 0) {
datanum.put("nStateID", "0");
datanum.put("cStateName", "Select State");
State_data.add(datanum);
datanum = new HashMap<String, String>();
datanum.put("nStateID", cursor_State.getString(0));
datanum.put("cStateName", cursor_State.getString(1));
State_data.add(datanum);
} else {
datanum.put("nStateID", cursor_State.getString(0));
datanum.put("cStateName", cursor_State.getString(1));
State_data.add(datanum);
}
i += 1;
} while (cursor_State.moveToNext());
}
String[] fromwhere = {"nStateID", "cStateName"};
int[] viewswhere = {R.id.txtnStateID, R.id.txtcStateName};
StateAdapter = new SimpleAdapter(getActivity(), State_data, R.layout.state_list_template, fromwhere, viewswhere);
spnState.setAdapter(StateAdapter);
cursor_State.close();
spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView stateId = (TextView) view.findViewById(R.id.txtnStateID);
stateCodeId = stateId.getText().toString();
fillDistrictData(stateCodeId);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Like this I am getting ID from sqlite.
Cursor c = db.rawQuery("SELECT nStateId from MemberMaster where nCustID ="+SesPMbrID+"", null);
c.moveToFirst();
String state = c.getString(0);
Use
spinner.setSelection(position);
I have a listview which connect to an sqlite database. at first it shows my sqlite fields. but my problem is,I can't delete row by setOnLongClickDelete().
error:
The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1510)
codes:
public class CartList extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(com.example.easyshopping.R.layout.cart);
openAndQueryDatabase();
displayResultList();
setOnLongClickDelete();
}
private void displayResultList() {
// setListAdapter(new ArrayAdapter<String>(this,R.layout.cartformat,results));
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.cartformat,results);
setListAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
Cursor c = database.rawQuery("SELECT title,qty,price FROM CART;", null);
if (c != null ) {
int totalPrice=0;
if (c.moveToFirst()) {
do {
String title = c.getString(c.getColumnIndex("title"));
int qty = c.getInt(c.getColumnIndex("qty"));
int price = c.getInt(c.getColumnIndex("price"));
int pricePerTitle=price*qty;
results.add("Title: " +title+ ",Quantity: "+qty+", Price: $"+pricePerTitle);
totalPrice=totalPrice+pricePerTitle;
}while (c.moveToNext());
}
TextView tTotalPrice=(TextView)findViewById(com.example.easyshopping.R.id.txttotalprice);
String total= Integer.toString(totalPrice);
tTotalPrice.setText(total);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
private void setOnLongClickDelete(){
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
try{ String currentString = results.get(position);
String resultRegexString = "Title\\:([^,]+),Quantity\\: ([^,]+), Price\\: \\$([\\W\\w]+)";
Pattern resultRegexPattern = Pattern.compile(resultRegexString);
Matcher resultRegexMatcher = resultRegexPattern.matcher(currentString);
if(resultRegexMatcher.find()){
String whereClause = "title=".concat(DatabaseUtils.sqlEscapeString(resultRegexMatcher.group(1))
.concat(" AND qty=").concat(resultRegexMatcher.group(2))
.concat(" AND price=").concat(resultRegexMatcher.group(3)));
SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
database.delete("CART", whereClause, null);
database.close();
Toast.makeText(getApplicationContext(), "Row Delete", Toast.LENGTH_SHORT).show();
results.remove(position);
}
return true;
} catch (Exception ex){
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
return false;
}
}
} );
displayResultList();
}
}
following Toast works:
Toast.makeText(getApplicationContext(), "Row Delete", Toast.LENGTH_SHORT).show();
If you are able to successfully remove the data from the SQLite database then you simply make your listAdapter global and then adding the code
listAdapter.notifyDataSetChanged();
after
results.remove(position);
in your onItemLongClick. After this you can remove calling displayResultList(); in setOnLongClickDelete().