I have a problem, I have created custom adapter with custom filtering on Contact object in Contacts app, it filters names of contacts just fine and list of filteredContacts is saved properly. The problem is that when I type anything in searchView, the number of found rows is correct but informations about found rows are not correct. The problem seems to be about getView function in adapter, it is not called when it should. For example if i have three contacts: "abc", "aaa", "aba" and they are shown at the beggining in this order from top to bottom, then if I type "ab" in searchView then it finds 2 rows but they are still the same as at the beggining, they should be "abc" and "aba" but they are "abc" and "aaa". The first ever view created by adapter seems to be stuck for some reason.
My activity class:
package com.example.androidlab;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import java.lang.reflect.Type;
import java.util.ArrayList;
public class AllContactsActivity extends AppCompatActivity {
static ArrayList<Contact> contacts = new ArrayList<>();
ContactsAdapter contactsAdapter;
ListView listView;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_contacts);
FloatingActionButton addContact = findViewById(R.id.newContact);
listView = findViewById(R.id.allContactsList);
searchView = findViewById(R.id.searchContacts);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.tanay.thunderbird.contacts", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("contacts", null);
Type type = new TypeToken<ArrayList<Contact>>() {
}.getType();
contacts = gson.fromJson(json, type);
if (contacts == null) {
contacts = new ArrayList<>();
}
if (contacts.size() == 0)
Toast.makeText(this, "There are no contacts.", Toast.LENGTH_LONG).show();
contactsAdapter = new ContactsAdapter(this, contacts);
listView.setAdapter(contactsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), EditContactActivity.class);
intent.putExtra("noteID", position);
startActivity(intent);
finish();
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
contactsAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
contactsAdapter.getFilter().filter(query);
return false;
}
});
addContact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), NewContactActivity.class);
startActivity(intent);
finish();
}
});
}
#Override
public void onBackPressed() {
finish();
}
}
My adapter class:
package com.example.androidlab;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ContactsAdapter extends BaseAdapter implements Filterable {
Context context;
List<Contact> contacts;
List<Contact> filteredContacts;
ContactFilter mFilter = new ContactFilter();
String filterString;
ContactsAdapter(Context context, List<Contact> contacts) {
this.context = context;
this.contacts = contacts;
filteredContacts = contacts;
}
#Override
public int getCount() {
if(contacts.size() != filteredContacts.size() || filterString != null)
{
return filteredContacts.size();
}
else {
filteredContacts = contacts;
return contacts.size();
}
}
#Override
public Object getItem(int position) {
return contacts.get(position);
}
#Override
public long getItemId(int position) {
return contacts.indexOf(getItem(position));
}
#Override
public Filter getFilter() {
return mFilter;
}
private class ViewHolder {
ImageView imageView;
TextView name;
TextView phoneNumber;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(AllContactsActivity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_contacts, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.contactName);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageOfContact);
holder.phoneNumber = (TextView) convertView.findViewById(R.id.phoneNumber);
Contact contact;
if(filteredContacts.size() != contacts.size()) contact = filteredContacts.get(position);
else contact = contacts.get(position);
if (contact.getAvatar() == null) {
if (contact.getGender() == true)
holder.imageView.setImageResource(R.drawable.ic_man);
else holder.imageView.setImageResource(R.drawable.ic_woman);
} else {
byte[] decodedString = Base64.decode(contact.getAvatar(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.imageView.setImageBitmap(decodedByte);
}
holder.name.setText(contact.getName());
holder.phoneNumber.setText(String.valueOf(contact.getPhoneNumber()));
}
return convertView;
}
private class ContactFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Contact> list = contacts;
int count = list.size();
final ArrayList<Contact> nList = new ArrayList<Contact>();
for (int i = 0; i < count; i++) {
if(contacts.get(i).getName().contains(filterString)) nList.add(contacts.get(i));
}
results.values = nList;
results.count = nList.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredContacts = (ArrayList<Contact>) results.values;
notifyDataSetChanged();
}
}
}
EDIT: I got rid of if(contentView == null) in getView function and everything works fine
The check for convertView == null should only handle the inflation of your View and the creation of a new ViewHolder the rest of you getView code should always run.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_contacts, null);
// attach a new holder to the inflated view
holder = new ViewHolder();
convertView.setTag(holder);
} else {
// get an existing holder from the existing view
holder = (ViewHolder) convertView.getTag();
}
// update your holder here:
// e.g. holder.name.setText(contact.getName());
return convertView;
}
Related
I'm tired already with my code. I'm writing chat application. My app consist of two activity. First activity has a listview of wich each row contain a last message which was send for the user while a second activity contain the whole conversation. In my app I used socket.io for android. My app works fine. Listview is refresh when a data is receive but when I press back button and then come back to the activity a listview not refresh itself already. In logs console I see that a data has received and "changed" method is called but listview is not refresh. What is wrong in belows code?
package com.example.seadog.fb_dialog;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import java.util.ArrayList;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends Activity {
public static ArrayList arrayList = new ArrayList();
public ListView listView;
public MyBaseAdapter adapter;
public TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Get Socket.io Object
*/
SocketIO socketIo = new SocketIO();
Socket mSocket = socketIo.getSocket(); // get socket
Integer id = socketIo.getId(); // get Website ID
if(mSocket == null) {
socketIo.Connection();
mSocket = socketIo.getSocket();
mSocket.on("message", new Emitter.Listener() {
/*
* Message Listener
*/
#Override
public void call(Object... args) {
Boolean isset = false;
try {
JSONObject object = (JSONObject) args[0];
String _id = object.getString("_id");
String message = object.getString("message");
JSONObject obj = new JSONObject();
obj.put("direction", "fb-lt");
obj.put("message", message);
obj.put("date", "2017-05-29T12:15:49.245Z");
for(int i = 0; i < arrayList.size(); i++){
ListData ld = (ListData) arrayList.get(i);
String id = ld.getId();
if(_id.equals(id)){
JSONArray Data = ld.getData();
Data.put(obj);
ld.setDescription(message);
arrayList.set(i, ld);
isset = true;
Log.d("LOG", message);
}
}
if(!isset) {
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj);
ListData ld = new ListData();
ld.set_id(_id);
ld.setID(1);
ld.setTitle("Klient:");
ld.setDescription(message);
ld.setData(jsonArray);
arrayList.add(ld);
}
} catch (JSONException e) {
e.printStackTrace();
}
changed();
}
});
}
/*
* Populate a listview
*/
listView = (ListView) findViewById(R.id.listView);
adapter = new MyBaseAdapter(this, arrayList);
listView.setAdapter(adapter);
/*
* OnItemClickListener
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Conversation.class);
intent.putExtra("item", position);
startActivity(intent);
TextView textView = (TextView) view.findViewById(R.id.descitem);
textView.setTypeface(null, Typeface.NORMAL);
}
});
textView = (TextView) findViewById(R.id.count);
}
private void changed() {
runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
Log.d("LOG:", "adapter refresh");
}
});
}
}
MyBaseAdapter Class:
package com.example.seadog.fb_dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MyBaseAdapter extends BaseAdapter {
Context context;
ArrayList<ListData> items = new ArrayList();
LayoutInflater inflater;
int id = 0;
public MyBaseAdapter(Context context, ArrayList items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public ListData getItem(int position) {
return items.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.list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
id = position > 0 ? getItem(position - 1).getID() : 0;
mViewHolder.Title.setText(currentListData.getTitle());
mViewHolder.Desc.setText(currentListData.getDescription());
if(1==1){
mViewHolder.Title.setVisibility(View.GONE);
}else {
if (id != currentListData.getID()) {
mViewHolder.Title.setVisibility(View.VISIBLE);
} else {
mViewHolder.Title.setVisibility(View.GONE);
}
}
return convertView;
}
private class MyViewHolder {
TextView Title, Desc;
public MyViewHolder(View item) {
Title = (TextView) item.findViewById(R.id.txtitem);
Desc = (TextView) item.findViewById(R.id.descitem);
Typeface title = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSans.ttf");
Title.setTypeface(title);
Typeface desc = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSans.ttf");
Desc.setTypeface(desc, Typeface.BOLD);
}
}
}
#Override
protected void onRestart() {
Intent intentBack = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intentBack);
super.onRestart();
}
you did not have any callback after you back to this activity .
You can either override onResume() method or startActivityForResult and do something in the onAcitvityForResult method.
I have a cell adapter that I use for all my listViews. The different type of lists are determined by the parameter TableType.
TableTypes 1 and 2 also use a external library that allows a slide out function for saving and deleting. As a result of this library OnClickListeners in the list views class will not work for TableType 1 and 2.
Thus I have added the OnClickListener in the cell adapter itself (The onClickListener for the "Cell" portion of the Tag). The cell adapter than uses the interface to parameter to call the necessary method.
The inconsistency here is that TableType 1 performs correctly without error. TableType 2 does not however as the Tag returns the position of the item above it.
I.E. if TableType 2 has 4 items, Item 2 will have Tag 1, Item 3 will have Tag 2. I'm not sure why this is happening because the code is identical between TableType 1 and 2.
CellAdapter:
package layout;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.Image;
import android.nfc.Tag;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.view.View;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import android.preference.PreferenceManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.tble.brgo.InfoArticle;
import com.tble.brgo.R;
import android.graphics.Typeface;
import android.widget.Filterable;
import android.widget.Filter;
import org.w3c.dom.Text;
/**
* Created by Praveen on 8/21/16.
*/
public class StandardCellAdapter extends ArrayAdapter<InfoArticle> implements Filterable {
public ArrayList<InfoArticle> orig;
public ArrayList<InfoArticle> Teachers;
public SwipeRevealLayout swipeV;
public TextView deleteButton;
public TextView saveButton;
public int tableType;
public displayInterface activity;
public Context ct;
public StandardCellAdapter(Context context, ArrayList<InfoArticle> titles, int tableType, displayInterface inter) {
super(context, 0, titles);
this.Teachers = titles;
this.tableType = tableType;
saveButton = new TextView(context);
deleteButton = new TextView(context);
activity = inter;
ct = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String title = getItem(position).title;
String desc = getItem(position).description;
Holder TagHolder = null;
TextView Ctitle;
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
if (tableType == 0) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.nclayout, parent, false);
Ctitle = (TextView) convertView.findViewById(R.id.cellTitle);
}
else if(tableType == 1){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.sclayout, parent, false);
Ctitle = (TextView) convertView.findViewById(R.id.cellTitle);
TagHolder = new Holder();
swipeV = (SwipeRevealLayout)convertView.findViewById(R.id.scSwipe);
saveButton = (TextView)convertView.findViewById(R.id.saveButton);
TagHolder.Save = saveButton;
TagHolder.Cell = Ctitle;
convertView.setTag(TagHolder);
}
else{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.tclayout, parent, false);
Ctitle = (TextView) convertView.findViewById(R.id.cellTitle);
TagHolder = new Holder();
swipeV = (SwipeRevealLayout)convertView.findViewById(R.id.tcSwipe);
deleteButton = (TextView) convertView.findViewById(R.id.deleteButton);
TagHolder.Delete = deleteButton;
TagHolder.Cell = Ctitle;
convertView.setTag(TagHolder);
}
}
else {
TagHolder = (Holder) convertView.getTag();
Ctitle = (TextView) convertView.findViewById(R.id.cellTitle);
}
if(tableType == 1) {
TagHolder.Save.setTag(position);
TagHolder.Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveTeacher((int)v.getTag());
}
});
TagHolder.Cell.setTag(position);
TagHolder.Cell.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
activity.displayWebpage(getItem((int)v.getTag()).description);
}
});
}
else if(tableType == 2) {
TagHolder.Delete.setTag(position);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deleteTeacher((int)v.getTag());
if(Teachers.size() == 0)
Teachers.add(new InfoArticle("Slide to Save Teachers", "google.com"));
notifyDataSetChanged();
}
});
TagHolder.Cell.setTag(position);
TagHolder.Cell.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
activity.displayWebpage(getItem((int)v.getTag()).description);
}
});
}
Typeface customFont = Typeface.SERIF;
Ctitle.setTypeface(customFont);
Ctitle.setText(title);
return convertView;
}
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<InfoArticle> results = new ArrayList<InfoArticle>();
if (orig == null)
orig = Teachers;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final InfoArticle g : orig) {
if (g.title.toLowerCase()
.contains(constraint.toString().toLowerCase())) {
results.add(g);
}
}
}
oReturn.values = results;
}
return oReturn;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
Teachers = (ArrayList<InfoArticle>)results.values;
notifyDataSetChanged();
}
};
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public ArrayList<InfoArticle> toIA(ArrayList<String> data){
ArrayList<InfoArticle> converted = new ArrayList<InfoArticle>();
for(String a: data)
{
converted.add(new InfoArticle(a,""));
}
return converted;
}
#Override
public int getCount() {
return Teachers.size();
}
#Override
public InfoArticle getItem(int position) {
return Teachers.get(position);
}
public void saveTeacher(int position){
SharedPreferences sharedPref = getContext().getSharedPreferences("MY_PREFERENCES",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
Set<String> teach = sharedPref.getStringSet("teachPref", new HashSet<String>());
teach.add(Teachers.get(position).title);
Set<String> link = sharedPref.getStringSet("linkPref", new HashSet<String>());
link.add(Teachers.get(position).description);
editor.putStringSet("teachPref", teach);
editor.putStringSet("linkPref", link);
editor.apply();
new AlertDialog.Builder(getContext())
.setTitle("Teacher Saved")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
}).setIcon(android.R.drawable.ic_dialog_alert)
.show();
swipeV.close(true);
}
private void deleteTeacher(int position){
SharedPreferences sharedPref = getContext().getSharedPreferences("MY_PREFERENCES",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
Set<String> teach = sharedPref.getStringSet("teachPref", new HashSet<String>());
Set<String> link = sharedPref.getStringSet("linkPref", new HashSet<String>());
teach.remove(Teachers.get(position).title);
link.remove(Teachers.get(position).description);
editor.putStringSet("teachPref", teach);
editor.putStringSet("linkPref", link);
editor.apply();
Teachers.remove(position);
new AlertDialog.Builder(getContext())
.setTitle("Teacher Deleted")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
}).setIcon(android.R.drawable.ic_dialog_alert)
.show();
swipeV.close(false);
}
class Holder{
TextView Delete;
TextView Save;
TextView Cell;
}
}
Turns out the problem wasn't due to the tags it was how I was saving my data. TableType 2 is used to display data from 2 different Hashsets which are then combined. Because Hashsets don't care about order, I my combined ArrayList would be mismatched due the lack of order. I fixed this by saving my ArrayList as a JSON serialized String.
I am trying to show some data offline from SQLite database. Data is coming fine but I am not able to show that on listview with adapter.
libraryList.clear();
libraryList = dBhelper.localQuestionBank(student_uuid, questionBankId);
if (libraryList.size() > 0) {
offSetFlag = libraryList.size();
libraryListView.setAdapter(questionBankAdapter);
questionBankAdapter.notifyDataSetChanged();
libraryListView.removeFooterView(loadMoreView);
loadingMore = false;
}
localQuestionBank method:
public ArrayList<Map<String, String>> localQuestionBank(String student_uuid, String question_bank_id) {
ArrayList<Map<String, String>> localQuestionBankList = new ArrayList<>();
ArrayList<Map<String, String>> questionBankList = new ArrayList<>();
String[] columns = new String[]{QUESTION_BANK_LABEL, AIP_UUID, QUESTION_SEQUENCE_NAME, QUESTION_MARKS, DESCRIPTION};
SQLiteDatabase db = mDbHelper.getWritableDatabase();
try {
Cursor cursor = db.query(LOCAL_QUESTION_BANK, columns, STUDENT_UUID+"=?" +" and "+ QUESTION_BANK_ID+"=?", new String[] { student_uuid, question_bank_id }, null, null, null);
if (cursor.moveToFirst()) {
do {
Map<String, String> subMap = new HashMap<String, String>();
subMap.put("aep_uuid", cursor.getString(cursor.getColumnIndex(AIP_UUID)));
subMap.put("question_marks", cursor.getString(cursor.getColumnIndex(QUESTION_MARKS)));
subMap.put("question_sequence_name", cursor.getString(cursor.getColumnIndex(QUESTION_SEQUENCE_NAME)));
subMap.put("description", cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
subMap.put("question_bank_label", cursor.getString(cursor.getColumnIndex(QUESTION_BANK_LABEL)));
questionBankList.add(subMap);
} while (cursor.moveToNext());
localQuestionBankList.addAll(questionBankList);
questionBankList.clear();
}
cursor.close();
db.close();
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
return localQuestionBankList;
}
Listview and Adapter:
questionBankAdapter = new QuestionBankAdapter(getActivity(),libraryList);
libraryListView.setAdapter(questionBankAdapter);
questionBankAdapter:
package eukti.myafterclass.adapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Map;
import eukti.myafterclass.R;
import eukti.myafterclass.ui.QuestionBankDetailsView;
import eukti.myafterclass.utils.InternetConnectionDetector;
public class QuestionBankAdapter extends BaseAdapter {
private final Activity context;
private ArrayList<Map<String, String>> dashboardList;
private static LayoutInflater inflater=null;
public QuestionBankAdapter(Activity context,
ArrayList<Map<String, String>> dashList) {
this.context = context;
this.dashboardList = dashList;
if (context!=null) {
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
#Override
public int getCount() {
return dashboardList.size();
}
#Override
public Object getItem(int position) {
return dashboardList.size();
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView sequence;
TextView marks;
WebView description;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
Log.d("LocalQBadapter:", dashboardList.get(position).get("question_sequence_name"));
rowView = inflater.inflate(R.layout.questionbank_items, null);
holder.sequence=(TextView) rowView.findViewById(R.id.questionSequenceName);
holder.marks=(TextView) rowView.findViewById(R.id.marks);
holder.description=(WebView) rowView.findViewById(R.id.description);
holder.description.getSettings().setJavaScriptEnabled(true);
String heading = dashboardList.get(position).get("question_sequence_name");
holder.sequence.setText(Html.fromHtml(heading));
holder.marks.setText("Marks : "+dashboardList.get(position).get("question_marks"));
String description = dashboardList.get(position).get("description");
String allAns = "<html><body>"+description.replace("\r\n", "<br/>")+"</body></html>";
holder.description.loadDataWithBaseURL(null, allAns, "text/html", "utf-8", null);
holder.description.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
holder.description.setLongClickable(false);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!InternetConnectionDetector.isNetworkAvailable(context)) {
Toast.makeText(context, "Check your connection and try again.", Toast.LENGTH_LONG).show();
} else {
Intent redirect = new Intent(context, QuestionBankDetailsView.class);
redirect.putExtra("aep_uuid", dashboardList.get(position).get("aep_uuid"));
redirect.putExtra("question_sequence_name", dashboardList.get(position).get("question_sequence_name"));
redirect.putExtra("question_marks", dashboardList.get(position).get("question_marks"));
redirect.putExtra("description", dashboardList.get(position).get("description"));
context.startActivity(redirect);
}
}
});
return rowView;
}
}
It seems fine for me but data is not showing in Listview.
What you have typed
#Override
public Object getItem(int position) {
return dashboardList.size();
}
what i think it should be
#Override
public Object getItem(int position) {
return dashboardList.get(position);
}
try it and let me know
In your getItem() method you are returning the size. You need to give the actual object from the dataset dashboardList.
#Override
public Object getItem(int position) {
return dashboardList.get(position);
}
Also in the getView code you can call the getItem method instead of accessing the dataset directly.
Map<String, String> item = (Map<String, String>) getitem(position);
Bind your UI from this item object. Also try to use a Viewholder pattern in your Adapter.
By getting data from server displaying custom list-view. List item is having one button and one textview. Here i'm trying to display another listview with selected listview data.Here selection is done when when user click on button in list item.I'have done upto getting selected data from first Listview and storing it in arraylist.after this how to use this arraylist(Which is present in adapter class) and display a listview below the first listview.How to proceed.Here is my firstlistview adapter class
package com.spatel.slantright.adapter;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.spatel.slantright.R;
import com.spatel.slantright.model.ManagerModel;
import com.spatel.slantright.model.UserDetials;
public class ManagersAdapter extends BaseAdapter {
Context context;
private List<UserDetials> rowItem = null;
private List<UserDetials> notFollowing = null;
LayoutInflater mInflater, notFollowinflater;
boolean state[] = { true, true, true, true };
public ManagersAdapter(Context context, List<UserDetials> alFollowings) {
this.context = context;
this.rowItem = alFollowings;
this.mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
/* private view holder class */
private class ViewHolder {
ImageView ivBlack;
TextView tvUsername;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final UserDetials row_pos;
row_pos = rowItem.get(position);
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_managers, null);
holder = new ViewHolder();
holder.ivBlack = (ImageView) convertView.findViewById(R.id.ivBlack);
holder.tvUsername = (TextView) convertView
.findViewById(R.id.tvUsername);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
// setting the image resource and title
//holder.ivBlack.setImageResource(rowItem.get(position).getIcon());
holder.tvUsername.setText(rowItem.get(position).getUserName());
holder.ivBlack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (state[position]) {
row_pos.setIcon(R.drawable.black_circle);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"UserDetails");
query.whereEqualTo("userId", ParseUser.getCurrentUser()
.getObjectId().toString());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> object,
ParseException e) {
ParseObject objects = object.get(0);
if (e == null) {
try {
JSONArray jArray = objects
.getJSONArray("followings");
JSONArray notfollowingJArray = new JSONArray();
final JSONArray followingJArray = new JSONArray();
if (jArray != null && jArray.length() > 0) {
// alFollowings = new
// ArrayList<UserDetials>();
for (int i = 0; i < jArray.length(); i++) {
// Excluding the item at position
System.out
.println("POSITION ::::::::::::::::"
+ position);
if (i != position) {
followingJArray.put(jArray.get(i));
}else {
System.out
.println("RRRRRRRRRRRRRRRRRRRRROOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWWWW" + rowItem.get(i).getUserName().toString());
followingJArray.put(jArray.get(i));
ArrayList<String> listdata = new ArrayList<String>();
if (jArray != null) {
for (int j=0;j<jArray.length();j++){
listdata.add(jArray.get(j).toString());
}
}
}
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"UserDetails");
query.whereEqualTo("userId", ParseUser.getCurrentUser()
.getObjectId().toString());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> object,
ParseException e) {
ParseObject objects = object.get(0);
objects.put(
"followings",
followingJArray);
objects
.saveInBackground();
}
});
}
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
});
state[position] = false;
rowItem.remove(position);
notifyDataSetChanged();
} else if (!state[position]) {
row_pos.setIcon(R.drawable.black_circle);
state[position] = true;
notifyDataSetChanged();
}
}
});
return convertView;
}
#Override
public int getCount() {
return rowItem.size();
}
#Override
public Object getItem(int position) {
return rowItem.get(position);
}
#Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
}
I am suffering a weird problem. Actually I have a customize listview in which I am using a filter, everything working fine but when I am typing a text to edittext it disappear the all listitem. I am strange why this going on with me, still I am not a champ of android so need some help. I have seen many similar problems on stackoverflow like this,this,this, and many more, but nothing works in my case. I dont know where i am doing mistake.
My listitem click working fine, So hope it will also work after item search into edittext.
Here is my MainActivity.java :
package com.sunil.listviewmuntilerowdelete;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
private String[] myfriendname = null;
EditText edtSearch;
private int[] photo = null;
ListView listView = null;
Context contex = null;
MyListAdapter adapter = null;
private List<MyFriendsSDetails> list = new ArrayList<MyFriendsSDetails>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contex = this;
listView = (ListView) findViewById(R.id.listview);
edtSearch = (EditText) findViewById(R.id.EditText01);
myfriendname = new String[] { "Sunil Gupta", "Abhishek Tripathi",
"Sandeep Pal", "Amit Verma" };
photo = new int[] { R.drawable.sunil, R.drawable.abhi,
R.drawable.sandy, R.drawable.amit };
final String text[] = { "Sunil is a great man",
"Abhishek is hardworking", "Sandeep is same as amit",
"Amit is unique" };
final Integer[] image = { R.drawable.sunil, R.drawable.abhi,
R.drawable.sandy, R.drawable.amit,
};
for (int index = 0; index < myfriendname.length; index++) {
MyFriendsSDetails details = new MyFriendsSDetails(
myfriendname[index], photo[index]);
list.add(details);
}
adapter = new MyListAdapter(contex, list);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
edtSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("key1", image[position]);
intent.putExtra("key2", text[position]);
startActivity(intent);
}
});
}
}
MyListAdapter.java
package com.sunil.listviewmuntilerowdelete;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<MyFriendsSDetails> {
Context context;
LayoutInflater inflater;
List<MyFriendsSDetails> list;
public MyListAdapter(Context context, List<MyFriendsSDetails> list) {
super(context, 0, list);
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.photo = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getMyfriendname());
holder.photo.setImageResource(list.get(position).getPhoto());
return convertView;
}
private class ViewHolder {
TextView name;
ImageView photo;
}
}
Here is MyFriendsSDetails.java :
package com.sunil.listviewmuntilerowdelete;
public class MyFriendsSDetails {
private String myfriendname = null;
private int photo = 0;
public MyFriendsSDetails(String friendname, int myphoto) {
this.myfriendname = friendname;
this.photo = myphoto;
}
public String getMyfriendname() {
return myfriendname;
}
public void setMyfriendname(String myfriendname) {
this.myfriendname = myfriendname;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
}
Thanks in advance.
As you are using custom adapter, android cant recognize "MainActivity.this.adapter.getFilter().filter(cs);"
You have to override getFilter() method and do manual changes.
Checkout below code :
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,FilterResults results) {
mDisplayedValues = (ArrayList<HashMap<String, String>>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList<HashMap<String, String>>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<HashMap<String, String>>(mDisplayedValues); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i).get("name");
if (data.toLowerCase().startsWith(constraint.toString())) {
HashMap<String, String> hmap=new HashMap<String, String>();
hmap.put("name", mOriginalValues.get(i).get("name"));
hmap.put("image", mOriginalValues.get(i).get("image"));
FilteredArrList.add(hmap);
Log.e("DEBUG", "name : "+data);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
and use it in your adapter getView()
holder.name.setText(mDisplayedValues.get(position).get("name"));