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)
}
}
Related
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);
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
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.
I've this adapter class :
public class NoteFeedListAdapter extends RecyclerView.Adapter<feedItemsHolder>{
private Activity activity;
private LayoutInflater inflater;
private List<NoteFeedItem> feedItems;
private List<CommentModel> commentItems;
private NoteCommentListAdapter adapter;
private RecyclerView mRecyclerView;
ImageLoader imageLoader = NoteAppController.getInstance().getImageLoader();
private static final String URL_LIST_VIEW_COMMENT = "http://url.com";
private int level = 0;
private Context mContext;
public NoteFeedListAdapter(Context context, List<NoteFeedItem> feedItems) {
this.feedItems = feedItems;
this.mContext = context;
}
public feedItemsHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.feed_item, null);
feedItemsHolder mh = new feedItemsHolder(v);
return mh;
}
public void onBindViewHolder(final feedItemsHolder fItemsHolder, final int i) {
final NoteFeedItem item = feedItems.get(i);
fItemsHolder.setLevel(item.getLevel());
if (item.getName2() != null) {
fItemsHolder.mHiddenComment.setText(item.getName2()+": "+item.getComment2());
fItemsHolder.feedImageView.setVisibility(View.VISIBLE);
and Inside onBindViewHolder :
int jComment = Integer.parseInt(item.getJumlahComment().toString());
if( jComment > 0){
fItemsHolder.mHiddenComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this code is what I used to call asyntask but result from asynctask cannot be shown in this adapter
commentItems = new ArrayList<CommentModel>();
adapter = new NoteCommentListAdapter(mContext, commentItems);
mRecyclerView = new RecyclerView(mContext);
getListViewComments(item.getUserid(), item.getId(),fItemsHolder,i, commentItems, adapter, mRecyclerView);
commentItems = new ArrayList<CommentModel>();
adapter = new NoteCommentListAdapter(mContext, commentItems);
mRecyclerView.setAdapter(adapter);
}
});
}
...
} else {
fItemsHolder.mHiddenComment.setVisibility(View.GONE);
fItemsHolder.mLinearHiddenComment.setVisibility(View.GONE);
}
if(item.getLevel() == Level.LEVEL_ONE){
level = Level.LEVEL_TWO;
}else if(item.getLevel() == Level.LEVEL_TWO){
level = Level.LEVEL_THREE;
}
}
public int getItemCount() {
return (null != feedItems ? feedItems.size() : 0);
}
private void getListViewComments(final String userid, String id_note,final feedItemsHolder feedItemsHolder, int i, final List<CommentModel> commentItems, final NoteCommentListAdapter adapter, final RecyclerView mRecyclerView) {
class ambilComment extends AsyncTask<String, Void, String> {
ProgressDialog loading;
com.android.personal.asynctask.profileSaveDescription profileSaveDescription = new profileSaveDescription();
String result = "";
InputStream inputStream = null;
#Override
protected void onPreExecute() {
feedItemsHolder.mLoading.setVisibility(View.GONE);
feedItemsHolder.mHiddenComment.setVisibility(View.GONE);
feedItemsHolder.mLinearHiddenComment.setVisibility(View.GONE);
feedItemsHolder.mLoading.setVisibility(View.VISIBLE);
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("userid", params[0]);
data.put("id_note", params[1]);
String result = profileSaveDescription.sendPostRequest(URL_LIST_VIEW_COMMENT,data);
return result;
}
protected void onPostExecute(String s) {
JSONArray dataJsonArr = null;
if(s.equals(null)){
Toast.makeText(mContext, "Internet Problem.", Toast.LENGTH_SHORT).show();
}else{
try{
JSONObject json = new JSONObject(s);
String id_note = json.getString("id_note");
Toast.makeText(mContext, id_note, Toast.LENGTH_SHORT).show();
dataJsonArr = json.getJSONArray("data");
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
String id_comment = c.getString("id_comment");
String uid = c.getString("userid");
String profile_name = c.getString("profile_name");
String profile_photo = c.getString("profile_photo");
String amount_of_like = c.getString("amount_of_like");
String amount_of_dislike = c.getString("amount_of_dislike");
String amount_of_comment = c.getString("amount_of_comment");
String content_comment = c.getString("content_comment");
String tgl_comment = c.getString("tgl_comment");
String parent_id = c.getString("parent_id");
CommentModel citem = new CommentModel();
citem.setId_note(id_note);
citem.setId_comment(id_comment);
citem.setUserid(uid);
citem.setProfileName(profile_name);
String pPhoto = c.isNull("profile_photo") ? null : c.getString("profile_photo");
citem.setProfile_photo(pPhoto);
citem.setJumlahLove(amount_of_like);
citem.setJumlahNix(amount_of_dislike);
citem.setJumlahComment(amount_of_comment);
citem.setContent_comment(content_comment);
citem.setTimeStamp(tgl_comment);
String prntID = c.isNull("parent_id") ? null : c.getString("parent_id");
citem.setParent_id(prntID);
citem.setLevel(level);
commentItems.add(citem);
}
adapter.notifyDataSetChanged();
}catch(JSONException e){
e.printStackTrace();
Log.w("getListNotesComment", "exception");
}
}
/* iH.mHiddenComment.setText("");*/
}
}
ambilComment ru = new ambilComment();
ru.execute(userid, id_note);
}
The problem is that I wanna add data from Asynctask and shown on another adapter. But how can i do that? please help. view from another adapter couldn't show with this code.
I just want to add images to different item according to my condition.
e.g.if status is equal to open then set open.png img status is equal to wip then wip.png status is equal to closed then closed.png and after setting images i want to open the activity according to ticket no and show all the detail of ticket on item click. so what should i write to set image and code to write on item method.i am new in android, here is my code
class viewticket extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pdialog = new ProgressDialog(UserLogedIn.this);
pdialog.setMessage("Loading....");
pdialog.setIndeterminate(false);
pdialog.setCancelable(false);
pdialog.show();
}
#Override
protected String doInBackground(String... params) {
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("userid", u_id));
// JSONObject jsonArray = jpar.makeHttpRequest(URLMyTicket, "POST", param);
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(URLMyTicket, ServiceHandler.POST, param);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null)
{
try {
contacts = new JSONArray(jsonStr);
a=contacts.length();
Log.v(TAG, ".................." + a);
if(a > 0 ) {
id = new String[contacts.length()];
stats = new String[contacts.length()];
due_date = new String[contacts.length()];
prob = new String[contacts.length()];
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String aa = c.getString(TAG_ID);
String bb = c.getString(TAG_PROB);
String cc = c.getString(TAG_status);
String dd = c.getString(TAG_due_date);
Log.v(TAG, "TAG_ID" + aa);
Log.v(TAG, "TAGPROB" + bb);
Log.v(TAG, "TAGPROB" + cc);
Log.v(TAG, "TAGPROB" + dd);
id[i] = aa;
prob[i] = bb;
stats[i] = cc;
due_date[i] = dd;
Log.v(TAG, "aaaaa" + id[i]);
Log.v(TAG, "bbbbb" + prob[i]);
Log.v(TAG, "cccc" + stats[i]);
Log.v(TAG, "dddd" + due_date[i]);
}
}
} catch (JSONException e) {
System.out.print("hiiiiiiiiiiii" );
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Dismiss the progress dialog
Log.v(TAG, "length of aaaaa" + a);
if(id!=null && id.length > 0 )
{
Ticket_adapter adapter=new Ticket_adapter(UserLogedIn.this,id,prob,stats,due_date);
lv.setAdapter(adapter);
}
else
{
Toast.makeText(UserLogedIn.this, "No Ticket Present", Toast.LENGTH_SHORT).show();
}
pdialog.hide();
pdialog.dismiss();
}
}
so could you please write code for me to set image according to status and how to write onitemclicklistener method.
adapter class
public class Ticket_adapter extends ArrayAdapter<String> {
Context context;
String[] id;
String[] prob;
String[] stats;
String[] due_date;
int [] stimg;
LayoutInflater inflater;
public Ticket_adapter(UserLogedIn context, String[] id,String[] prob,String[] stats,String[] due_date,int[] stimg) {
super(context, R.id.list_item,id);
this.context=context;
this.id=id;
this.prob=prob;
this.stats=stats;
this.due_date=due_date;
this.stimg=stimg;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.list_item,null);
}
TextView idtxt=(TextView)convertView.findViewById(R.id.uid);
TextView probtxt=(TextView)convertView.findViewById(R.id.prob);
TextView stustxt=(TextView)convertView.findViewById(R.id.status);
TextView duetxt=(TextView)convertView.findViewById(R.id.duedate);
ImageView immm=(ImageView)convertView.findViewById(R.id.image12);
idtxt.setText(id[position]);
probtxt.setText(prob[position]);
stustxt.setText(stats[position]);
duetxt.setText(due_date[position]);
immm.setImageResource(stimg[position]);
return convertView;
}
}
public class Ticket_adapter extends ArrayAdapter<String> {
Context context;
String[] id;
String[] prob;
String[] stats;
String[] due_date;
int [] stimg;
LayoutInflater inflater;
public Ticket_adapter(UserLogedIn context, String[] id,String[] prob,String[] stats,String[] due_date,int[] stimg) {
super(context, R.id.list_item,id);
this.context=context;
this.id=id;
this.prob=prob;
this.stats=stats;
this.due_date=due_date;
this.stimg=stimg;
}
static class ViewHolder {
public TextView idtxt;
public TextView probtxt;
public TextView stustxt;
public TextView duetxt;
public ImageView immm;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.idtxt= (TextView) rowView.findViewById(R.id.uid);
viewHolder.probtxt= (TextView) rowView.findViewById(R.id.prob);
viewHolder.stustxt= (TextView) rowView.findViewById(R.id.status);
viewHolder.duetxt= (TextView) rowView.findViewById(R.id.duedate);
viewHolder.immm= (ImageView) rowView
.findViewById(R.id.image12);
rowView.setTag(viewHolder);
}
viewHolder.idtxt.setText(id[position]);
viewHolder.probtxt.setText(prob[position]);
viewHolder.stustxt.setText(stats[position]);
viewHolder.duetxt.setText(due_date[position]);
if(stats[position].equals("open"))
{
viewHolder.immm.setImageResource(R.drawable.openimage);
}
else if(stats[position].equals("wip"))
{
viewHolder.immm.setImageResource(R.drawable.wipimage);
}
else
{
viewHolder.immm.setImageResource(R.drawable.closedimage);
}
return rowView;
}
}
in your getView() method
if(stats[position].Equals("open"))
{
immm.setImageResource(R.drawable.open);
}
else if(stats[position].Equals("wip"))
{
immm.setImageResource(R.drawable.wip);
}
else
{
immm.setImageResource(R.drawable.closed);
}