android custom List Adapter repeat old item - android

this is my custom adapter i use it for first time its work without repeat
public class MessageAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<MessageList> MessageList;
private final Context context;
public MessageAdapter(Activity activity, List<MessageList> MessageList, Context c) {
this.activity = activity;
this.MessageList = MessageList;
this.context = c;
}
#Override
public int getCount() {
return MessageList.size();
}
#Override
public Object getItem(int location) {
return MessageList.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
final MessageList m = MessageList.get(position);
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
if (m.getDir().equals("left")) {
convertView = inflater.inflate(R.layout.left_message, null);
} else if (m.getDir().equals("right")) {
convertView = inflater.inflate(R.layout.right_message, null);
}
mHolder = new ViewHolder();
mHolder.message = (LinearLayout) convertView.findViewById(R.id.messages);
convertView.setTag(mHolder);
for(String message : m.getMessageList()){
TextView text = new MyTextView(activity);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0, 0, 0, 2);
text.setLayoutParams(p);
text.setText(message);
text.setPadding(8,8,8,8);
text.setTextSize(14f);
}
}
else {
mHolder = (ViewHolder) convertView.getTag();
}
Log.d("ffffffffffffffffff",""+m.getMessageList().size());
return convertView;
}
private class ViewHolder {
private LinearLayout message;
}
}
this is the result
but when i receive new data and i want to add it after do
adapter.notifyDataSetChanged();
its repeat the first data and add it to listView see result
we can see its repeat the data it must add new data i add it to arrayList not repeat old item on it i am sure about my ArrayList its right
at the end just lets see ho9w i add new data
first when its work without problem
private List<MessageList> Message_List = new ArrayList<MessageList>();
adapter = new MessageAdapter(this, Message_List, getApplicationContext());
message_list.setAdapter(adapter);
now i will add data for the first time
try {
JSONArray jsonarray = new JSONArray(data);
for (int i = 0; i < jsonarray.length(); i++) {
try {
JSONObject jsonobject = jsonarray.getJSONObject(i);
MessageList messageList = new MessageList();
String fname = jsonobject.getString("Fname");
int user_id = jsonobject.getInt("user_id");
String message = jsonobject.getString("message");
String date = jsonobject.getString("date");
String direction = jsonobject.getString("direction");
JSONArray MessageArry = jsonobject.getJSONArray("message");
ArrayList<String> ListMessage = new ArrayList<String>();
for (int j = 0; j < MessageArry.length(); j++) {
ListMessage.add((String) MessageArry.get(j));
}
messageList.setUser_id(user_id);
messageList.setMessage(message);
messageList.setDate(date);
messageList.setFname(fname);
messageList.setDir(direction);
messageList.setMessageList(ListMessage);
Message_List.add(messageList);
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
data used its
String data = "[ { \"direction\": \"left\",\"user_id\": \"50\",\"Fname\": \"Mohamed Mohamed\", \"status\": \"online\" , \"m_status\": \"sent\", \"date\": \"9:20\", \"message\": [\"hello\",\"hahhahah ok ok say hello\",\"-_-\",\"where are \",\"u ?\"] }, { \"direction\": \"right\",\"user_id\": \"50\",\"Fname\": \"Mustafa Naser\", \"status\": \"online\" , \"m_status\": \"sent\", \"date\": \"10:20\", \"message\": [\"hello\",\"what u want :/\",\"xD\"] }]";
when i get new data and i want to append it i add data like this
try {
JSONObject jsonobject1 = new JSONObject(json);
MessageList messageList = new MessageList();
String fname = jsonobject1.getString("name");
int user_id = jsonobject1.getInt("id");
String message = jsonobject1.getString("message");
String date = jsonobject1.getString("date");
String direction = jsonobject1.getString("direction");
ArrayList<String> ListMessage = new ArrayList<String>();
for (int j = 0; j < 1; j++) {
ListMessage.add(message);
}
messageList.setUser_id(user_id);
messageList.setMessage(message);
messageList.setDate(date);
messageList.setFname(fname);
messageList.setDir(direction);
messageList.setMessageList(ListMessage);
Message_List.add(messageList);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
adapter.notifyDataSetChanged();
data used its
{"message":"qqqqqqqq","id":50,"date":"2016-06-24 04:44:06","name":"medo medo","direction":"left","mode":0}

Each time you create a view, you put all messages inside, regardless of whether you have already shown them.
The problem is with the following loop:
for(String message : m.getMessageList()) {
// add a text view for each message
}
You need to find a way to check if the message has already been shown and add it to the layout only if it's not there.

I suggest you to do as follow:
In your adapterclass add this method:
public void updateList(List<MessageList> ){
this.MessageList = l;
notifyDataSetChanged();
}
And, after you fetch the new List, call the adapter.updateList(newList) method! It will update itself with the notifyDataSetChanged(); inside of the method.

Related

ListView doesn't refresh with notifydatasetchanged

I have a MySQL database of two columns displayed in an android ListView. I use Retrofit 1.9 to get the data from MySQL. The adapter for the ListView is a BaseAdapter and I don't have a DatabaseHelperclass. When I add a data in the ListView from my mobile phone, it doesn't refresh the ListView. I have to close and restart the app. I try to refresh with listViewAdapter.notifyDataSetChanged();.
This is the fragment where the listview is:
public class rightFragment extends Fragment {
String BASE_URL = "http://awebsite.com";
View view;
ListView listView;
ListViewAdapter listViewAdapter;
Button buttondisplay;
Button buttonadd;
EditText new_id;
EditText newWordFra;
EditText newWordDeu;
ArrayList<String> id = new ArrayList<>();
ArrayList<String> fra = new ArrayList<>();
ArrayList<String> deu = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(com.example.geeko.Deu.R.layout.fragment_right, container, false);
listView = (ListView) view.findViewById(com.example.geeko.Deu.R.id.listView); //<<<< ADDED NOTE use your id
listViewAdapter = new ListViewAdapter(getActivity(), id, fra, deu);
displayData();
buttondisplay = (Button) view.findViewById(com.example.geeko.Deu.R.id.buttondisplay);
buttonadd = (Button) view.findViewById(com.example.geeko.Deu.R.id.buttonadd);
buttondelete = (Button) view.findViewById(com.example.geeko.Deu.R.id.newID);
newWordFra = (EditText) view.findViewById(com.example.geeko.Deu.R.id.newWordFra);
newWordDeu = (EditText) view.findViewById(com.example.geeko.Deu.R.id.newWordDeu);
buttonadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Toast.makeText(MainActivity.this , "button add", Toast.LENGTH_LONG).show();
insert_data();
listViewAdapter.notifyDataSetChanged();
}
});
buttondisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (buttondisplay.getText().toString().contains("Show")) {
listView.setAdapter(listViewAdapter);
buttondisplay.setText("Hide");
} else {
listView.setAdapter(null);
buttondisplay.setText("Show");
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long ids) {
new_id.setText(id.get(position));
newWordFra.setText(fra.get(position));
newWordDeu.setText(deu.get(position));
}
});
return view;
}
public void insert_data() {
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL) //Setting the Root URL
.build();
AppConfig.insert api = adapter.create(AppConfig.insert.class);
api.insertData(
newWordFra.getText().toString(),
newWordDeu.getText().toString(),
new Callback<Response>() {
#Override
public void success(Response result, Response response) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
String resp;
resp = reader.readLine();
Log.d("success", "" + resp);
JSONObject jObj = new JSONObject(resp);
int success = jObj.getInt("success");
if(success == 1){
Toast.makeText(getActivity(), "Successfully inserted", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getActivity(), "Insertion Failed", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Log.d("Exception", e.toString());
} catch (JSONException e) {
Log.d("JsonException", e.toString());
}
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
}
}
);
}
public void displayData() {
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL) //Setting the Root URL
.build();
AppConfig.read api = adapter.create(AppConfig.read.class);
api.readData(new Callback<JsonElement>() {
#Override
public void success(JsonElement result, Response response) {
String myResponse = result.toString();
Log.d("response", "" + myResponse);
try {
JSONObject jObj = new JSONObject(myResponse);
int success = jObj.getInt("success");
if (success == 1) {
JSONArray jsonArray = jObj.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
id.add(jo.getString("id"));
fra.add(jo.getString("fra"));
deu.add(jo.getString("deu"));
}
listView.setAdapter(listViewAdapter);
} else {
Toast.makeText(getActivity(), "No Details Found", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Log.d("exception", e.toString());
}
}
#Override
public void failure(RetrofitError error) {
Log.d("Failure", error.toString());
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
}
}
);
}
}
This is the listViewAdpater class :
public class ListViewAdapter extends BaseAdapter {
private final Context context;
private ArrayList<String> id = new ArrayList<String>();
private ArrayList<String> fra = new ArrayList<String>();
private ArrayList<String> deu = new ArrayList<String>();
LayoutInflater layoutInflater;
public ListViewAdapter(Context ctx, ArrayList<String> id, ArrayList<String> fra, ArrayList<String> deu) {
this.context = ctx;
this.id = id;
this.fra = fra;
this.deu = deu;
}
#Override
public int getCount() {
return id.size();
}
#Override
public Object getItem(int position) {
return id.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View view, ViewGroup parent) {
final Holder holder;
if (view == null) {
layoutInflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item, null);
holder = new Holder();
holder.txt_fra = (TextView) view.findViewById(R.id.fra);
holder.txt_deu = (TextView) view.findViewById(R.id.deu);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
holder.txt_fra.setText(fra.get(position));
holder.txt_deu.setText(deu.get(position));
return view;
}
static class Holder {
TextView txt_fra, txt_deu;
}
}
Should I create a method to refresh my ListView?
First of all I would suggest to remove the unnecessary initialization of your variables inside the Adapter:
private ArrayList<String> id = new ArrayList<String>();
private ArrayList<String> fra = new ArrayList<String>();
private ArrayList<String> deu = new ArrayList<String>();
By assigning the pointer from your fragment you will already assign an initialized ArrayList. You want both pointers to point at the same ArrayList Object so changes apply to both.
Apparently, the data stored in the Adapter is not being updated correctly. I would suggest to debug your app - setting the breakpoint so that you can see the data that the Adapter stores after an update.
The idea of writing a method for updates has already been implemented with the notifyDataSetChanged(). Just override the method in your adapter and do eventual changes before calling the super.notifyDataSetChanged().
If you have any success with those changes let us know.
buttonadd.setOnClickListener(new View.OnClickListener() {
...
insert_data();
listViewAdapter.notifyDataSetChanged();
}
});
In your onClickListener, you are calling insert_data(), followed by listViewAdapter.notifyDataSetChanged()
insert_data() method is sending a network request to retrieve the data to populate the list. BUT, you are calling notifyDataSetChanged BEFORE the network request is done. That's why the listView is empty, and will stay empty. You need to wait AFTER the network request and AFTER you have populated your ArrayList with the data to call notifyDataSetChanged.
How do we know the network request is done? Simply at the end of the callback (which you've implemented):
new Callback<Response>() {
#Override
public void success(Response result, Response response) {...
At the end of the success method, you call listViewAdapter.notifyDataSetChanged() method.
Hope that makes sense! Try it and let us know what happens
I've founded a solution. I've added getActivity.recreate(); in the method insert_data.
public void success(Response result, Response response) {
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(result.getBody().in()));
String resp;
resp = reader.readLine();
Log.d("success", "" + resp);
JSONObject jObj = new JSONObject(resp);
int success = jObj.getInt("success");
if(success == 1){
Toast.makeText(getActivity(), "Successfully inserted",
Toast.LENGTH_SHORT).show();
getActivity().recreate();
} else{
Toast.makeText(getActivity(), "Insertion Failed",
Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Log.d("Exception", e.toString());
} catch (JSONException e) {
Log.d("JsonException", e.toString());
}
}
I'm not sure that is the best solution but it works.

Cant access viewHolder in a customArrayadapter in another function?

I am creating an application with a list that list cards based on the value from the server.
I created a StudentCardArrayAdapter to achieve this and everything works fine. All the data has been populated in card list. also I able to get the values on button click in each card separately.
What I need is on clicking the button it will call a method requestion server for data asynchronously and get a value from the server and according to that value, i need to change the button text in that particular card.
My StudentCardArrayAdapter code:
public class StudentCardArrayAdapter extends ArrayAdapter<StudentCard> {
private static final String TAG = "CardArrayAdapter";
private List<StudentCard> cardList = new ArrayList<StudentCard>();
private Context mContext;
String selected = "0";
PreferenceHelper prefs;
CardViewHolder viewHolder;
View row;
ProgressDialog pd;
static class CardViewHolder {
TextView studentname;
TextView stop;
Button selectbutton;
CircleImageView imageId;
}
public StudentCardArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.mContext = context;
prefs = new PreferenceHelper(this.mContext);
pd = new ProgressDialog(this.mContext);
}
#Override
public void add(StudentCard object) {
cardList.add(object);
super.add(object);
}
#Override
public int getCount() {
return this.cardList.size();
}
#Override
public StudentCard getItem(int index) {
return this.cardList.get(index);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.student_card, parent, false);
viewHolder = new CardViewHolder();
viewHolder.studentname = (TextView) row.findViewById(R.id.studentname);
viewHolder.stop = (TextView) row.findViewById(R.id.stop);
viewHolder.selectbutton = (Button) row.findViewById(R.id.selectbutton);
viewHolder.imageId = (CircleImageView) row.findViewById(R.id.imageId);
row.setTag(viewHolder);
} else {
viewHolder = (CardViewHolder)row.getTag();
}
StudentCard card = getItem(position);
viewHolder.studentname.setText(card.getStudName());
viewHolder.studentname.setTextColor(Color.parseColor("#000000"));
viewHolder.stop.setText(card.getStudStop());
viewHolder.stop.setTextColor(Color.parseColor("#000000"));
if(card.getSelected().equals("1")){
viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
viewHolder.selectbutton.setEnabled(false);
}
else{
viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.select));
viewHolder.selectbutton.setEnabled(true);
}
final String studid = card.getStudId();
final String busname = prefs.getString("busname", "0");
final String schoolid = prefs.getString("schoolid", "");
viewHolder.selectbutton.setOnClickListener(new View.OnClickListener()
{
String updatedvalue = "0";
#Override
public void onClick(View v)
{
Log.e("studid",studid);
Log.e("busname",busname);
Log.e("schoolid",schoolid);
selectstudent(v, studid, busname, schoolid,mContext);
//Toast.makeText(v.getContext(), amountinfo, Toast.LENGTH_SHORT).show();
/*SnackbarManager.show(Snackbar.with(this) // context
.text(amountinfo));*/
}
});
Picasso.with(mContext).load(card.getImageUrl()).fit().error(R.mipmap.ic_launcher).into(viewHolder.imageId);
return row;
}
public void selectstudent(final View v, String studid, String busname, String schoolid, final Context mContext) {
String returnedselected = "0";
Log.e("BASE_URL_STUDENT_UPDATE", Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid);
RestClientHelper.getInstance().get(Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid, new RestClientHelper.RestClientListener() {
#Override
public void onSuccess(String response) {
Log.e("RESULT", response);
try {
JSONObject result = new JSONObject(response);
JSONArray posts = result.optJSONArray("status");
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
String status = post.optString("status");
if (status.equals("true")) {
selected = post.optString("selected");
} else {
selected = post.optString("selected");
String error = post.optString("error");
SnackbarManager.show(Snackbar.with(getContext()) // context
.text(error));
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
} finally {
if(selected.equals("1")){
viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
viewHolder.selectbutton.setEnabled(false);
}
}
}
#Override
public void onError(String error) {
Log.e("error", error);
selected = "0";
}
});
}
}
I used the below code but nothing works.. No error also.. and not change in button text.I get value of selected as 1 from server.
if(selected.equals("1")){
viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
viewHolder.selectbutton.setEnabled(false);
}
I am new to android.. and is stuck here. Please help me out.
FINALLY IT WORKED
As changes mention by Krish, I updated the code suggested by him.
And added this changes in onClick it worked
if(card.getSelected().equals("1")){
viewHolder.selectbutton.setText(mContext.getResources().getString(R.string.selected));
viewHolder.selectbutton.setEnabled(false);
}
Change the code like this,
public void selectstudent(StudentCard card, String studid, String busname, String schoolid, final Context mContext) {
String returnedselected = "0";
Log.e("BASE_URL_STUDENT_UPDATE", Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid);
RestClientHelper.getInstance().get(Constants.BASE_URL_STUDENT_UPDATE + "?studid=" + studid+"&busname="+busname+"&schoolid="+schoolid, new RestClientHelper.RestClientListener() {
#Override
public void onSuccess(String response) {
Log.e("RESULT", response);
try {
JSONObject result = new JSONObject(response);
JSONArray posts = result.optJSONArray("status");
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
String status = post.optString("status");
if (status.equals("true")) {
selected = post.optString("selected");
} else {
selected = post.optString("selected");
String error = post.optString("error");
SnackbarManager.show(Snackbar.with(getContext()) // context
.text(error));
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
} finally {
if(selected.equals("1")){
card.setSelected("1");
notifyDataSetChanged();
}
}
}
#Override
public void onError(String error) {
Log.e("error", error);
selected = "0";
}
});
}
and change this line like this ,
final StudentCard card = getItem(position);
and call method inside onclick.
selectstudent(card, studid, busname, schoolid,mContext);

Remove an item from view inside an adapter

I am trying to remove an item from view when its flag become 4. I tried mObjects.remove(position) and then notifyDataSetChanged(). but it didn't worked.we tried all the following
if (getItem(position).getFlag().trim().equalsIgnoreCase("4")) {
remove(position);
adapter.notifyDataSetChanged();
matcheslistview.setAdapter(adapter);
also this one
// mObjects.remove(position)
// notifyDataSetChanged();
and this one
// mObjects.remove(position);
//remove(position);
//mainObjects.remove(position);
//notifyDataSetChanged();
and this one
// Object toRemove = adapter.getItem(position);
// mObjects.remove(toRemove);
// mObjects.clear();
and all the time we got java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0.Here is the complete adapter class
private class MatchedDataAdapter extends BaseAdapter implements Filterable {
private AQuery aQuery;
private Activity mActivity;
private LayoutInflater mInflater;
private SessionManager sessionManager;
private int uflag;
MyFilter mfilter;
DatabaseHandler db;
ArrayList<LikeMatcheddataForListview> mObjects;
ArrayList<LikeMatcheddataForListview> mainObjects;
Context context;
public MatchedDataAdapter(Activity context,
ArrayList<LikeMatcheddataForListview> objects,
int imageHeigthAndWidth[]) {
this.mObjects = objects;
mainObjects = objects;
//Log.e("size", Integer.toString(mObjects.size()));
this.mActivity = context;
try {
mInflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
catch (Exception e)
{
e.printStackTrace();
}
aQuery = new AQuery(context);
db = new DatabaseHandler(context);
}
#Override
public int getCount() {
return mObjects.size();
}
#Override
public LikeMatcheddataForListview getItem(int position) {
return mObjects.get(position);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.matchedlistviewitem,
null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.userimage);
holder.textview = (TextView) convertView
.findViewById(R.id.userName);
holder.lastMasage = (TextView) convertView
.findViewById(R.id.lastmessage);
holder.imgStatus = (ImageView) convertView
.findViewById(R.id.imgStatus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText(getItem(position).getUserName());
if (getItem(position).getFlag().trim().equalsIgnoreCase("4")) {
mObjects.remove(position);
adapter.notifyDataSetChanged();
matcheslistview.setAdapter(adapter);
we want to remove Item with flag 4,we are reading this flag with a service from db and onrecive we call class DisplayContentTask as below
class GetLikeMatchedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
new DisplayContentTask(intent).execute();
}
}
how we can get Item position in order to remove the Item with flag 4...or My be another approach to remove Item with flag 4 we don't know but appreciate your help on this
class DisplayContentTask extends AsyncTask<Void, Void, Void> {
Intent intent;
private Ultilities mUltilities = new Ultilities();
private List<NameValuePair> getuserparameter;
private String likedmatchedata, Unmatchedata;
int match1;
private LikedMatcheData matcheData;
private ArrayList<com.appdupe.flamer.pojo.Likes> likesList;
private LikeMatcheddataForListview matcheddataForListview;
DatabaseHandler mDatabaseHandler = new DatabaseHandler(getActivity());
private boolean isResponseSuccess = true;
ArrayList<LikeMatcheddataForListview> tempArray = new ArrayList<LikeMatcheddataForListview>();
#Override
protected void onPreExecute() {
super.onPreExecute();
AppLog.Log(TAG, "BackgroundTaskForFindLikeMatched onPreExecute ");
}
DisplayContentTask(Intent intent) {
this.intent = intent;
}
#Override
protected Void doInBackground(Void... voids) {
try {
File appDirectory = mUltilities
.createAppDirectoy(getResources().getString(
R.string.appdirectory));
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground appDirectory "
+ appDirectory);
File _picDir = new File(appDirectory, getResources().getString(
R.string.imagedirematchuserdirectory));
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground ");
// getuserparameter = mUltilities.getUserLikedParameter(params);
likedmatchedata = intent.getStringExtra("GET_MATCHED_RESPONSE");
// Unmatchedata = intent.getStringExtra("GET_UNMATCHED_RESPONSE");//hadi
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground likedmatchedata "
+ likedmatchedata);
Gson gson = new Gson();
matcheData = gson.fromJson(likedmatchedata,
LikedMatcheData.class);
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground matcheData "
+ matcheData);
// "errNum": "51",
// "errFlag": "0",
// "errMsg": "Matches found!",
if (matcheData.getErrFlag() == 0) {
likesList = matcheData.getLikes();
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground likesList "
+ likesList);
if (tempArray != null) {
tempArray.clear();
}
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground likesList sized "
+ likesList.size());
Log.v("Matches", "" + likesList.size());
match1 = likesList.size();
for (int i = 0; i < likesList.size(); i++) {
Log.d("likelist", likesList.toString());
matcheddataForListview = new LikeMatcheddataForListview();
String userName = likesList.get(i).getfName();
String facebookid = likesList.get(i).getFbId();
// Log.i(TAG, "Background facebookid......"+facebookid);
String picturl = likesList.get(i).getpPic();
int falg = likesList.get(i).getFlag();
// if (likesList.get(i).getFlag()==4) {
// likesList.remove(getId());
// }
Log.i("komak10",""+likesList.get(i).getFlag());
String latd = likesList.get(i).getLadt();
matcheddataForListview.setFacebookid(facebookid);
matcheddataForListview.setUserName(userName);
matcheddataForListview.setImageUrl(picturl);
matcheddataForListview.setFlag("" + falg);
matcheddataForListview.setladt(latd);
// matcheddataForListview.setFilePath(filePath);
File imageFile = mUltilities.createFileInSideDirectory(
_picDir, userName + facebookid + ".jpg");
// logDebug("BackGroundTaskForUserProfile doInBackground imageFile is profile "+imageFile.isFile());
Utility.addBitmapToSdCardFromURL(likesList.get(i)
.getpPic().replaceAll(" ", "%20"), imageFile);
matcheddataForListview.setFilePath(imageFile
.getAbsolutePath());
if (!preferences.getString(Constant.FACEBOOK_ID, "")
.equals(facebookid)) {
tempArray.add(matcheddataForListview);
}
}
DatabaseHandler mDatabaseHandler = new DatabaseHandler(
getActivity());
// SessionManager mSessionManager = new SessionManager(
// MainActivity.this);
String userFacebookid = preferences.getString(
Constant.FACEBOOK_ID, "");
//
boolean isdataiserted = mDatabaseHandler.insertMatchList(
tempArray, userFacebookid);
} else if (matcheData.getErrFlag() == 1) {
if(tempArray!=null)
{
tempArray.clear();
}
} else {
// do nothing
}
} catch (Exception e) {
AppLog.handleException(
"BackgroundTaskForFindLikeMatched doInBackground Exception ",
e);
// some thing wrong happend
isResponseSuccess = false;
}
return null;
}
Don't remove the object in getview, if you have to filter it, filter it before sending out to adapter. May be possible that while creating the child view the 1st cell has tag "4" now the view didn't create(since return was not called) but you are trying to remove its position, so it will definitely give you IndexOutOfBoundsException.
My best solution would be, set the adapter with
new ArrayList<LikeMatcheddataForListview>()
whenever you start the screen. Once your AsyncTask completes filter out the child with tags "4"(better filter it out in the asynctask only, less task in ui thread) then refresh the adapter, like
public void refresh(ArrayList<LikeMatcheddataForListview>() arrObjects){
objects = arrObjects;
notifyDataSetChanged();
}
Check it out, it should do the trick
Please try following
Your code
if (getItem(position).getFlag().trim().equalsIgnoreCase("4")) {
mObjects.remove(position);
adapter.notifyDataSetChanged();
matcheslistview.setAdapter(adapter);
}
TO
do not set adapter again to list view
if (getItem(position).getFlag().trim().equalsIgnoreCase("4")) {
mObjects.remove(position);
notifyDataSetChanged();
}
This may not be correct approach to remove the item form listview.
Whenever your adapter data is getting changed then just check if that flag matches your string i.e. "4" in each item and remove the respective item from the list and just call notifyItemRemoved with position insted of notifyDataSetChanged

Listview items were added in wrong place

I am using listview with dynamic items.It is saving in wrong place(That means 1st item showing in third item, second item showing in 5th item, etc).I dont know how to solve this one.I have added the relevant code.Please check it.
EDIT:
UpcomingGoalAdapter.java:
public class UpcomingGoalAdapter extends ArrayAdapter<UpcomingGoalItems> {
private Context context;
#SuppressWarnings("unused")
private List<UpcomingGoalItems> items;
String eventIdForVol;
private UpcomingGoalAdapter adapter;
String userIdStr, tokenStr;
public UpcomingGoalAdapter(Context context, int resource, List<UpcomingGoalItems> objects) {
super(context, resource, objects);
this.context = context;
this.items = objects;
this.adapter = this;
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final UpcomingGoalItems rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.adapt_upcoming_goal, null);
holder = new ViewHolder();
holder.tvInterest = (TextView) convertView.findViewById(R.id.tv_interest_goal_adapt);
holder.ivEdit = (ImageView) convertView.findViewById(R.id.iv_edit_goal_adapt);
holder.tvCount = (TextView) convertView.findViewById(R.id.tv_count_goal_adapt);
holder.ivDelete = (ImageView) convertView.findViewById(R.id.iv_delete_goal_adapt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if((rowItem.getInterest().equals(null) || rowItem.getInterest().equals(""))
&& (rowItem.getLocation().equals(null) || rowItem.getLocation().equals(""))
&& (rowItem.getFromDate().equals(null) || rowItem.getFromDate().equals(""))
&& (rowItem.getToDate().equals(null) || rowItem.getToDate().equals(""))) {
holder.tvInterest.setText("-");
} else {
holder.tvInterest.setText("Looking for "+rowItem.getInterest() + " in " + rowItem.getSplitLocation() + " On ( "
+ rowItem.getFromDate() + " - " + rowItem.getToDate()+" ) ");
}
holder.tvCount.setText(rowItem.getCount());
return convertView;
}
private class ViewHolder {
TextView tvInterest;
ImageView ivEdit;
TextView tvCount;
ImageView ivDelete;
}
UpcomingGoalActivity.java:
ArrayList<UpcomingGoalItems> itemsaArrayList;
UpcomingGoalAdapter itemsAdapter;
ListView listView;
itemsaArrayList = new ArrayList<UpcomingGoalItems>();
itemsAdapter = new UpcomingGoalAdapter(UpcomingGoalActivity.this, R.layout.adapt_upcoming_goal, itemsaArrayList);
listView = (ListView) findViewById(R.id.lv_interest_goal_search);
listView.setAdapter(itemsAdapter);
hitGoalApi();
private void hitGoalApi(){
String myGoalsUrl = PK_MY_GOALS;
Log.e("myGoalsUrl", myGoalsUrl);
StringRequest request = new StringRequest(Request.Method.GET, myGoalsUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss();
if(response != null && !response.startsWith("<HTML>")){
Log.e("MyGoalsRes", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArrData = jsonObject.getJSONArray("data");
for(int i=0; i < jsonArrData.length(); i++){
JSONObject getDataJsonObj = jsonArrData.getJSONObject(i);
LOCATION = getDataJsonObj.getString("location");
String[] placeArray = LOCATION.split("\\s*,\\s*");
Log.e("placeArray", ""+ Arrays.toString(placeArray));
String placeStr = placeArray[0];
FROM_DATE = getDataJsonObj.getString("fromdate");
TO_DATE = getDataJsonObj.getString("todate");
GOAL_ID = getDataJsonObj.getString("g_id");
getInterest = getDataJsonObj.getString("users_interest_goals");
hitCountApi(GOAL_ID, placeStr, FROM_DATE, TO_DATE, getInterest);
}
}
catch (JSONException e){
e.printStackTrace();
}
}else{
toastShort(getApplicationContext(), "Check Internet");
}
}
}
private void hitCountApi(final String goalId, final String splitLoc, final String fromDate,
final String toDate, final String getInteresn) {
String countUrl = PK_GOAL_SEARCH + goalId + ".json";
Log.e("countUrl", countUrl);
StringRequest request = new StringRequest(Request.Method.GET, countUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss();
if (response != null && !response.startsWith("<HTML>")) {
Log.e("CountRes", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject metaJsonObj = jsonObject.getJSONObject("_metadata");
String totalRecStr = metaJsonObj.getString("total_records");
Log.e("totalRecStr", "" + totalRecStr);
UpcomingGoalItems items = new UpcomingGoalItems();
items.setSplitLocation(splitLoc);
items.setFromDate(fromDate);
items.setToDate(toDate);
items.setGoalId(goalId);
items.setInterest(getInterest);
items.setCount(totalRecStr); ;
itemsaArrayList.add(items);
itemsAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
toastShort(getApplicationContext(), "Check Internet");
}
}
})
Based on the Volley response order I have to show the right id for listview.It takes wrong order.
Let me know why it is ordering at wrong place and how to solve this.Thank You.
tldr: Use ArrayList instead of List:
private ArrayList<UpcomingGoalItems> items;
The ArrayAdapters I use tend to look like this:
(the ArrayList< String> names is an ArrayList created from the names of the ArrayList< Task>)
public class ArrayAdapterTask extends ArrayAdapter<String> {
private final Context context;
private int layoutId;
private ArrayList<Task> tasks;
public ArrayAdapterTask(Context context, ArrayList<String> names, ArrayList<Task> tasks, int layoutId) {
super(context, layoutId, names);
this.context = context;
this.tasks = null;
this.tasks = tasks;
this.layoutId = layoutId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// Do stuff with the tasks.get(position)
}
}

Android:Getting a value recyclerview item value without using `getAdapterPosition()`

I have a recyclerview that populate the json data which is coming from an api.
Here are the items in each recylerview item project_id,title,etc, with each recyclerview i have checkbox associated with it.
Here is the code for populating the data in recyclerview.
for (int j = 0, count = data.length(); j < count; j++) {
String name = json.getString(TAG_PROJECT_ID);
PROJECT_ID.add(name);
Log.e("array id added",name);
String name1 = json.getString(TAG_PROJECT_TITLE);
PROJECT_TITLE.add(name1);
}
when click on the checkbox on each recyclerviewitem i want to get the project_id of the corresponding item.
//click listener
mCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GET THE PROJECT ID
}
)}
UPDATED THE ACTUAL CODE
(for simplicity ,i had added only relevant code here )
My Recyclerview adapter
public class FavouriteManager extends RecyclerView.Adapter<FavouriteManager.RecyclerViewHolder> {
ArrayList<String> PROJECT_ID;
ArrayList<String> PROJECT_TITLE;
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new RecyclerViewHolder(v1);
}
#Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
projecttitle = PROJECT_TITLE.get(i);
viewHolder.mProjectName.setText(projecttitle);
#Override
public int getItemCount() {
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
set = pref.getStringSet("FAV", null);
if (set != null) {
selected = new ArrayList<String>(set);
Log.e("Item Added", "");
} else {
selected = new ArrayList<String>();
}
Length = selected.size();
if (Length == 0) {
RelativeLayout IMG = (RelativeLayout) getActivity().findViewById(R.id.fav_img);
IMG.setVisibility(View.VISIBLE);
}
return Length;
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView mProjectName;
CheckBox mCheck;
RecyclerViewHolder(final View itemView) {
super(itemView);
mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
mCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int h = getAdapterPosition();
if (!offlinejson.equals("")) {
try {
JSONObject jsonObject = new JSONObject(offlinejson);
JSONArray data = jsonObject.getJSONArray(TAG_DATA);
JSONObject jsondata = data.getJSONObject(h);
Log.e("getCheckedPos(getID)", String.valueOf(jsondata));
check = PROJECT_ID.get(h);
Log.e("getCheckedPos(getID)", String.valueOf(jsondata));
if (selected.contains(check)) {
selected.remove(check);
mCheck.setBackgroundResource(R.drawable.ic_favorite_white1_24dp);
Snackbar snackbar = Snackbar.make(v, "Property Unfavorited", Snackbar.LENGTH_SHORT);
snackbar.show();
notifyItemRemoved(h);
Log.e("FOUND","found");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
try {
JSONObject jsonObject = new JSONObject(offlinejson);
JSONArray data = jsonObject.getJSONArray(TAG_DATA);
PROJECT_ID = new ArrayList<String>();
PROJECT_TITLE = new ArrayList<String>();
for (int j = 0, count = data.length(); j < count; j++) {
json = data.getJSONObject(j);
//JSONArray jsonArray = new JSONArray(json);
// Log.e("JSON", String.valueOf(json));
if (selected.contains(String.valueOf(json.get(TAG_PROJECT_ID)))) {
try {
String name = json.getString(TAG_PROJECT_ID);
PROJECT_ID.add(name);
Log.e("array id added",name);
String name1 = json.getString(TAG_PROJECT_TITLE);
PROJECT_TITLE.add(name1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
Note:
I don't want use getAdapterPosition() method because its not applicable with my json structure.
Among different types of solutions, you can also use setTag() and getTag().
Set your project id in to checkbox and get when required.
Eg: mCheck.setTag(); //(key,value) or (value).
For getting ID use getTag() and to set ID use setTag().
http://developer.android.com/reference/android/widget/CheckBox.html

Categories

Resources