i have a problem..
Here is the code:
private final class AsyncSenderAll extends AsyncTask<SimpleCursorAdapter, Void, SimpleCursorAdapter>{
#Override
protected void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(PilotLogbook_viewdrafts.this);
pd.setTitle("Drafts");
pd.setMessage("Please wait, refreshing drafts...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.show();
}
#Override
protected SimpleCursorAdapter doInBackground(SimpleCursorAdapter... params) {
DBAdapter DBHelper = new DBAdapter(PilotLogbook_viewdrafts.this);
DBHelper.open();
Cursor cursor = DBHelper.getAllDrafts();
// The desired columns to be bound
#SuppressWarnings("static-access")
String[] columns = new String[] {
DBHelper.KEY_DATE,
DBHelper.KEY_PIC_NAME,
DBHelper.KEY_AIRCRAFT_REGISTRATION,
DBHelper.KEY_DEPARTURE_PLACE,
DBHelper.KEY_ARRIVAL_PLACE
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.lblLayout_Draft_Date,
R.id.lblLayout_Draft_PIC_Name,
R.id.lblLayout_Draft_Aircraft_Registration,
R.id.lblLayout_Draft_Departure_Place,
R.id.lblLayout_Draft_Arrival_Place
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.layout_pilotlogbook_viewdrafts,
cursor,
columns,
to,
0);
return dataAdapter;
}
#Override
protected void onPostExecute(SimpleCursorAdapter dataAdapter){
super.onPostExecute(dataAdapter);
ListView listView = (ListView) findViewById(R.id.listAllDrafts);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
int RowID = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
Bundle bundle = new Bundle();
bundle.putInt("RowID",RowID);
Intent newIntent = new Intent(getApplicationContext(), PilotLogbook_draftdetails.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
finish();
}
});
pd.dismiss();
}
}
I wanna to use a loading dialog while the listview is loading data from database. The problem is that i get
The constructor SimpleCursorAdapter(PilotLogbook_viewdrafts.AsyncSenderAll, int, Cursor, String[], int[], int)
is undefined
at
dataAdapter = new SimpleCursorAdapter(
this, R.layout.layout_pilotlogbook_viewdrafts,
cursor,
columns,
to,
0);
And i don't know how to fix it. Can you guys help me with this part of the code? Thanks in advance! :D
Try this
dataAdapter = new SimpleCursorAdapter(
PilotLogbook_viewdrafts.this, R.layout.layout_pilotlogbook_viewdrafts,
cursor,
columns,
to,
0);
You are using this in the wrong place, in this case this means AsyncSenderAll class, but the adapter requires a context so try to put your Activity class there. For example MainActivity.this:
dataAdapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.layout_pilotlogbook_viewdrafts,
cursor,
columns,
to,
0);
Hope this helps.
Related
I need some help to add an onItemClickListener to a ListView of contacts. I want to capture some information from the selected contacts like name, phonenumbers and other info and pass it to another activity.
I'm not sure how to add a onListItemListener to my code below and how to capture info from the selected contact? I also wonder how to pass more than one value with an Intent? I use this for one value:
Intent i = new Intent(Activity_1.this, Activity_2.class);
startActivityForResult(i, 1);
Intent intent = new Intent();
intent.putExtra("imageId", imagePath);
setResult(RESULT_OK, intent);
finish();
And here are my code in the activity with the ListView. I got some help here yesterday and I wonder if the while (cursor.moveToNext()){} part of the code is important for the ListView since it works without it?
Preciate some help! Thanks!
public class Activity_3 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
listView=(ListView)findViewById(R.id.contactList);
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID};
// Get a cursor with all people
Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);
//Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);
String[] fromColumn = {ContactsContract.Contacts.DISPLAY_NAME};
int[] toView = {R.id.contactItem };
while (cursor.moveToNext())
{
String Name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String Number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor, fromColumn, toView );
listView.setAdapter(adapter);
}
}
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view , int position,
long id) {
// TODO Auto-generated method stub
do your operations here...
}
});
use this onitemclicklistener to listen to your listview clicks .
My ListView currently is able to display only one column data. I want it to display two column data.
Here is the code for my ListView:
public class ProjectExplorer extends ListActivity {
private projectdatabase database;
protected Cursor cursor;
protected ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = new projectdatabase(ProjectExplorer.this);
getinfo();
}
private void getinfo() {
database.open();
cursor = database.getDataforDisplay();
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0);
setListAdapter(adapter);
}
#Override
public void onListItemClick(ListView parent, View view, int position, long id) {
super.onListItemClick(parent, view, position, id);
Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
c.moveToPosition(position);
// get project name here
String str_projectname= c.getString(c.getColumnIndex("project_name"));
Toast.makeText(this, str_projectname, Toast.LENGTH_LONG).show();
}
Here is the method in database class which return the cursor:
public Cursor getDataforDisplay () {
String[] columns = new String[] {KEY_ROWID, PROJECT_NAME, PROJECT_FINISH_DATE, PROJECT_DIFFICULTY, PROJECT_STATUS};
Cursor c = projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
return c;
}
I also want to display KEY_ROWID which is defined as _id
You can use android.R.layout.simple_list_item_2 specifying column no.2 from table from where data can be retrieved and specifies location with help of it can be displayed on Activity - android.R.id.text2
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[] {"project_name","column_no2"}, new int[] { android.R.id.text1, andriod.R.id.text2 }, 0);
You can also create your on custom Adapter specifying your own Listview and creating it from scratch and customized everything.
The best way to achieve this at least in my opinion is to create custom Adapter for your ListView in that case you can set your own design how single listview element should look like . Just populate some arrays using your Cursor and set them to your custom adapter.
So in this code, I am trying to retrieve a cursor based on the query passed through my showResults() and then create an adapter and loadermanager. I feel like the problem is being caused with the layout and id within my SimpleCursorAdapter constructor because the error started when I created the layout and id. I used Log and if statement to see whether it was the cursor that was null and nothing showed up on the logcat, so that must mean by cursor is fine.
public class SearchResultsActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView list;
private DatabaseTable db;
private SimpleCursorAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
db = new DatabaseTable(this);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
db = new DatabaseTable(this);
Cursor cursor = db.getContactMatches(query, null);
list = (ListView)findViewById(android.R.id.list);
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
getLoaderManager().initLoader(0, null, this);
list.setAdapter(mAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent contactIntent = new Intent(getApplicationContext(), ContactActivity.class);
contactIntent.setData(getIntent().getData());
startActivity(contactIntent);
}
});
}
Since your code is crashing on the line list.setAdapter(mAdapter);, list is the only object that it makes sense to give you a null pointer.
Upon further examination, it is clear you are not assigning list after it is initially declared. You need to add something like this in onCreate(), after setContentView():
list = (ListView) findViewById(android.R.id.list);
(This is how you'd access it if your object had android:id="#android:id/list"; if you assigned your own ID, use R.id.your_id_here instead.)
this is probably your error here:
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
that adapter requires a Cursor passed into the constructor at the spot where you passed null in for the parameter, like this:
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
cursor, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
Try getting a readable database first before you query it:
db = new DatabaseTable(this);
Cursor cursor = db.getReadableDatabase().getContactMatches(query, null);
I am using database in my application,in that i have fetched data from database,but i dont know how to display it in the textview.
Here is my code:
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
db.open();
Cursor c = db.getAllTitles();
startManagingCursor(c);
String[] from = new String[] { db.KEY_INCOME };
int[] to = new int[] { R.id.textView1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.report, c, from, to);
Here the notes have the database value.But don't know how to proceed how to display it in listview.
do like this
String[] titles= new String[] {c.TITLE };
int[] view = new int[] { R.id.textview };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, c, titles, view );
listview.setAdapter(notes);
You need to find your listview and set your adapter. (Assuming you are not using a ListActivity)
ListView list = (ListView) this.findViewById(R.id.yourlistview);
list.setAdapter(notes);
If you are using a ListActivity (or ListFragment), it's even simpler as the framework assumes you are using a certain id and you can just set the adapter like so:
setListAdapter(notes);
if you want to set set Tesxt on list item click you can do iy like
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
tv.setTextt(c.getString(1));
}
I want to be able to fetch a row from a list view and populate textViews in a different activity. I want to be able to do this onclick of an item with the list view..
public class CBFilter extends Activity {
ListView RecipeNames;
Cursor cursor;
SimpleCursorAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RecipeNames = (ListView) findViewById(R.id.List1);
RecipeNames.setTextFilterEnabled(true);
RecipeText = (TextView) findViewById(R.id.recipeText);
adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
String[] from = {CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.recipeText};
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void CreateNew(View view){
Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}
}
How can i convert this code in order to achieve the required functionality.. thanks Stefan
try like this
public void onListItemClick(ListView parent, View v, int position, long id) {
String string = from[position]); // this depends on your Adapter ...
Intent i = new Intent(CheckData.this,ShowData.class);
i.putExtra("SELECTED", string);
startActivity(i);
i got it from this link , it will be use full for you :
Use of SQLite