I'm using Realm in my mobile application to store line items for my list view. I'm extending the RealmBaseAdapter which is working fine. Problem is that if I make a query to the database to filter my items, my adapter is not picking up the changed list and causing the out of bound index error.
This is where I set my adapter with the initial values,
results = realm.where(BillingLineItem.class).findAll();
adapter = new BillingListAdapter(getActivity(), results);
And this is the part that I'm doing the filtering based on the spec number,
results = realm.where(BillingLineItem.class)
.equalTo("SpecNumber", spec)
.findAll();
adapter.notifyDataSetChanged();
And like I said before After that query results will be updated but adapter doesn't pick up the changes.
EDIT: My adapter for the list view
public class BillingListAdapter extends RealmBaseAdapter<BillingLineItem> {
private LayoutInflater inflater = null;
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
private boolean isActionMode;
public BillingListAdapter(Context mContext, RealmResults<BillingLineItem> lineItems) {
super(mContext,lineItems);
inflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.isActionMode = false;
}
// I commented out this part because RealmBaseAdapter automaticly implements this methods in the super class
/*#Override
public int getCount() {
return lineItems.size();
}
#Override
public Object getItem(int position) {
return lineItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
/****** Inflate billing_foreground_item.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.billing_foreground_item, null);
/****** View Holder Object to contain billing_foreground_item.xml file elements ******/
holder = new ViewHolder();
holder.SubOper = (TextView) vi.findViewById(R.id.tvSubOper);
holder.Spec = (TextView) vi.findViewById(R.id.tvSpec);
holder.Address = (TextView) vi.findViewById(R.id.tvAddress);
holder.SKU = (TextView) vi.findViewById(R.id.tvSku);
holder.SKUDesc = (TextView) vi.findViewById(R.id.tvSkuDesc);
holder.Quantity = (TextView) vi.findViewById(R.id.tvQuantity);
holder.Unit = (TextView) vi.findViewById(R.id.tvUnit);
holder.BilledQty = (TextView) vi.findViewById(R.id.tvBBilledQty);
holder.RemainingQty = (TextView) vi.findViewById(R.id.tvRemainingQty);
holder.ivLineIcon = (ImageView) vi.findViewById(R.id.ivLineIcon);
holder.rlItem = (RelativeLayout) vi.findViewById(R.id.rlItem);
holder.ErrorMessage = (TextView) vi.findViewById(R.id.txtErrorDisplay);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
/************ Set Model values in Holder elements ***********/
if (adapterData.get(position).getFinalFlag()) {
holder.ivLineIcon.setImageResource(R.drawable.finalflagblue);
holder.rlItem.setBackgroundColor(Color.rgb(255, 255, 255));
if (adapterData.get(position).getCompleted()) {
holder.rlItem.setBackgroundColor(Color.rgb(223, 235, 245));
}
if (adapterData.get(position).getErrorFlag()){
holder.rlItem.setBackgroundColor(Color.rgb(231, 25, 57));
holder.ErrorMessage.setVisibility(View.VISIBLE);
holder.ErrorMessage.setText(adapterData.get(position).getErrorMessage());
}
} else if (adapterData.get(position).getDeleteFlag()) {
holder.ivLineIcon.setImageResource(R.drawable.trashiconred);
holder.rlItem.setBackgroundColor(Color.rgb(255, 255, 255));
if (adapterData.get(position).getErrorFlag()){
holder.rlItem.setBackgroundColor(Color.rgb(231, 25, 57));
holder.ErrorMessage.setVisibility(View.VISIBLE);
holder.ErrorMessage.setText(adapterData.get(position).getErrorMessage());
}
} else if (adapterData.get(position).getChanged()) {
holder.ivLineIcon.setImageResource(R.drawable.changedicongreen);
holder.rlItem.setBackgroundColor(Color.rgb(255, 255, 255));
if (adapterData.get(position).getErrorFlag()){
holder.rlItem.setBackgroundColor(Color.rgb(231, 25, 57));
holder.ErrorMessage.setVisibility(View.VISIBLE);
holder.ErrorMessage.setText(adapterData.get(position).getErrorMessage());
}
} else if (adapterData.get(position).getNewLine()) {
holder.ivLineIcon.setImageResource(R.drawable.newlineicon);
holder.rlItem.setBackgroundColor(Color.rgb(255, 255, 255));
if (adapterData.get(position).getErrorFlag()){
holder.rlItem.setBackgroundColor(Color.rgb(231, 25, 57));
holder.ErrorMessage.setVisibility(View.VISIBLE);
holder.ErrorMessage.setText(adapterData.get(position).getErrorMessage());
}
} else {
holder.ivLineIcon.setImageResource(R.drawable.linesiconblack);
holder.rlItem.setBackgroundColor(Color.rgb(255, 255, 255));
holder.ErrorMessage.setVisibility(View.GONE);
}
if (mSelection.get(position) != null) {
//Log.d(TAG, "Item Selected");
holder.rlItem.setBackgroundColor(Color.rgb(255, 255, 192));// this is a selected position so make it hilighted
}
holder.SubOper.setText(adapterData.get(position).getSubOper());
holder.Spec.setText(adapterData.get(position).getSpecNumber());
holder.Address.setText(adapterData.get(position).getAddress());
holder.SKU.setText(adapterData.get(position).getSKUNumber());
holder.SKUDesc.setText(adapterData.get(position).getSKUDesc());
holder.Quantity.setText(adapterData.get(position).getQuantity());
holder.Unit.setText(adapterData.get(position).getUnit());
holder.BilledQty.setText(adapterData.get(position).getBilledQty());
holder.RemainingQty.setText(adapterData.get(position).getRemainingQty());
return vi;
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
public void setActionMode(boolean isActionMode)
{
this.isActionMode = isActionMode;
}
#Override
public boolean isEnabled(int position)
{
final BillingLineItem item = (BillingLineItem) getItem(position);
if (!item.getDeleteFlag().equals("true"))
{
//only enable items that are not inside the basket
return true;
}
//all other items are disabled during actionmode
return false;
}
public static class ViewHolder {
public TextView SubOper;
public TextView Spec;
public TextView Address;
public TextView SKU;
public TextView SKUDesc;
public TextView Quantity;
public TextView Unit;
public TextView BilledQty;
public TextView RemainingQty;
public ImageView ivLineIcon;
public RelativeLayout rlItem;
public TextView ErrorMessage;
}
}
Some information to point out here:
Android is based on java thus it's variables are passed by reference value (More Info).
It means that if you have an object:
RealmResults<BillingLineItem> results;
And you pass this variable as a parameter to the Adapter constructor:
adapter = new BillingListAdapter(getActivity(), results);
The list which you have outside the adapter and the variable inside the adapter are actually the same objects (two variables pointing to the same reference).
After you make your second query:
results = realm.where(BillingLineItem.class)
.equalTo("SpecNumber", spec)
.findAll();
You are making a new reference and storing it in the result object thus the list outside the adapter and the list which you passed earlier to the adapter are literally different objects so the adapter will not be able to sense the change and you get the error. You can fix it like this:
results.clear();
//adapter.notifyDataSetChanged() if you want to show the change before data fetched...
results.addAll(realm.where(BillingLineItem.class)
.equalTo("SpecNumber", spec)
.findAll());
adapter.notifyDataSetChanged();
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 created a searchview, when i type a word and press ENTER from the keyboard, the results show on a listview in 3 or 4 seconds. so i want to insert a progres spinner till the result populated. And Also I want to try to highlight the search word in the result in list view. I use "SpannableString highlightKeyword" but unable to highlight the result search word, I have tried many ways followed by several websites, but nothing happend. I Couldn't figure out where the mistake. Here are My codes:
mainactivity.java :
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
// Declare Variables
ListView list;
ListViewAdapter adapter;
SearchView editsearch;
String[] animalNameList;
ArrayList<AnimalNames> arraylist = new ArrayList<AnimalNames>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
String[] animalNameList = res.getStringArray(R.array.animalNameList);
// Locate the ListView in listview_main.xml
list = (ListView) findViewById(R.id.listview);
for (int i = 0; i < animalNameList.length; i++) {
AnimalNames animalNames = new AnimalNames(animalNameList[i]);
// Binds all strings into an array
arraylist.add(animalNames);
}
// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(this, arraylist);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
// Locate the EditText in listview_main.xml
editsearch = (SearchView) findViewById(R.id.search);
editsearch.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextSubmit(String newText) {
String text = newText;
adapter.filter(text);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
===== ListviewAdapter.java:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<AnimalNames> animalNamesList = null;
private ArrayList<AnimalNames> arraylist;
public ListViewAdapter(Context context, List<AnimalNames> animalNamesList) {
mContext = context;
this.animalNamesList = animalNamesList;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<AnimalNames>();
this.arraylist.addAll(animalNamesList);
}
public class ViewHolder {
TextView name;
}
#Override
public int getCount() {
return animalNamesList.size();
}
#Override
public AnimalNames getItem(int position) {
return animalNamesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.name = (TextView) view.findViewById(R.id.name);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.name.setText(animalNamesList.get(position).getAnimalName());
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
animalNamesList.clear();
if (charText.length() == 0) {
animalNamesList.addAll(arraylist);
} else {
for (AnimalNames wp : arraylist) {
if (wp.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText)) {
animalNamesList.add(wp);
}
}
}
notifyDataSetChanged();
}
////Here My problem starts :
public static SpannableString highlightKeyword(CharSequence text, Pattern p, int fgcolor, int bgcolor) {
SpannableString ss = new SpannableString(text);
ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
int start = 0;
int end;
Matcher m = p.matcher(text);
while (m.find(start)) {
start = m.start();
end = m.end();
BackgroundColorSpan bgspan = new BackgroundColorSpan(bgcolor);
ss.setSpan(bgspan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ForegroundColorSpan fgspan = new ForegroundColorSpan(fgcolor);
ss.setSpan(fgspan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = end;
}
return ss;
}
////But even did not the result word highlighted.
Thanks in Advance.
Alhamdulillah, At last I found my answer on google. Highligh in custom adapter within getview() method. As follows :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
TextView text;
if (convertView == null) {
view = mInflater.inflate(R.layout.list_item, null);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}
// HIGHLIGHT...
String fullText = getItem(position);
if (mSearchText != null && !mSearchText.isEmpty()) {
int startPos = fullText.toLowerCase(Locale.US).indexOf(mSearchText.toLowerCase(Locale.US));
int endPos = startPos + mSearchText.length();
if (startPos != -1) {
Spannable spannable = new SpannableString(fullText);
ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setText(spannable);
} else {
text.setText(fullText);
}
} else {
text.setText(fullText);
}
return view;
}
MAINSCREEN
imports;
public class HomeFragment extends Fragment {
// Declare Variables
ListView list;
TextView text;
ListViewAdapter adapter;
EditText editsearch;
String[] title;
String[] date;
String[] status;
ArrayList<ListCourse> arraylist = new ArrayList<ListCourse>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
date = new String[] { "11/01/2011", "10/05/2006", "12/07/2002", "05/04/2011", "01/08/2012",
"09/12/2017", "22/06/2024", "31/01/2000", "10/10/2156", "10/02/2006" };
title = new String[] { "Programação", "Matemática", "Logística",
"Mobile", "Sistemas Operativos", "iOS", "Android", "Windows",
"Hardware", "Formação" };
status = new String[] { " ongoing ", " ongoing ",
" ongoing ", " standby ", " ongoing ", " ongoing ",
" ongoing ", " ongoing ", " finished ", " ongoing " };
// Locate the ListView in listview_main.xml
list = (ListView) rootView.findViewById(R.id.listview);
for (int i = 0; i < title.length; i++)
{
ListCourse wp = new ListCourse(date[i], title[i],
status[i]);
// Binds all strings into an array
arraylist.add(wp);
}
// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(getActivity(), arraylist);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
return rootView;
}
}
Do I need to paste my adapter too? The id of the TextView in the .xml layout is status.
My idea is to change the background to yellow if standy, red to finish and green to ongoing. I have a shape_"color".xml for each one.
ADAPTER
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<ListCourse> coursepopulatelist = null;
private ArrayList<ListCourse> arraylist;
public ListViewAdapter(Context context, List<ListCourse> coursepopulatelist) {
mContext = context;
this.coursepopulatelist = coursepopulatelist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<ListCourse>();
this.arraylist.addAll(coursepopulatelist);
}
public class ViewHolder {
TextView title;
TextView date;
TextView status;
}
#Override
public int getCount() {
return coursepopulatelist.size();
}
#Override
public ListCourse getItem(int position) {
return coursepopulatelist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.title = (TextView) view.findViewById(R.id.title);
holder.date = (TextView) view.findViewById(R.id.date);
holder.status = (TextView) view.findViewById(R.id.status);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.title.setText(coursepopulatelist.get(position).getTitle());
holder.date.setText(coursepopulatelist.get(position).getDate());
holder.status.setText(coursepopulatelist.get(position).getStatus());
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(mContext, SingleItemView.class);
// Pass all data rank
intent.putExtra("title",(coursepopulatelist.get(position).getTitle()));
// Pass all data country
intent.putExtra("date",(coursepopulatelist.get(position).getDate()));
// Pass all data population
intent.putExtra("status",(coursepopulatelist.get(position).getStatus()));
// Pass all data flag
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
coursepopulatelist.clear();
if (charText.length() == 0) {
coursepopulatelist.addAll(arraylist);
}
else
{
for (ListCourse wp : arraylist)
{
if (wp.getDate().toLowerCase(Locale.getDefault()).contains(charText))
{
coursepopulatelist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
And this is my shape.xml, example red.
shape
xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:shape="rectangle"
<solid android:color="#c50504" />
<stroke
android:width="10dip"
android:color="#c50504" />
<corners android:radius="10dp" />
Set the same shape to your list item just change the color inside getView wrt status.
try changing your getView to:
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.title = (TextView) view.findViewById(R.id.title);
holder.date = (TextView) view.findViewById(R.id.date);
holder.status = (TextView) view.findViewById(R.id.status);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.title.setText(coursepopulatelist.get(position).getTitle());
holder.date.setText(coursepopulatelist.get(position).getDate());
holder.status.setText(coursepopulatelist.get(position).getStatus());
//<<----------See the code below-------------------
int color = Color.RED;
String s=coursepopulatelist.get(position).getStatus();
if (s.trim().equals("ongoing")) {
color = Color.GREEN;
} else if (s.trim().equals("standby")) {
color = Color.YELLOW;
} else if (s.trim().equals("finished")) {
color = Color.RED;
}
GradientDrawable gd = (GradientDrawable) holder.status.getBackground();
gd.setColor(color);
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(mContext, SingleItemView.class);
// Pass all data rank
intent.putExtra("title",(coursepopulatelist.get(position).getTitle()));
// Pass all data country
intent.putExtra("date",(coursepopulatelist.get(position).getDate()));
// Pass all data population
intent.putExtra("status",(coursepopulatelist.get(position).getStatus()));
// Pass all data flag
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
return view;
}
There's a few ways to do this, depending on how much memory you have available, etc. It seems like all you really want to do is to change the background color. Given that, I would do the following:
Create a HashMap that contains the colors you want indexed by the strings.
It seems you want to change the background of the individual item based on the string that it contains. In that case, you'll want to check the value of each string inside the view, and set the color based off of what it ends up with in the HashMap.
For instance.
static HashMap<String, Integer> colorMap=null;
//This should be run from the constructor
public populateMap () {
if (colorMap==null) {
colorMap=new HashMap<String,Integer>();
colorMap.add("finish",0xffff0000);
}
}
public View getView(int position, View convertView, ViewGroup parent)
String text; //Put in your text value here
TextView tv; // This is your view, I'm assuming a TextView
tv.setText(text);
if (colorMap.containsKey(text)) {
tv.setBackgroundColor(colorMap.get(text));
}
}
My Search is done using a listView, so when the user key in the the first letter in the firstname, it will return the result, how can I make it display "no result found" in Android?
Please advise, do I need to create a a TextView?
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<User> namelist = null;
private ArrayList<User> arraylist;
public ListViewAdapter(Context context,
List<User> worldpopulationlist) {
mContext = context;
this.namelist = worldpopulationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<User>();
this.arraylist.addAll(namelist);
}
public class ViewHolder {
TextView firstname;
TextView lastname;
TextView position;
TextView company;
ImageView pix;
TextView searchresult;
}
#Override
public int getCount() {
return namelist.size();
}
#Override
public User getItem(int position) {
return namelist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.firstname = (TextView) view.findViewById(R.id.firstname);
holder.lastname = (TextView) view.findViewById(R.id.lastname);
holder.position= (TextView) view.findViewById(R.id.position);
holder.company = (TextView) view.findViewById(R.id.company);
// Locate the ImageView in listview_item.xml
holder.pix = (ImageView) view.findViewById(R.id.pix);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.firstname.setText(namelist.get(position).getFirstName());
holder.lastname.setText(namelist.get(position).getLastName());
holder.position.setText(namelist.get(position).getDesignation());
holder.company.setText(namelist.get(position).getCompanyName());
// Set the results into ImageView
holder.pix.setImageResource(namelist.get(position).getProfileImage());
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(mContext, SingleItemView.class);
// Pass all data country
intent.putExtra("firstname",
(namelist.get(position).getFirstName()));
intent.putExtra("lastname",
(namelist.get(position).getLastName()));
// Pass all data rank
intent.putExtra("position",
(namelist.get(position).getDesignation()));
// Pass all data population
intent.putExtra("company",
(namelist.get(position).getCompanyName()));
// Pass all data flag
intent.putExtra("pix",
(namelist.get(position).getProfileImage()));
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
namelist.clear();
if (charText.length() == 0) {
namelist.addAll(arraylist);
} else {
for (User wp : arraylist) {
if (wp.getFirstName().toLowerCase(Locale.getDefault()).contains(charText)||wp.getLastName().toLowerCase(Locale.getDefault()).contains(charText) ) {
namelist.add(wp);
}
else
{
//what should I enter here?
}
}
}
notifyDataSetChanged();
}
}
In my App, for this case, I have an item in list which has a special type 'EMPTY_RESULT'.
Basically, after getting out result which is EMPTY, generate an item with specific type 'EMPTY_RESUT'in Adapter (means getCount() == 1 ), and notify dataset change.
Then, when getView be called, return the empty item and certainly you could design some UI for it to present empty result, such as an icon and text 'nothing found'.
Instead of creating a TextView, you can create a Toast, which is essentially a popup. You can create it like this:
Toast.makeText(mContext, "No Results Found", Toast.LENGTH_SHORT).show();
You can change the last parameter to Toast.LENGTH_LONG if you want the popup to last longer.
I got a list, that i would like to show in a ListView, using my own custom adapter. So this list have some null values in it, and also some "important" not-null values.
It could be like this:
(N representing Null)
(D representint some valuable data)
myList:
[N,N,D,D,N,N,D,N,D,N,N] for example.
So i got my adapter, but i cannot handle the null values in the list.
This is my adapter:
public class ItemAdapter extends ArrayAdapter {
List<Item> itemList;
private Activity act;
boolean selling;
public ItemAdapter(Activity act, List<Item> itemList, boolean selling) {
super(act, R.layout.item_view_layout, itemList);
this.itemList = itemList;
this.act = act;
this.selling = selling;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final Item itemManipulated = itemList.get(position);
if (convertView == null) {
convertView = new ItemInShopView(act, itemManipulated);
holder = new ViewHolder();
holder.convertView = convertView;
holder.itemNameTextView = (TextView) ((ItemView) convertView).getItemNameTextView();
holder.iconImageView = (ImageView) ((ItemView) convertView).getItemIconImageView();
holder.coinView = (CoinView) ((ItemInShopView) convertView).getCoinView();
holder.coinView.init(act);
holder.itemRarityHidedTextView = (TextView) ((ItemView) convertView).getItemRarityHidedTextView();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemNameTextView.setText(itemManipulated.getName());
holder.iconImageView.setImageResource(itemManipulated.getIcon());
holder.itemRarityHidedTextView.setText(itemManipulated.getRarity());
Colorer.setTextViewColorByItemRarity(holder.itemNameTextView, holder.getRarity(), act);
if (selling) {
holder.coinView.setCoins(itemManipulated.getSellPrice());
} else {
holder.coinView.setCoins(itemManipulated.getPrice());
}
holder.convertView.setOnClickListener(new OnClickListenerWithPreventDoubleTapper(itemManipulated));
holder.convertView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
return convertView;
}
class OnClickListenerWithPreventDoubleTapper extends OnClickListenerWithPreventDoubleTap {
Item item;
public OnClickListenerWithPreventDoubleTapper(Item item) {
this.item = item;
}
#Override
protected void performAction() {
new ItemDialog(act, item).show();
}
}
static class ViewHolder {
TextView itemRarityHidedTextView;
TextView itemNameTextView;
ImageView iconImageView;
CoinView coinView;
View convertView;
public String getRarity() {
return itemRarityHidedTextView.getText().toString();
}
}
}
How could i implement some way to get the adapter handle null values and shows nothing there, or maybe show a dummy layout with nothing on it?
I couldnt make a new list without null values and pass these new list to the adapter, because the nulls have an important meaning in an other view.
I hope my problem is clear. Please help if you can.
Why not just?
if (itemManipulated != null){
holder.itemNameTextView.setText(itemManipulated.getName());
holder.iconImageView.setImageResource(itemManipulated.getIcon());
holder.itemRarityHidedTextView.setText(itemManipulated.getRarity());
Colorer.setTextViewColorByItemRarity(holder.itemNameTextView, holder.getRarity(),act);
if (selling) {
holder.coinView.setCoins(itemManipulated.getSellPrice());
} else {
holder.coinView.setCoins(itemManipulated.getPrice());
}
} else {
holder.itemNameTextView.setText("No Item");
... (whatever default values you like)
}
Edit:
If you don't want to display nulls at all you will need to do something more complex.
in getView: find the nth piece of actual data
in getCount: return the number of pieces of actual data
getView:
int posInArray = -1;
for (int i =0; i <= position; i++){
posInArray++;
while(itemList.get(posInArray) == null){
posInArray++;
}
}
itemManipulated = itemList.get(posInArray);
...
getCount:
int count = 0;
for(int i = 0; i < itemList.size(); i++){
if (itemList.get(i) != null){
count++;
}
}
return count;