Tuesday, May 14, 2013

[android help] After parsing, the images in imageview of listview are not the right images(random image)


I made oneListView withimageview and textview. After parsing json data i am getting right text intextview but the image inimageview are not the right image.How can i make it correct? thanks in advance..


My code for parser.java class.



public class Parser {
private ArrayList mDataSource = new ArrayList();
public Parser(String json) {
jsonParser(json);
}
private void jsonParser(String json) {
try {
JSONObject obj = new JSONObject(json);
//JSONObject var = obj.getJSONObject("activities");
JSONArray list = obj.getJSONArray("activities");
int len = list.length();
for (int i = 0; i < len; i++) {
try {
JSONObject data = list.getJSONObject(i);
String summary=data.getString("type");
String state ;
String title ;
String image ;
String concatinate;
String review ;
String status ;
String url ;
//String image1;

// String image1;
ArrayList addList = new ArrayList();
JSONArray addArray = obj.getJSONArray("activities");
int size = addArray.length();
/**
* parser sub json
*/
for(int j = 0; j < size; j++) {
addList.add(addArray.getJSONObject(j).getString("activity_id"));
}

if(summary.contains("user")){
url= data.getString("user_name");
title = data.getString("time");
image = data.getString("thumb");
// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }


concatinate=url+" "+"joined the etable community";
review=null;
status=null;
state=null;
//image1=null;

mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));
}else if(summary.contains("checkin")){
url= data.getString("user_name");
title = data.getString("time");
state=data.getString("subscriber_name");
image = data.getString("thumb");
// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }
concatinate=url+" "+"was spotted at"+" "+state;
review=null;
status=null;
// image1=data.getString("thumb");
mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));
}else if(summary.contains("favorite")){
url= data.getString("user_name");
title = data.getString("time");
state=data.getString("subscriber_name");
image = data.getString("thumb");
// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }
concatinate=url+" "+"favorited"+" "+state;
// image1=data.getString("thumb");
review=null;
status=null;
mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));

}else if(summary.contains("review")){
url= data.getString("user_name");
title = data.getString("time");
state=data.getString("subscriber_name");
image = data.getString("thumb");
// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }
concatinate=url+" "+"wrote a review for"+" "+state;
review=data.getString("review_full");
// image1=data.getString("thumb");
status=null;
mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));

}else if(summary.contains("status")){
url= data.getString("user_name");
title = data.getString("time");
image = data.getString("thumb");
// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }
status=data.getString("full_status");
concatinate=url+" "+"saya\n"+""+status;
// image1=null;
review=null;
state=null;
mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));
}else if(summary.contains("photo_upload")){
url= data.getString("user_name");
title = data.getString("time");
image = data.getString("thumb");
// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }
state=data.getString("subscriber_name");
concatinate=url+" "+"uploaded a photo of"+" "+state;
//image1=data.getString("thumb");
review=null;
status=null;
mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));
}



else{
url= data.getString("user_name");
title = data.getString("time");
image = data.getString("thumb");
/// if(image.contains("false")){
// image="http://www.etablecdn.com//user//uploads//varun.nayyar405@gmail.com//varun.nayyar405.jpg";
// }
status=null;
concatinate=url+" "+"some data missing";
review=null;
state=null;
// image1=null;
mDataSource.add(new Data(state, image, title, summary, addList, url,concatinate,review,status));
}



} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public ArrayList getSource() {
return mDataSource;
}}


code of Adapter.java



public class Adapter extends BaseAdapter {

private static final String TAG = "Adapter";
private Activity mActivity;
public ArrayList mObjects;
// /private final Context context;
Context context;

static class ViewHolder {
static ImageView icon;
TextView title;
TextView name;
TextView review;
DownloadImageTask mTask;
String ab[];
// DownloadImageTask1 mTask1;
// ImageView photo;
}

public Adapter(Activity activity, Context context, ArrayList mObjects) {

this.mActivity = (Activity) activity;
this.context = context;
this.mObjects = mObjects;

}

public void setObjects(ArrayList mObjects) {
this.mObjects = mObjects;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

final Data item = mObjects.get(position);
View rowView = convertView;

if (rowView == null) {
LayoutInflater inflater = mActivity.getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) rowView.findViewById(R.id.image);
// viewHolder.photo = (ImageView) rowView.findViewById(R.id.photo);
viewHolder.title = (TextView) rowView.findViewById(R.id.title);
viewHolder.name = (TextView) rowView.findViewById(R.id.name);

viewHolder.review = (TextView) rowView.findViewById(R.id.status);
rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();

holder.title.setText(item.getmTitle());
holder.name.setText(item.getmConcatinate());

holder.review.setText(item.getmreview());
holder.icon.setBackgroundResource(R.drawable.ic_ab);
// holder.photo.setBackgroundResource(0);
holder.mTask = new DownloadImageTask(item.getmImageUrl(), holder.icon);
if (!holder.mTask.isCancelled()) {
holder.mTask.execute();
}

ViewHolder.icon.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Log.d("Parsing JSON Data", "Before user try122222"); // perform
//Object tag= v.getTag();
Log.d("Parsing JSON Data","abc");
String title = item.getmConcatinate();
String picture=item.getmImageUrl();


Intent intent = new Intent(v.getContext(), LargeView.class);
Log.d("Parsing JSON Data", "Before user try444444");
intent.putExtra("data", title);
Log.d("Parsing JSON Data", "Before user try555555");
intent.putExtra("image3",picture);
Log.d("Parsing JSON Data", "Before user try66666666");
v.getContext().startActivity(intent);
Log.d("Parsing JSON Data", "Before user try7777");
}
});

// holder.mTask1 = new DownloadImageTask1(item.getmImageUrl1(),
// holder.photo);
// if (!holder.mTask1.isCancelled()) {
// holder.mTask1.execute();
// }

return rowView;
}

@Override
public int getCount() {

return (this.mObjects.size());
}

@Override
public Object getItem(int position) {

return (this.mObjects.get(position));
}

@Override
public long getItemId(int position) {

return (position);
}

public AbsListView.RecyclerListener mRecyclerListener = new RecyclerListener() {

public void onMovedToScrapHeap(View view) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
DownloadImageTask imagetask = viewHolder.mTask;
// DownloadImageTask1 imagetask1 = viewHolder.mTask1;
if (imagetask != null) {
imagetask.cancel(true);
}
// if (imagetask1 != null) {
// // imagetask1.cancel(true);
// }
}

};}


Main.java



@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectAll().penaltyLog().build();
StrictMode.setThreadPolicy(policy);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile3);

name = (TextView) findViewById(R.id.textView1);
bmImage2 = (ImageView) findViewById(R.id.imageView1);
address = (TextView) findViewById(R.id.textView2);
gender = (TextView) findViewById(R.id.textView3);
loyalitypoints = (TextView) findViewById(R.id.textView7);
followers = (TextView) findViewById(R.id.textView8);
following = (TextView) findViewById(R.id.textView9);
// list13 = new ArrayList>();
mListView = (ListView) findViewById(android.R.id.list);
mListView.setClickable(true);






// mListView=(ListView)findViewById(R.id.list);
mAdapter = new Adapter(this,c,mSource );
mListView.setAdapter(mAdapter);
Log.w("Parsing JSON Data", "Before Item click");

mListView.setRecyclerListener(mAdapter.mRecyclerListener);


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...