I have done the notepad tutorial from android webside. I added an own Module-Class. Now I also want to add a own baseadapter. But I have problems with the implementation.
My problem is the fillData() method. It is in the third code part. I am also not sure, if I will need a cursor?
I hope anybody can help me, to correct the fillData() method.
My Module-Class
public class Module {
private String title;
private String device_type;
private String home_code;
private String device_code;
public Module(String n, String m, String hc, String mc) {
title = n;
device_type = m;
home_code = hc;
device_code = mc;
}
public String getTitle() { return title; }
public String getDeviceType() { return device_type; }
public String getHomeCode() { return home_code; }
public String getDeviceCode() { return device_code; }
}
My Module Adapter:
public class ModuleAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Module> listModule;
public ModuleAdapter(Context context, List<Module> listModule) {
this.context = context;
this.listModule = listModule;
}
public int getCount() {
return listModule.size();
}
public Object getItem(int position) {
return listModule.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Module entry = listModule.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.notes_row, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.text1);
tvTitle.setText(entry.getTitle());
TextView tvDeviceType = (TextView) convertView.findViewById(R.id.text2);
tvDeviceType.setText(entry.getDeviceType());
TextView tvHomeCode = (TextView) convertView.findViewById(R.id.text3);
tvHomeCode.setText(entry.getHomeCode());
TextView tvDeviceCode = (TextView) convertView.findViewById(R.id.text4);
tvDeviceCode.setText(entry.getDeviceCode());
return convertView;
}
#Override
public void onClick(View view) {
Module entry = (Module) view.getTag();
listModule.remove(entry);
// listModule.remove(view.getId());
notifyDataSetChanged();
}
private void showDialog(Module entry) {
// Create and show your dialog
// Depending on the Dialogs button clicks delete it or do nothing
}
}
The fillData() method from the main code:
private void fillData() {
//Cursor notesCursor = mDbHelper.fetchAllNotes();
//startManagingCursor(notesCursor);
final List<Module> from = new ArrayList<Module>();
from.add(new Module(NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_DEVICETYPE, NotesDbAdapter.KEY_HOMECODE, NotesDbAdapter.KEY_DEVICECODE));
// Now create a simple cursor adapter and set it to display
//SimpleCursorAdapter notes =
// new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
//notes.setViewBinder(new ModuleViewBinder());
ModuleAdapter adapter = new ModuleAdapter(this, from);
setListAdapter(adapter);
}
Thanks a lot!
Felix
The problem is your data is in a database, so you should keep using the SimpleCursorAdapter as you were doing in the code you commented out.
Your new code puts a module filled in with the names of the database columns (and not the actual data) in the ArrayList:
from.add(new Module(NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_DEVICETYPE, NotesDbAdapter.KEY_HOMECODE, NotesDbAdapter.KEY_DEVICECODE));
Then your custom adapter works correctly, because it displays those values.
What probably got you confused is that you were passing the same strings to the SimpleCursorAdapter, but that adapter uses the column names to fetch data from the database. Your custom adapter instead simply shows what's in the list.
If you want to show the values from the database you should stick to SimpleCursorAdapter (or extend it if you need to do more).
Related
Im new in realm db. I completed add and get data in realm db. But, I couldn't sort(ascending and descending).Im my code it display items in listview. Listview contains 5 list and each list contains 4 field(name, age, skill and date). if I sort(ascending) name, need to ascending in 5 list.My code is not work
I post my code here,
private void Ascending_order() {
realm.beginTransaction();
RealmResults<Employee> result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
realm.copyFromRealm(result);
realm.commitTransaction();
employeedetailadapter.notifyDataSetChanged();
}
Adapter class:
public class EmployeeDetailAdapter extends BaseAdapter {
private ArrayList<Employee>employeeDetaillists = new ArrayList<>();
private Context c;
private LayoutInflater inflater;
private OnItemClick mCallback;
private SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");
public EmployeeDetailAdapter(Context c,ArrayList<Employee> employeeDetaillists, OnItemClick listener) {
this.employeeDetaillists = employeeDetaillists;
this.c= c;
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mCallback = listener;
}
#Override
public int getCount() {
return employeeDetaillists.size();
}
#Override
public Object getItem(int position) {
return employeeDetaillists.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder holder;
if (v==null){
v= (View) inflater.inflate(R.layout.list_single_item,null);
holder = new Holder();
holder.tvPersonName = (TextView) v.findViewById(R.id.tvPersonName);
holder.tvPersonAge = (TextView) v.findViewById(R.id.tvPersonAge);
holder.tvPersonSkill = (TextView) v.findViewById(R.id.tvPersonSkill);
holder.ivEditPesonDetail=(ImageView)v.findViewById(R.id.ivEditPesonDetail);
holder.tvPersondate=(TextView)v.findViewById(R.id.tvPersondate);
holder.ivDeletePerson=(ImageView)v.findViewById(R.id.ivDeletePerson);
v.setTag(holder);
}else{
holder = (Holder) v.getTag();
}
holder.tvPersonName.setText(employeeDetaillists.get(position).getName());
holder.tvPersonAge.setText(employeeDetaillists.get(position).getAge());
holder.tvPersonSkill.setText(employeeDetaillists.get(position).getSkill());
String strDate = df.format(employeeDetaillists.get(position).getSdate());
holder.tvPersondate.setText(strDate);
holder.ivDeletePerson.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Delete(employeeDetaillists.get(position).getName(),position);
}
});
return v;
}
private void Delete(String name, int position) {
mCallback.onClickdelete(name, position);
}
public void updateData(RealmResults<Employee> result) {
}
class Holder {
TextView tvPersonName, tvPersonAge, tvPersonSkill,tvPersondate;
ImageView ivDeletePerson, ivEditPesonDetail;
}
}
Your code does't change db. You just get sorted items but don't use them.
realm.copyFromRealm(result); // this line does nothing
realm.commitTransaction(); // this one too, because you change nothing
employeedetailadapter.notifyDataSetChanged(); // you data is the same, so this line also useless here
To see your data sorted you should use RealmResults in your adapter. With this approach your list always will sorted, even after adding new items. But note: your adapter should extends RealmRecyclerViewAdapter.
You should run this code before creating adapter and use result inside adapter:
RealmResults<Employee> result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
Also you can try manually update data of your adapter.
private void Ascending_order() {
RealmResults<Employee> result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
employeedetailadapter.updateData(result); // update data inside adapter before calling `notifyDataSetChanged`
employeedetailadapter.notifyDataSetChanged();
}
You need to create updateData method yourself:
public void updateData(RealmResults<Employee> result) {
employeeDetaillists = new ArrayList<Employee>(result);
}
First of all, while getting data from Realm you don't need to write it in Transaction. Write Transaction is required only when you are adding data in realm or updating any realm object.
And about your problem, To get sorted data from realm, You can do it like this
RealmResults<Employee> result = realm.where(Employee.class).sort("name", Sort.ASCENDING).findAll();
Now the data you got is sorted, If you still see wrong order in your ListView then there could be some issue in your Adapter. If you share your adapter code, then I can help further :)
Updated:
Adapter Class
public class EmployeeDetailAdapter extends BaseAdapter {
private RealmResults<Employee> employeeDetaillists;
private Context c;
private LayoutInflater inflater;
private OnItemClick mCallback;
private SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");
public EmployeeDetailAdapter(Context c,RealmResults<Employee> employeeDetaillists, OnItemClick listener) {
this.employeeDetaillists = employeeDetaillists;
this.c= c;
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mCallback = listener;
}
#Override
public int getCount() {
return employeeDetaillists.size();
}
#Override
public Object getItem(int position) {
return employeeDetaillists.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder holder;
if (v==null){
v= (View) inflater.inflate(R.layout.list_single_item,null);
holder = new Holder();
holder.tvPersonName = (TextView) v.findViewById(R.id.tvPersonName);
holder.tvPersonAge = (TextView) v.findViewById(R.id.tvPersonAge);
holder.tvPersonSkill = (TextView) v.findViewById(R.id.tvPersonSkill);
holder.ivEditPesonDetail=(ImageView)v.findViewById(R.id.ivEditPesonDetail);
holder.tvPersondate=(TextView)v.findViewById(R.id.tvPersondate);
holder.ivDeletePerson=(ImageView)v.findViewById(R.id.ivDeletePerson);
v.setTag(holder);
}else{
holder = (Holder) v.getTag();
}
holder.tvPersonName.setText(employeeDetaillists.get(position).getName());
holder.tvPersonAge.setText(employeeDetaillists.get(position).getAge());
holder.tvPersonSkill.setText(employeeDetaillists.get(position).getSkill());
String strDate = df.format(employeeDetaillists.get(position).getSdate());
holder.tvPersondate.setText(strDate);
holder.ivDeletePerson.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Delete(employeeDetaillists.get(position).getName(),position);
}
});
return v;
}
private void Delete(String name, int position) {
mCallback.onClickdelete(name, position);
}
public void updateData(RealmResults<Employee> result) {
}
class Holder {
TextView tvPersonName, tvPersonAge, tvPersonSkill,tvPersondate;
ImageView ivDeletePerson, ivEditPesonDetail;
}
}
In your Activity please change following function
private void Ascending_order() {
result = realm.where(Employee.class)
.sort("name", Sort.ASCENDING).findAll();
employeedetailadapter.notifyDataSetChanged();
}
"result" list should be declared on class level and should be passed to Adapter's constructor as well.
Like
class Activity {
RealmResults<Employee> result;
EmployeeDetailAdapter employeedetailadapter;
//// Other Code
public onCreate(Bundle b) {
result = realm.where(Employee.class).findAll();
employeedetailadapter = new EmployeeDetailAdapter(this, result, listener);
// Other code
}
}
I have a project where I can add data,delete them and update them using SQLite. Of course I display them in a listview. The last thing I want to do is a use a searchView object,so the user can search the data he wants and check if they are there. However the SearchView object does nothing. I type on purpose something that exists in the listview and the matched row doesn't appear.
Here is my code.
public class DisplayForldersActivity extends AppCompatActivity {
DatabaseHandler dba;
private ArrayList<MyFolder> dbFolders = new ArrayList<>();
private FolderAdapter folderAdapter;
private ListView listView;
SearchView inputSearch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_forlders);
listView = (ListView)findViewById(R.id.list);
inputSearch = (SearchView) findViewById(R.id.inputSearch);
fetchDataFromDB();
}
private void fetchDataFromDB() {
dbFolders.clear();
dba = new DatabaseHandler(getApplicationContext());
ArrayList<MyFolder> foldersFromDB = dba.getFolderDetails();
for(int i=0; i<foldersFromDB.size();i++){
String plateNo = foldersFromDB.get(i).getPlateNumber();
String owner = foldersFromDB.get(i).getOwnerName();
String cardId = foldersFromDB.get(i).getCardId();
String content = foldersFromDB.get(i).getContent();
String date = foldersFromDB.get(i).getRecordDate();
int mid = foldersFromDB.get(i).getItemId();
MyFolder f = new MyFolder();
f.setPlateNumber(plateNo);
f.setOwnerName(owner);
f.setCardId(cardId);
f.setContent(content);
f.setRecordDate(date);
f.setItemId(mid);
dbFolders.add(f);
folderAdapter = new FolderAdapter(DisplayForldersActivity.this,R.layout.folders_row,dbFolders);
listView.setAdapter(folderAdapter);
folderAdapter.notifyDataSetChanged();
inputSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
folderAdapter.getFilter().filter(newText);
return false;
}
});
}
dba.close();
}
And my adapter's code
private class FolderAdapter extends ArrayAdapter<MyFolder>{
Activity activity;
int layoutResource;
MyFolder myFolder;
ArrayList<MyFolder> mData = new ArrayList<>();
public FolderAdapter(Activity act, int resource, ArrayList<MyFolder> data) {
super(act, resource,data);
activity = act;
layoutResource = resource;
mData = data;
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size();
}
#Override
public MyFolder getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null || row.getTag()==null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResource,null);
holder = new ViewHolder();
holder.mPlateNo = (TextView)row.findViewById(R.id.plateNumberList);
//holder.mOwner = (TextView)row.findViewById(R.id.ownerName);
holder.mcardId = (TextView)row.findViewById(R.id.idNumber);
//holder.mContent = (TextView)row.findViewById(R.id.processing);
holder.mDate = (TextView)row.findViewById(R.id.dateText);
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
holder.myF = getItem(position);
holder.mPlateNo.setText(holder.myF.getPlateNumber());
//holder.mOwner.setText(holder.myF.getOwnerName());
//holder.mcardId.setText(holder.myF.getCardId());
//holder.mPlateNo.setText(holder.myF.getPlateNumber());
holder.mDate.setText(holder.myF.getRecordDate());
final ViewHolder finalHolder = holder;
holder.mPlateNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String dateText = finalHolder.myF.getRecordDate().toString();
String owner = finalHolder.myF.getOwnerName().toString();
String cardId = finalHolder.myF.getCardId().toString();
String plateNumber = finalHolder.myF.getCardId().toString();
String content = finalHolder.myF.getContent().toString();
int mid = finalHolder.myF.getItemId();
Intent i = new Intent(DisplayForldersActivity.this,DetailedFolderActivity.class);
i.putExtra("id",mid);
i.putExtra("owner",owner);
i.putExtra("cardId",cardId);
i.putExtra("plateNumber",plateNumber);
i.putExtra("content",content);
i.putExtra("dateText",dateText);
startActivity(i);
}
});
return row;
}
class ViewHolder{
MyFolder myF;
int mid;
TextView mPlateNo;
TextView mOwner;
TextView mcardId;
TextView mContent;
TextView mDate;
}
}
Did I do something wrong? Thanks
You need to call notifyDataSetChanged in the adapter attached to the listView to invalidate the ListView and make it draw the changed/removed/inserted lines.
Your array adapter needs to implement Filterable interface, which will override getFilter() method. Then in your getFilter() method you can perform your filtering opertion.
Check out this stackoverflow link, this code might help you out
No results with custom ArrayAdapter Filter
The SerchView doesn't handle any search, it is just a widget. You have to implement the search logic by your own. Try the solution described here:
http://developer.android.com/guide/topics/search/search-dialog.html
You can also do this by calling SearchView.setOnQueryTextListener(SearchView.OnQueryTextListener listener) and pass in a custom implementation of the listener.
Am trying to update my listview on every selection of the spinner. but its not working. Instead of getting new data, listview is repeating the same values.
I am unable to find out what is my mistake.
here is my avtivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.activity_performance_details);
PerfList = new ArrayList<PerformanceListItem>();
months = (Spinner) findViewById(R.id.load_month);
listview_performance = (ListView) findViewById(R.id.performance_details_list);
sadapter = new PerformanceAdapter(PerformanceDetails.this, PerfList);
months.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner a=(Spinner)parent;
if(a.getId() == R.id.load_month) {
monthid =1+(int)months.getSelectedItemPosition();
Toast.makeText(getApplicationContext(),""+monthid,Toast.LENGTH_LONG).show();
new setAsyncTask_performance().execute();
}
}
after selecting spinner data it is sent to server and from server its relevant data is fetched and sent back to the list view. now when i first time select the spinner it show the data accordingly. But on second selection it will include the previous data without updating the listview
Adapter Code:
public class PerformanceAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private Context context;
private List<PerformanceListItem> performanceList;
public PerformanceAdapter(Activity activity, List<PerformanceListItem> PList) {
this.activity = activity;
this.performanceList = PList;
}
#Override
public int getCount() {
return performanceList.size();
}
#Override
public Object getItem(int position) {
return performanceList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.performance_itemlist, null);
}
Animation slideUp = AnimationUtils.loadAnimation(activity, R.anim.slide_up);
TextView staffName = (TextView) convertView.findViewById(R.id.perf_staffName);
TextView staffDesignation = (TextView) convertView.findViewById(R.id.perf_Design);
TextView staffPerformance = (TextView) convertView.findViewById(R.id.perf_performance);
PerformanceListItem plist = performanceList.get(position);
staffName.setText(plist.getpStaffName());
staffDesignation.setText(plist.getpDesignation());
staffPerformance.setText(plist.getpPerformance());
slideUp.setDuration(500);
convertView.startAnimation(slideUp);
slideUp = null;
return convertView;
}
}
and this is my performance list to get and set data
PerformanceListItems code:
public class PerformanceListItem {
private String pSid;
private String pStaffName;
private String pDesignation;
private String pPerformance;
private String pList;
public PerformanceListItem(){
}
public PerformanceListItem(String pList){
this.pList = pList;
}
public String getpSid(){
return pSid;
}
public void setpSid(String pSid){
this.pSid = pSid;
}
public String getpStaffName(){
return pStaffName;
}
public void setpStaffName(String pStaffName){
this.pStaffName = pStaffName;
}
public String getpDesignation(){
return pDesignation;
}
public void setpDesignation(String pDesignation){
this.pDesignation = pDesignation;
}
public String getpPerformance(){
return pPerformance;
}
public void setpPerformance(String pPerformance){
this.pPerformance = pPerformance;
}
}
After debugging the entire code i found that my JSONObject is not updating with new value
any help would be appreciable.
Update the data of your adapter when you execute this
new setAsyncTask_performance().execute();
If you want to show only the new data just remove all your listview items then update the data and set the adapter again.
dont set adapter in oncreate. Set your adapter in Asynctask Post execute. and set your array inside doinbackground along with getting data task.
So my app uses a local SQLite db through a contentProvider
In it's mainActivity I have a listView displaying the contents of a table from the above DB.
I use a custom adapter to display the listview. Each item has a button in it's (custom) layout, that when pressed, shows a custom dialog that inserts a new record in that table, then the dialog gets dismissed.
In order to achieve this behavior, I placed the button click handler inside the customAdapter.
I would like to be able to refresh the listView after the inserting is done (so when dialog is dismissed)
How can I achieve this?
I should probably need to call notifyDataSetChanged somehow from inside the custom Adapter but I can't.
In short, my custom adapter looks like this:
public class DisplayStuffAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> iduser;
private ArrayList<String> product;
public DisplayStuffAdapter(Context c){
this.mContext = c;
}
public DisplayStuffAdapter(Context c, ArrayList<String> id, ArrayList<String> userid, ArrayList<String> product) {
this.mContext = c;
this.id = id;
this.userid = userid;
this.product = product;
}
public int getCount() {
return id.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public class Holder {
TextView txt_id;
TextView txt_userid;
TextView txt_prod;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.myitem, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.tv_MkId);
mHolder.txt_userid = (TextView) child.findViewById(R.id.tv_MkUserId);
mHolder.txt_prod = (TextView) child.findViewById(R.id.tv_MkProduct);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_userid.setText(userid.get(pos));
mHolder.txt_prod.setText(product.get(pos));
Button bt = (Button) child.findViewById(R.id.itemButton);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(mContext);
final View promptsView = li.inflate(R.layout.bid_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder.setMessage("Input data")
.setIcon(R.drawable.add_red_24)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("Add new record", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ContentValues values = new ContentValues();
values.put(MyProvider.TCOL_ID, myid);
values.put(MyProvider.TCOL_OTHERID, Integer.toString(getActiveUserId()));
Uri uri = mContext.getContentResolver().insert(MyProvider.CONTENT_URI_TABLE, values);
values = new ContentValues();
dialogInterface.dismiss();
}
}
}
});
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);
....
}
});
....
I remove some parts from the code to make it more readable.
Now, in my MainActivity, I set the adapter like this:
public class MainActivity extends Activity{
private ArrayList<String> id = new ArrayList<String>();
private ArrayList<String> userid = new ArrayList<String>();
private ArrayList<String> product = new ArrayList<String>();
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillListView();
}
...
private void fillListView(){
id.clear();
userid.clear();
product.clear();
String[] col = {MyProvider.TCOL_ID_ID, MyProvider.TCOL_USERID, MyProvider.TCOL_PROD};
String where = "done = 1";
Cursor mCursor = MainActivity.this.getContentResolver().query(MyProvider.CONTENT_URI_TABLE, col, where, null, MyProvider.TCOL_DATE + " desc");
if (mCursor != null) {
if (mCursor.moveToFirst()) {
do {
id.add(Integer.toString(mCursor.getInt(0)));
userid.add(Integer.toString(mCursor.getInt(1)));
product.add(mCursor.getString(2));
} while (mCursor.moveToNext());
}
}
DisplayStuffAdapter disadpt = new DisplayStuffAdapter(MainActivity.this,id,userid,product);
disadpt.notifyDataSetChanged();
ListView lv = (ListView) findViewById(R.id.mylistView);
lv.setAdapter(disadpt);
}
So this all works great, except that when I add a new record to the table using the customdialog described above... the dialog closes, and the listview remains unchanged.
How can I refresh the listView?
In general, when you're querying data from a DB, you should use ContentProvider and CursorLoader. You can configure your content provider to automatically notify loaders when some data changes, using ContentResolver notifyChange() method. Place the call to this method in your ContentProvider implementation (for example after the insert). This is an example of adapter you can use (but you can use SimpleCursorAdapter providing a view binder too).
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater mInflater;
public CustomCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
if(cursor.getPosition()%2==1) {
view.setBackgroundColor(context.getResources().getColor(R.color.background_odd));
}
else {
view.setBackgroundColor(context.getResources().getColor(R.color.background_even));
}
TextView content = (TextView) view.findViewById(R.id.row_content);
content.setText(cursor.getString(cursor.getColumnIndex(Table.CONTENT)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.listitem, parent, false);
}
}
public class DisplayStuffAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> iduser;
private ArrayList<String> product;
public DisplayStuffAdapter(Context c){
this.mContext = c;
}
public void loadData(){
id.clear();
userid.clear();
product.clear();
String[] col = {MyProvider.TCOL_ID_ID, MyProvider.TCOL_USERID, MyProvider.TCOL_PROD};
String where = "done = 1";
Cursor mCursor = MainActivity.this.getContentResolver().query(MyProvider.CONTENT_URI_TABLE, col, where, null, MyProvider.TCOL_DATE + " desc");
if (mCursor != null) {
if (mCursor.moveToFirst()) {
do {
id.add(Integer.toString(mCursor.getInt(0)));
userid.add(Integer.toString(mCursor.getInt(1)));
product.add(mCursor.getString(2));
} while (mCursor.moveToNext());
}
}
notifyDataSetChanged();
}
...
}
public class MainActivity extends Activity{
private DisplayStuffAdapter disadpt = null;
ContentObserver displayStuffObserver = new ContentObserver(new Handler()){
#Override
public void onChange(boolean selfChange) {
if(disadpt != null) {
disadpt.loadData();
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
disadpt = new DisplayStuffAdapter(this);
ListView lv = (ListView) findViewById(R.id.mylistView);
lv.setAdapter(disadpt);
disadpt.loadData();
getContentResolver().registerContentObserver(MyProvider.CONTENT_URI_TABLE,true, displayStuffObserver);
}
}
Do not forget to unregister your content observer
First of all, I am not getting why you are sending three different arraylists to the adapter. You can simply make a modal class having all the fields that you require in your adapter. Considering your current scenario it will be something like this
public class ModalClass {
private String id = "";
private String userId = "";
private String product = "";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
}
So this is your modal class with getters and setters. Now all you have to do is you have to make an ArrayList of this modal class like this
List<ModalClass> modalClassList=new ArrayList<ModalClass>();
and you have to set all the data that you want to display in your list in this arraylist by making use of modal class setter functions. Like this
if (mCursor != null) {
if (mCursor.moveToFirst()) {
do {
ModalClass modalClass=new ModalClass();
modalClass.setId(Integer.toString(mCursor.getInt(0)));
modalClass.setUserId(Integer.toString(mCursor.getInt(1)));
modalClass.setProduct(mCursor.getString(2));
modalClassList.add(modalClass);
} while (mCursor.moveToNext());
}
}
and now you have your arraylist ready, so you can set it to your listview like this
ListView lv = (ListView) findViewById(R.id.mylistView);
DisplayStuffAdapter disadpt = new DisplayStuffAdapter(MainActivity.this,modalClassList);
lv.setAdapter(disadpt);
And accordingly you have to modify your adapter constructor which i think you can do by your own.
Also how to set values in your adapter, you can make use of your modal class getter methods like this.
ModalClass modalClass=modalClassList.get(pos);
mHolder.txt_id.setText(modalClass.getId());
mHolder.txt_userid.setText(modalClass.getUserId());
mHolder.txt_prod.setText(modalClass.getProduct());
Now when you want to insert a new row in your adapter, you have to simply create an object of ModalClass and set all the new values in that like we have done in our MainActivity class and then finally add that to your modalClassList followed by notifyDataSetChanged();
ModalClass modalClass=new ModalClass();
modalClass.setId(yourNewInsertedRowId);
modalClass.setUserId(yourNewInsertedRowUserId);
modalClass.setProduct(yourNewInsertedRowProduct);
modalClassList.add(modalClass);
notifyDataSetChanged();
And this time your list will be notify for sure. Cheers :)
I would like to ask some question about AdapterView.
In my application, there is an activity which retrieve data from database and display them in AdapterView.
However, when i install the application in different devices, I found that the part I have just mentioned could only function on some devices. The others cannot show the database results.
Here is my code:
private void showResults(String query) {
Cursor cursor = searchCustByInputText(query);
if (cursor == null) {
//
} else {
// Specify the columns we want to display in the result
String[] from = new String[] {
"cust_code",
"chinese_name"};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[] {
R.id.scust_code,
R.id.schinese_name};
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter customers = new SimpleCursorAdapter(this,R.layout.cust_list_item, cursor, from, to);
mListView.setAdapter(customers);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) mListView.getItemAtPosition(position);
String cust_code = c.getString(c.getColumnIndex("cust_code"));
if (callFromAct.equals("Main")) {
String pay_term = c.getString(c.getColumnIndex("pay_term"));
String chinese_name = c.getString(c.getColumnIndex("chinese_name"));
String english_name = c.getString(c.getColumnIndex("english_name"));
String address_1 = c.getString(c.getColumnIndex("address_1"));
String address_2 = c.getString(c.getColumnIndex("address_2"));
String address_3 = c.getString(c.getColumnIndex("address_3"));
String address_4 = c.getString(c.getColumnIndex("address_4"));
String contact = c.getString(c.getColumnIndex("contact"));
String telephone = c.getString(c.getColumnIndex("telephone"));
String last_order_date = c.getString(c.getColumnIndex("last_order_date"));
//Pass data to another Activity
Intent it = new Intent(CustEnqActivity.this, CustEnqDetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("cust_code", cust_code);
bundle.putString("pay_term", pay_term);
bundle.putString("chinese_name", chinese_name);
bundle.putString("english_name", english_name);
bundle.putString("address_1", address_1);
bundle.putString("address_2", address_2);
bundle.putString("address_3", address_3);
bundle.putString("address_4", address_4);
bundle.putString("contact", contact);
bundle.putString("telephone", telephone);
bundle.putString("last_order_date", last_order_date);
it.putExtras(bundle);
startActivity(it);
}
else {
returnToCallingAct(cust_code);
}
//searchView.setQuery("",true);
}
});
}
}
Besides, I discovered there were two warnings in my logcat.
The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated
AdapterView is a raw type. References to generic type AdapterView should be parameterized
Are they related to the problem?
Try to create a class that extends BaseAdapter and use ViewHolders for performance
eg:
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context); // only context can also be used
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());
return convertView;
}
// or you can try better way
private TextView detail(View v, int resId, String text) {
TextView tv = (TextView) v.findViewById(resId);
tv.setText(text);
return tv;
}
private ImageView detail(View v, int resId, int icon) {
ImageView iv = (ImageView) v.findViewById(resId);
iv.setImageResource(icon); //
return iv;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
}
}
More info/example:
http://www.pcsalt.com/android/listview-using-baseadapter-android/#sthash.lNGSCiyB.dpbs