i created a custom listView of all installed apps but i don't know how to add a search functionality because it's a little complicated(..my app) can anyone help me with that?
(picture of the app )
App.java - app constructor
public class App {
private int number;
private String name;
private String version;
private Drawable drawable;
public App(int number, String name, String version, Drawable drawable){
this.number = number;
this.name = name;
this.version = version;
this.drawable = drawable;
}
//Getters & Setters...
}
AppAdapter.java - listView Adapter
public class AppAdapter extends ArrayAdapter<App> {
Context context;
List<App> objects;
public AppAdapter(Context context, int resources, int textViewResources, List<App> objects){
super(context, resources, textViewResources, objects);
this.context = context;
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater layoutInflater = ((Activity)context).getLayoutInflater();
View view = layoutInflater.inflate(R.layout.custom_card,parent,false);
TextView tvName = (TextView)view.findViewById(R.id.tvName);
TextView tvVersion = (TextView)view.findViewById(R.id.tvVersion);
TextView tvNumber = (TextView)view.findViewById(R.id.tvNumber);
ImageView ivImage = (ImageView)view.findViewById(R.id.ivImage);
App current = objects.get(position);
tvName.setText(String.valueOf(current.getName()));
tvVersion.setText(String.valueOf(current.getVersion()));
tvNumber.setText(String.valueOf(current.getNumber()));
ivImage.setImageDrawable(current.getDrawable());
return view;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<App> appList;
ListView lv;
AppAdapter appAdapter;
App lastSelected;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText etSearch = findViewById(R.id.etSearch);
PackageManager packageManager = getPackageManager();
List<PackageInfo> mApps = packageManager.getInstalledPackages(0);
//array strings to all packages, names and version
final String[] arrPackages = new String[mApps.size()];
final String[] arrVersion = new String[mApps.size()];
String[] arrName = new String[mApps.size()];
//array of Drawables for icons...
Drawable[] arrIcons = new Drawable[mApps.size()];
App[] arrApps = new App[mApps.size()];
appList = new ArrayList<>();
//reading all app's packages and version to the arrays
for (int i = 0; i < mApps.size(); i++){
arrVersion[i] = mApps.get(i).versionName;
arrPackages[i] = mApps.get(i).packageName;
}
for (int i = 0; i < mApps.size(); i++){
try {//getting app's names from theres packages
arrName[i] = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(arrPackages[i], PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
arrName[i] = "Unknown";
}
try {//same as names for icons
arrIcons[i] = packageManager.getApplicationIcon(arrPackages[i]);
} catch (PackageManager.NameNotFoundException e) {
arrIcons[i] = getDrawable(R.drawable.placeholder);
}
arrApps[i] = new App(i + 1, "Name: "+arrName[i], "Version: "+arrVersion[i], arrIcons[i]);
appList.add(arrApps[i]);
}
//on item click open app
appAdapter = new AppAdapter(this,0,0,appList);
lv = findViewById(R.id.lv);
lv.setAdapter(appAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lastSelected = appAdapter.getItem(position);
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(arrPackages[position]);
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}
}
});
//(trying to..) Add Text Change Listener to EditText
etSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Call back the Adapter with current character to Filter
MainActivity.this.appAdapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
(when i try to search something it's gives nothing...)
exchange this line
App current = objects.get(position);
to this
App current = (App) getItem(position);
you don't need to keep List<App> objects; inside your adapter, it is passed in constructors super call (last param) and kept under the hood for you. check out ArrayAdapter source - passed ArrayList is kept as mObjects and further used in some methods, e.g. getPosition, getCount and getItem
ArrayAdapter already implements Filterable, so there is an overriden method getFilter, which returns ArrayFilter - this inner class is declared on the bottom. when you call getFilter().filter( then performFiltering gets called (in a separated thread) and iterates through local copy of your data (line 588). It is using values.get(i).toString().toLowerCase() to compare objects from array with passed String (CharSequence in fact). so in your custom App class override toString method and return in there some searchable value, e.g.
#Override
public String toString(){
return name;
}
this is not best approach, because toString may be used in a lot of mechanisms (its base method of Object) and with above toString implementation two Apps with same name, but different version or number are threated as same object, which isn't true... maybe better way would be to return name+version+number;, but still you have also Drawable in there. thats why I've suggested (in this answer before edit) to make own class extends BaseAdapter and implement own filtering or at least use ArrayAdapter, but override getFilter method and return your own Filter implementation comparing variables instead of using toString. Then won't be needed to override this class, leave it as it is. By default it returns kind-of memory address, so it is unique for every new App instance, even when created with exacly same variables
also in THIS answer you can find nice example how to implement that Filter
Related
I'm currently new to android development and I'm in the process of building my first app. I'm stuck on a particular tricky bit in which I'm trying to implement and Endless Recycler View.
I'm using a recycler view (currently) just to get the current dates and display them in a calendar output. There is two things that are really holding me up. One there is not much documentation out there on using an endless recycler view without a database (I do plan to but again not right now), and not knowing where/how to make it endless. The recyclerView I have Implemented is already working I just need it to load endlessly. The only examples I could find essentially required me to be using a database, or were to create one gigantic method in the onCreate method. Again I have also tried to implement my own setOnScrollListener inside the home class but alas I couldn't get it working.
Below are the desired results that I've got pictured in my head.
Link to UI picture
Here is my code so far. Ideally I would like to keep the on scroll Listener in the RecyclerAdapter as I will probably reuse it later on in other aspects of the app. Thank you in advanced.
HOME CLASS
public class Home extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private static final String TAG = "Home Class" ;
//Recycler View Variables and myCalendarClass Variables
private myCalendarClass myCalendarObj;
private int weeksInView = 1; //will need to make this update dynamicallly based on when a user toggles the view
private ArrayList<Calendar> calendarArrayList = new ArrayList<>();
private ArrayList<Integer> iconList = new ArrayList<>();
private ArrayList<Integer> eventCounterList = new ArrayList<>();
//recycler view dynamic loading variables
private RecyclerView calendarRecyclerView;
private boolean isLoading = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initializeNavMenuButtons();
setViewSpinner();
this.myCalendarObj = new myCalendarClass();
setAllCalendarFields(this.myCalendarObj);
getImagesAndEventCounters();
//Example of the order to call the methods when a user switches the view
//setCalendarItrToCurrentSunday();
//setCalendarArrayList();
//getImagesAndEventCounters();
}
//Recycler view code
//gets images and events to feed into the recycler view
private void getImagesAndEventCounters() {
Log.d(TAG, "initImageBitMaps: called");
ArrayList<Calendar> weekView = getCalendarArrayList();
int daysInView = this.weeksInView * 7;
System.out.println("Days in view " + daysInView + " Weeks In View " + this.weeksInView);
for (int i = 0; i < daysInView; i++) {
calendarArrayList.add(weekView.get(i));
iconList.add(R.id.fitnessIcon);
eventCounterList.add(R.string.XEventsDefault);
iconList.add(R.id.educationIcon);
eventCounterList.add(R.string.XEventsDefault);
iconList.add(R.id.workIcon);
eventCounterList.add(R.string.XEventsDefault);
iconList.add(R.id.personalIcon);
eventCounterList.add(R.string.XEventsDefault);
initRecyclerView();
}
}
//passes data to the parent recycler view and sets the view
private void initRecyclerView() {
//For one recyclerView
this.parentLayoutManager = new GridLayoutManager(this, 7);
//GridLayoutManager layoutManager = parentLayoutManager;
RecyclerView recyclerView = findViewById(R.id.calendarRecyclerView);
recyclerView.setLayoutManager(parentLayoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,iconList,eventCounterList,weeksInView,calendarArrayList);
recyclerView.setAdapter(adapter);
//ViewSpinner(Drop Down Menu)
private void setViewSpinner(){
Spinner viewSpinner = findViewById(R.id.ViewDropDownButton);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Calendar_View_List, android.R.layout.simple_spinner_item);
viewSpinner.setAdapter(adapter);
viewSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
myCalendarClass myCalendarObj;
if(text.equals("Week View")){
myCalendarObj = new myCalendarClass("Week View");
setAllCalendarFields(myCalendarObj);
getImagesAndEventCounters();
this.myCalendarObj = myCalendarObj;
}
else if (text.equals("Biweekly View")){
myCalendarObj = new myCalendarClass("Biweekly View");
setAllCalendarFields(myCalendarObj);
getImagesAndEventCounters();
this.myCalendarObj = myCalendarObj;
}
else if (text.equals("Month View")){
myCalendarObj = new myCalendarClass("Month View");
setAllCalendarFields(myCalendarObj);
getImagesAndEventCounters();
this.myCalendarObj = myCalendarObj;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
//setters
//sets all the local variables needed from myCalendarClass
private void setAllCalendarFields(myCalendarClass c){
this.myCalendarObj = c;
this.calendarArrayList = c.getCalendarArrayList();
this.weeksInView = c.getWeeksInView();
}
//getters
//returns the calendar arraylist
private ArrayList<Calendar> getCalendarArrayList(){
return this.calendarArrayList;
}
RECYCLER ADAPTER
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
//variables for creating the recyclerView cards
private ArrayList<Integer> iconList;
private ArrayList<Integer> eventCounterList;
private Context mContext;
private int itemCount;
private ArrayList<Calendar> calendarArrayList;
private ArrayList<String> dayStr;
private ArrayList<String> dayNum;
public RecyclerViewAdapter(Context context, ArrayList<Integer> imageList, ArrayList<Integer> eventArray, int weeksInView,ArrayList<Calendar> calendarArrayList){
this.iconList = imageList;
this.eventCounterList = eventArray;
this.mContext = context;
this.itemCount = weeksInView*7;
this.calendarArrayList = calendarArrayList;
setDayStrNDayNum();
}
#NonNull
#Override
//this is the method that actually inflates each individual layout
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: called.");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem,parent,false);
return new ViewHolder(view);
}
#Override
//this is where we bind the data to each individual list items
//essentially all the data and stuff is actually attached in this method to each indiviual list item
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.fitnessIcon.getDrawable();
holder.educationIcon.getDrawable();
holder.personalIcon.getDrawable();
holder.workIcon.getDrawable();
holder.fitnessEventCounter.setText(eventCounterList.get(position));
holder.educationEventCounter.setText(eventCounterList.get(position));
holder.workEventCounter.setText(eventCounterList.get(position));
holder.personalEventCounter.setText(eventCounterList.get(position));
holder.dayString.setText(dayStr.get(position));
holder.dayNum.setText(dayNum.get(position));
}
//returns the amount of items we wish to include in the recycler view (not the individual items within a card layout
// but instead how many cards we wish to include...probably
#Override
public int getItemCount() {
return itemCount;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView fitnessIcon;
ImageView educationIcon;
ImageView workIcon;
ImageView personalIcon;
TextView dayString;
TextView dayNum;
TextView fitnessEventCounter;
TextView educationEventCounter;
TextView workEventCounter;
TextView personalEventCounter;
public ViewHolder(View itemView){
super(itemView);
//icons within the layout_listitem
fitnessIcon = itemView.findViewById(R.id.fitnessIcon);
educationIcon = itemView.findViewById(R.id.educationIcon);
workIcon = itemView.findViewById(R.id.workIcon);
personalIcon = itemView.findViewById(R.id.personalIcon);
//text fields within the layout_listitem
dayNum = itemView.findViewById(R.id.dayNum);
dayString = itemView.findViewById(R.id.dayStr);
fitnessEventCounter = itemView.findViewById(R.id.fitnessEventCounter);
educationEventCounter = itemView.findViewById(R.id.educationEventCounter);
workEventCounter = itemView.findViewById(R.id.workEventCounter);
personalEventCounter = itemView.findViewById(R.id.personalEventCounter);
}
}
public void setItemCount(int newItemCount){
itemCount = newItemCount;
}
//sets the dayStr arrays and the dayNum array using CalendarArrayList
// to pull the day in the month and the string value(monday,tuesday,etc.)
private void setDayStrNDayNum(){
Iterator<Calendar> itr = this.calendarArrayList.iterator();
ArrayList<String> tempDayNum = new ArrayList<>();
ArrayList<String> tempDayStr = new ArrayList<>();
int i = 0;
while(itr.hasNext()){
String dayInMonth = getDayInMonth(calendarArrayList.get(i));
String weekDayToString = weekDayToString(calendarArrayList.get(i));
tempDayNum.add(dayInMonth);
tempDayStr.add(weekDayToString);
i++;
itr.next();
}//while
this.dayNum = tempDayNum;
this.dayStr = tempDayStr;
}
//takes in a Calendar Obj, gets the int, and returns it in a String Format
private String getDayInMonth (Calendar c){
Integer temp = c.get(Calendar.DAY_OF_MONTH);
String answer = Integer.toString(temp);
return answer;
}
//takes in a Calendar Obj, gets the weekday digit from 1 to 7, and returns a String
// 1 being Su for Sunday, 2 Mo for Monday, and etc.
private String weekDayToString (Calendar x){
int temp = x.get(Calendar.DAY_OF_WEEK);
switch(temp){
case 1:
return "Su";
case 2:
return "Mo";
case 3:
return "Tu";
case 4:
return "We";
case 5:
return "Th";
case 6:
return "Fr";
case 7:
return "Sa";
}
return "null";
}//weekDayToString
}
here is an idea: in onCreate create some base list containing the next 1000 dates surrounding the current date for your recyclerview and use the following link to understand how to find out after any scrollevent at which position you are on the recyclerview. once you get close to the edges simply add an additional 1000 dates on that end of the spectrum.
Look at the following link to see how to set an onscrolllistener on a recyclerview inside of the adapter (basically you need to pass the recyclerview to the adapters constructor)
hope this helps
Currently had a listview where I showed:
moves
That works fine.
The problem is that now I would like to add pagination, so I show 10 "moves", and then, with an arrow or something, link to the next 10 "moves".
This is my ListView:
RecyclweView Java:
RecyclerView lstMovsWallet = (RecyclerView) findViewById(R.id.lstMovsWallet);
lstMovsWallet.setLayoutManager(new
LinearLayoutManager(MovsMobileWallet.this));
AdapterCobrosPendientesListado adapter = new
AdapterCobrosPendientesListado(MovsMobileWallet.this, items);
lstMovsWallet.setAdapter(adapter);
Adapter for de RecyclerView :
public class AdapterCobrosPendientesListado extends RecyclerView.Adapter<AdapterCobrosPendientesListado.ViewHolder> {
private LayoutInflater mInflater;
protected List<MovimientoCuenta> items;
public AdapterCobrosPendientesListado(Context context, List<MovimientoCuenta> data) {
this.mInflater = LayoutInflater.from(context);
this.items = data;
}
#Override
public AdapterCobrosPendientesListado.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_adapter_billings_listhistory, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(AdapterCobrosPendientesListado.ViewHolder holder, int position) {
DecimalFormat formater = new DecimalFormat("###.00");
String numero = items.get(position).getNumber();
String cantidad = items.get(position).getMonto();
String fecha = items.get(position).getFecha();
String referencia = items.get(position).getReferencia();
String debitoCredito = items.get(position).getDebitoCredito();
holder.number.setText(numero);
holder.mount.setText(cantidad);
holder.date.setText(fecha);
holder.ref.setText(referencia);
if(debitoCredito.compareTo("DBT")==0){
holder.title.setText("Pago");
holder.auxBilling.setImageResource(R.mipmap.signonegativo);
}
else {
holder.title.setText("Cobro");
holder.auxBilling.setImageResource(R.mipmap.signomas);
}
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView number;
public TextView mount;
public TextView date;
public ImageView auxBilling;
public TextView ref;
public TextView title ;
public ViewHolder(View itemView) {
super(itemView);
number = itemView.findViewById(R.id.txtNumberPhoneBilling);
mount = itemView.findViewById(R.id.txtMountBillingNotifications);
date = itemView.findViewById(R.id.txtDateBillingNotifications);
auxBilling = itemView.findViewById(R.id.btnCancelBillingNotifications);
ref = itemView.findViewById(R.id.txtDateBillingRef);
title = itemView.findViewById(R.id.TitleMovs);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
/* // convenience method for getting data at click position
public String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}*/
}
Here I leave the class of movements to replicate:
public class MovimientoCuenta {
private String number;
private String monto;
private String moneda;
private String fecha;
private String ID;
private String referencia ;
private String filtro ;
private String debitoCredito ;
private String nombreMov;
public MovimientoCuenta(String number, String monto, String moneda, String fecha, String ID, String referencia, String filtro, String debitoCredito,String nombreMov) {
this.number = number;
this.monto = monto;
this.moneda = moneda;
this.fecha = fecha;
this.ID = ID ;
this.filtro =filtro;
this.referencia=referencia;
this.debitoCredito =debitoCredito;
this.nombreMov =nombreMov;
}
Any help will be welcome from now thanks.
You can try adding another data model in your list at the end of the current list. Have the view type of this model be a button. On clicking this, you can fetch the next set of moves from your server using offset and limit in your query.
Make the list of a generic type say Parcelable and have all the elements in the list be child via either extending or implementing the parent type.
Eg the list can be of type class A
ArrayList<A>
And the elements in it can be of types
Class B extends A
Class C extends A
Now in your getView , check the type of data using instance of operator and inflate the correct view layout
If(yourlist.get(positionpassedingetview) instanceof B){
//Inflate the view for B item if not already inflated
}else{
//Inflate view of type C
}
For every click on the above said load more item button, use the offset as the current length of list minus one and the limit will always be 10. For the first ever query, use offset as 0 or the sql will throw an exception, rest subsequent queries will have the offset = list size minus one (minus one because the last element of the list is a load more button and not the actual data)
Eg in mysql, this could be:
Select * from yourtable where yourcondition order by yourorder limit youroffset,10;
If there's no more data, just update the model of the load more saying no more items and disabling the button. If you find more items, insert them at the poisition above the last element which is the load more button and notify the list adapter.
You will have to find a tutorial for such a heterogenous list view with more than one viewtypes and viewholders. This is a general idea of how to do it, i myself use this approach with a recyclerview with a similar logic
I am facing an issue where I am getting ArrayIndexOutOfBound on photoViewAttacherList.get(position). Now I can easily handle by catching the error, but I am not able to understand why it is happening at the very first place. Here is the code of the adapter. It's strange because I am adding the new PhotoViewAttacher in instantiate function which will be called for every new item added.
public class FullImagePagerAdaptor extends PagerAdapter {
private final LayoutInflater inflater;
private final ArrayList<String> urls;
private final Context context;
private final ArrayList<String> descriptions;
private ArrayList<PhotoViewAttacher> photoViewAttacherList;
private TogglePagingListener togglePagingListener;
public FullImagePagerAdaptor(Context context, ArrayList<String> urls, ArrayList<String>
descriptions, TogglePagingListener togglePagingListener) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.urls = urls;
this.context = context;
this.descriptions = descriptions;
photoViewAttacherList = new ArrayList<>();
this.togglePagingListener = togglePagingListener;
}
#Override
public int getCount() {
return urls.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
View v = inflater.inflate(R.layout.fragment_full_image, container, false);
TextView description = (TextView) v.findViewById(R.id.txt_full_image_description);
ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progress_bar_full_image);
ImageView imageView = (ImageView) v.findViewById(R.id.full_image_view);
if (descriptions != null && descriptions.get(position) != null) {
description.setText(descriptions.get(position));
}
Log.d("sg","Instantiating Pager:with position" + position);
ImageUtility.loadImage(context, urls.get(position),
GlobalVariables.IMAGE_TYPE.URL, 0, 0,
imageView, progressBar);
photoViewAttacherList.add(new PhotoViewAttacher(imageView));
// THIS IS WHERE ARRAYINDEXOUTOFBOUND EXCEPTION IS GETTING RAISED
photoViewAttacherList.get(position).setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
#Override
public void onMatrixChanged(RectF rect) {
if(isImageZoomed(position))
togglePagingListener.disablePaging();
else
togglePagingListener.enablePaging();
}
});
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
photoViewAttacherList = null;
container.removeView((View) object);
}
public boolean isImageZoomed(int pos) {
return checkZoom(pos) != 1.0;
}
public float checkZoom(int pos){
return photoViewAttacherList.get(pos).getScale();
}
public interface TogglePagingListener {
public void enablePaging();
public void disablePaging();
}
From your code I understand one thing that, you want to add a new PhotoViewAttacher object to your existing list and add a listener setOnMatrixChangeListener to that object.
For this you are adding that object in list and then accessing the same again from list, rather you can have an object created and add to list and to the same object you can add event listener. This is a clean approach.
position should also be recalculated to know exactly where the element was inserted but this is final so you can not change it so declare a new variable for it.
Snippet:
PhotoViewAttacher photoObj = new PhotoViewAttacher(imageView);
photoViewAttacherList.add(photoObj);
final int newPositionInserted = photoViewAttacherList.size() - 1; //Recalculating the position here to know exactly where the object was added
// Here you can use the object which was created so you do not need to worry about the index also
photoObj.setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
#Override
public void onMatrixChanged(RectF rect) {
if(isImageZoomed(newPositionInserted))
togglePagingListener.disablePaging();
else
togglePagingListener.enablePaging();
}
});
I'm trying to inflate a list using baseadapter within an activity. The list just doesn't inflate. From the logs implemented within the class, the getView() function doesn't even execute. Here's the code. -
public class CallLog extends Activity {
ListView logList;
List mList;
Context mCtx;
ArrayList<String> logName;
ArrayList<String> logNumber;
ArrayList<String> logTime;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.reject_call_log);
mCtx = getApplicationContext();
ListView logList = (ListView) findViewById(R.id.log_list);
mList = new List(mCtx, R.layout.log_row);
logList.setAdapter(mList);
SharedPreferences savedLogName = PreferenceManager.getDefaultSharedPreferences(mCtx);
SharedPreferences savedLogNumber = PreferenceManager.getDefaultSharedPreferences(mCtx);
SharedPreferences savedLogTime = PreferenceManager.getDefaultSharedPreferences(mCtx);
try{
logName = new ArrayList(Arrays.asList(TextUtils.split(savedLogName.getString("logName", null), ",")));
logNumber = new ArrayList(Arrays.asList(TextUtils.split(savedLogNumber.getString("logNumber", null), ",")));
logTime = new ArrayList(Arrays.asList(TextUtils.split(savedLogTime.getString("logTime", null), ",")));
Collections.reverse(logName);
Collections.reverse(logNumber);
Collections.reverse(logTime);
}catch(NullPointerException e){
e.printStackTrace();
//TextView noLog = (TextView)findViewById(R.id.no_log);
}
}
public class List extends BaseAdapter {
LayoutInflater mInflater;
TextView nameText;
TextView numberText;
TextView timeText;
int timePos = 1;
public List(Context context, int resource) {
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
v = mInflater.inflate(R.layout.row, null);
}
nameText = (TextView) v.findViewById(R.id.log_name);
numberText = (TextView) v.findViewById(R.id.log_number);
timeText = (TextView) v.findViewById(R.id.log_time);
nameText.setText(logName.get(position));
numberText.setText(logNumber.get(position));
timeText.setText(logTime.get(timePos) + logTime.get(timePos+1));
Log.d("RejectCall", "ListView");
timePos+=2;
return v;
}
}
}
Where is it all going wrong? Also, is there a better way to do what I'm trying to do?
Please replace the following code :
#Override
public int getCount() {
return 0;
}
with
#Override
public int getCount() {
return logName.size();
}
As list view only show the numbers of rows that is returned by this method and right now you are returning 0;
And after fetching the data in arraylist please use adapter.notifyDataSetChanged() to notify the list view.
You have to call notifyDataSetChanged() as you are filling data in array list after setting the adapter. so to notify the list view that data has been changed you have to call notify method(as above)
Your getItem() and getCount() haven't been implemented. If you want any kind of adapter to work for the list, these need to be implemented. Your list is also not holding any actual data, so getItem() has nothing to set.
Don't forget to call notifiyDataSetChanged() in your adapter after you set appropriate implementations for the above two functions.
I'm new to android and I have following problem
I have a ListView. The ListView is filled with data from an ArrayList. I added a CheckBox to every row.
After clicking on a "Resume-Button" I want to write the selected items to a SQLliteDB (the db works fine - I tested it with static data).
My question is now:
How can I get the checked items from the list?
Thx everyone for help!
Best regards
kyp
You can use this ListView method getCheckedItemPositions() to get all the checked positions. Make sure that the child view in your ListView actually implements the Checkable interface otherwise the checkmark information will not be passed along to the ListView.
You can go through the list, using a for loop and getItemAtPosition(int).
For each row you just check if the item is checked, isChecked() for a checkbox. Without seeing the code that is as specific as I can get. If this is not clear enough then post some code about how/what items are stored in the listView
Hope this helps!
But i figured it out myself.
I only wanted to select the row by clicking on the checkBox view.
I modified my custom Array Adapter
Here is my code:
public class ContactAdapter extends ArrayAdapter<Contact> implements OnClickListener{
private static HashMap<Integer, Boolean> teamItems = new HashMap<Integer, Boolean>();
int count = 0;
private List<Contact> items;
private int mode;
CheckBox add_to_team;
static int counter = 3;
public ContactAdapter(Context context, int textViewResourceId, List<Contact> objects, int mode) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.mode = mode;
this.items = objects;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
View v = convertView;
if(v == null) {
//get a reference to the LayoutInflator
LayoutInflater li = (LayoutInflater)getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//and inflate our XML into the View
v = li.inflate(R.layout.list_contacts_item, null);
}
String race = items.get(position).race;
String nick = items.get(position).nick;
String name = items.get(position).name;
final int pos = position;
if(v!= null) {
// Do the work for the other views
// MODE 1 = SELECTION-MODE
if(mode == 1) {
add_to_team.setVisibility(0);
try {
if(count!=0) {
boolean b = teamItems.get(pos);
if(b==false) {
add_to_team.setChecked(false);
}
else {
add_to_team.setChecked(true);
}
}
} catch (NullPointerException e) {
}
add_to_team.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
teamItems.put(pos, arg1);
count++;
}
});
} else {
add_to_team.setVisibility(8);
}
}
return v;
}
}
I added a HashMap in which I save the selected items. I can call the select state from my activity, by calling the HashMap... And it works great!