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);
Related
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 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
Well I have an arraylist from my sqlite data. And I'm setting this arraylist on my arrayadapter of my spinner.
Here's how I do it:
public ArrayList<AttendantModelNames> getAllAttendantNames() {
ArrayList<AttendantModelNames> attendantModelArrayList = new ArrayList<>();
hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_ATTENDANTS, null);
res.moveToFirst();
while (!res.isAfterLast()) {
AttendantModelNames l_att = new AttendantModelNames();
l_att.setname(res.getString(res
.getColumnIndex(KEY_NAME)));
attendantModelArrayList.add(l_att);
res.moveToNext();
}
Log.d(TAG, attendantModelArrayList.toString());
res.close();
return attendantModelArrayList;
}
Then my AttendantModelNames POJO:
public class AttendantModelNames {
String name;
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
}
Then on my spinner:
private void setSpinner() {
spinner = (Spinner) findViewById(com.duka.R.id.spinner_toolbar);
ArrayList<AttendantModelNames> modelArrayList = ah.getAllAttendantNames();
ArrayAdapter<AttendantModelNames> dataAdapter =
new ArrayAdapter<AttendantModelNames>(this, android.R.layout.simple_spinner_item, modelArrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
int selectionPosition= dataAdapter.getPosition(modelArrayList.get(position).getname());
spinner.setSelection(selectionPosition);
I tried setting this but doesn't seem to work, says:
get(int) in arraylist cannot be applied to (java.lang.String) on
position
Any suggestions will be appreciated.
This is how i resolved my issue:
I started out by returning an list of type string:
public List<String> getAllAttendantNames() {
List<String> list = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ATTENDANTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);//selectQuery,selectedArguments
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(2));//adding 2nd column data
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
// returning lables
return list;
}
Then set up spinner:
private void loadSpinnerData() {
List<String> labels = ah.getAllAttendantNames();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labels);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
int spinnerPosition = dataAdapter.getPosition(s_attname);
Log.d("spinner_set_selection", s_attname + spinnerPosition);
spinner.setSelection(spinnerPosition);
}
And ofcourse onCreate():
spinner = (Spinner) findViewById(R.id.spinner);
// Loading spinner data from database
loadSpinnerData();
I have a little issue that I don't know how to address.
I have a Cursor that gets data from my data base it looks like this:
public Cursor getAllData(){
int id;
SQLiteDatabase DB = this.getWritableDatabase();
String dateTime = Helper.getCurrentDate();
String date = dateTime.substring(0, 10);
String date_to_look = date.substring(0, 10);
String sql = "SELECT h.product_id, h.qty, h.date_time, p.product " +
" FROM product_history h " +
" LEFT JOIN products p ON (p.rowid = h.product_id) ";
Cursor res = DB.rawQuery(sql, null);
return res;
}
Then on my activity I'm using the following method:
public void populateListView() {
Cursor cursordata = myDb.getAllData();
String productname = cursordata.getString(cursordata.getColumnIndex("product"));
String [] products = {productname};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, products);
ListView listviewman = (ListView)findViewById(R.id.listviewman);
listviewman.setAdapter(adapter);
}
Execute it with onclick:
public void shine(View v){
Button shine = (Button)findViewById(R.id.shine);
populateListView();
}
What am I doing wrong?
Edit:
Shows last row from the table only.
public void populateListView() {
Cursor cursordata = myDb.getAllData();
if (cursordata.moveToFirst()) {
do {
String productname = cursordata.getString((cursordata.getColumnIndex("product")));
String [] products = {productname};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, products);
ListView listviewman = (ListView)findViewById(R.id.listviewman);
listviewman.setAdapter(adapter);
} while (cursordata.moveToNext());
}
Sahil here is the updated version of your suggestion which i use, it's not working:
public void populateListView() {
Cursor cursordata = myDb.getAllData();
List<String> li_records=new ArrayList<String>;
if (cursordata.moveToFirst()) {
do {
String productname = cursordata.getString((cursordata.getColumnIndex("product")));
String[] products = {productname};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, products);
ListView listviewman = (ListView) findViewById(R.id.listviewman);
listviewman.setAdapter(adapter);
li_records.add(productname);
} while (cursordata.moveToNext());
}
String[] products=li_records.toArray();
}
try this it will work fine:
public void populateListView() {
Cursor cursordata = myDb.getAllData();
ArrayList<String> products = new ArrayList<>();
if (cursordata.moveToFirst()) {
do {
String productname = cursordata.getString((cursordata.getColumnIndex("product")));
products.add(productname);
} while (cursordata.moveToNext());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, products);
ListView listviewman = (ListView)findViewById(R.id.listviewman);
listviewman.setAdapter(adapter);
}
Try to put
cursordata.moveToFirst()
after
List<String> li_records=new ArrayList<String>;
Cursor cursordata = myDb.getAllData();
use do while loop like this,
// looping through all rows and adding to list
if (cursordata.moveToFirst()) {
do {
String productname = c.getString((c.getColumnIndex("product")));
li_records.add(productname);
} while (cursordata.moveToNext());
}
String[] products=li_records.toArray();
use this array in your adapter
I am using listview in android. Previously i used to pull data from database so I used CursorAdapter but now I am fetching response from website and on the website I have different links and each link has its name associated with it. I have names and links in two arrays of string. I have to show only the names and when clicked each name will open a same new intent with link as parameter.
Here is what I am doing
listView = (ListView) findViewById(R.id.list);
String[] names= new String[] { "name1",
"name2"
};
String[] links= new String[] { "link1",
"link2"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, send names and links);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String itemValue = (String) listView.getItemAtPosition(position);
Intent myIntent = new Intent(arg0.getContext(), NewPage.class);
myIntent.putExtra("Url",how to get this);
startActivity(myIntent);
}
});
}
Wrap the two pieces of information in a custom object:
class Data {
String name;
String link;
#Override
public String toString() {
return name;
}
}
Building the list of data items:
String[] names= new String[] { "name1",
"name2"
};
String[] links= new String[] { "link1",
"link2"
};
List<Data> output = new ArrayList<Data>();
for (int i = 0; i < names.length; i++) {
Data d = new Data();
d.name = names[i];
d.link = links[i]
output.add(d);
}
ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, output);
You can then use the Data in the OnItemClickListener:
Data item = (Data) listView.getItemAtPosition(position);
Intent myIntent = new Intent(arg0.getContext(), NewPage.class);
myIntent.putExtra("Url", d.link);
startActivity(myIntent);