Array Adapter doesn't call GetView - java

I have
public class MainActivity extends FragmentActivity
I wrote Loader class which implements AsyncResponse:
public class Loader implements AsyncResponse {
public Activity contextActivity;
Loader(Activity context) {
this.contextActivity = context;
}
Class "Loader" has method "DrawTabInfo" which calling from AsyncResponse callback.
public void drawTabInfo(String jsonBlob, int tabId)
{
JSONObject dataJsonObj;
PullToRefreshListView list = null;
try {
dataJsonObj = new JSONObject(jsonBlob);
JSONArray jsonData = dataJsonObj.getJSONArray("steals");
ArrayList<JSONObject> data = new ArrayList<JSONObject>();
for (int i=0;i<jsonData.length();i++){
data.add(jsonData.getJSONObject(i));
}
Adapter adapter = new Adapter(contextActivity, data);
LayoutInflater inflater = LayoutInflater.from(contextActivity);
if (tabId == 0) {
View view = inflater.inflate(R.layout.listviewlayout, null, false);
list = (PullToRefreshListView) view.findViewById(R.id.listView);
}
if (tabId == 1) {
View view = inflater.inflate(R.layout.listviewlayout2, null, false);
list = (PullToRefreshListView) view.findViewById(R.id.listView2);
}
list.setAdapter(adapter);
adapter.setNotifyOnChange(true);
Log.d("COUNT") shows - "4", what means array is built fine.
public class Adapter extends ArrayAdapter<JSONObject> {
private final Activity context;
private final ArrayList<JSONObject> profiles;
public String header;
public String preText;
public String imageURL;
static class ViewHolder {
public TextView header;
public ImageView image;
public TextView preText;
public LinearLayout linearItemLayout;
}
public Adapter(Activity context, ArrayList<JSONObject> array) {
super(context, R.layout.tab1_list_layout, array);
Log.d("ADAPTER", "SETTING ADAPTER");
this.context = context;
this.profiles = array;
Log.d("COUNT", "" + profiles.size());
}
#Override
public int getCount()
{
return profiles.size();
}
My getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
Log.i("GetView Log", "Adapters GetView hasbeen called");// thats never calling
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.tab1_list_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.linearItemLayout = (LinearLayout) rowView.findViewById(R.id.linearItemLayout);
viewHolder.header = (TextView) rowView.findViewById(R.id.itemHeader);
viewHolder.image = (ImageView) rowView.findViewById(R.id.itemPreImage);
viewHolder.preText = (TextView) rowView.findViewById(R.id.itemPreText);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
JSONObject item = null;
try {
item = profiles.get(position);
header = item.getString("header");
preText = item.getString("previewText");
imageURL = item.getString("previewImageUrl");
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("ADAPTER LOG", header);
holder.header.setText(header);
holder.preText.setText(preText);
int headerHeight = (int) Math.round(header.length() * 1.5);
holder.linearItemLayout.setMinimumHeight(40 + headerHeight + preText.length());
Picasso.with(context).setIndicatorsEnabled(true);
Picasso.with(context).load(imageURL).resize(140,100).into(holder.image);
return rowView;
}
In MainActivity i calling, where "this" = MainActivity;
Loader loader = new Loader(this);
loader.loadTabInfo(0);
Method getView never calling. What I'am doing wrong?
UPD:
Solved:
public void drawTabInfo(String jsonBlob, int tabId)
{
JSONObject dataJsonObj;
PullToRefreshListView list = null;
try {
dataJsonObj = new JSONObject(jsonBlob);
JSONArray jsonData = dataJsonObj.getJSONArray("steals");
ArrayList<JSONObject> data = new ArrayList<JSONObject>();
for (int i=0;i<jsonData.length();i++){
data.add(jsonData.getJSONObject(i));
}
Adapter adapter = new Adapter(contextActivity, data);
//LayoutInflater inflater = LayoutInflater.from(contextActivity);
if (tabId == 0) {
//View view = inflater.inflate(R.layout.listviewlayout, null, false);
list = (PullToRefreshListView) contextActivity.findViewById(R.id.listView);

Your code is fine .. just you need a little bit change in get view method
add a return statement.. like return rowView;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
Log.d("GETVIEaW", "GETVIEW");
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.tab1_list_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.linearItemLayout = (LinearLayout) rowView.findViewById(R.id.linearItemLayout);
viewHolder.header = (TextView) rowView.findViewById(R.id.itemHeader);
viewHolder.image = (ImageView) rowView.findViewById(R.id.itemPreImage);
viewHolder.preText = (TextView) rowView.findViewById(R.id.itemPreText);
rowView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.preText.setText("Type your name");
return rowView;
}

Are you sure that Log.d() is not called? You may want to change logging scope, usually default scope is info and it doesn't show debug messages. You can try to use Log.i() or change scope.

Try posting your adapter.setNotifyOnChange(true); to handler. Follow this link

Related

Custom List view value fluctuated when new value added?

I am trying do the chatting application. When send a message that should be in right side , received message should be in left side. I have done everything in good manner. when I add new value in list view , previous value has fluctuated. Please can any one solve my problem ? I have attached my source code.
Main Activity :
public class MainActivity extends AppCompatActivity {
ArrayList<ChatData> data = new ArrayList<ChatData>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.list_item);
final ChatAdaptor chatAdaptor = new ChatAdaptor(this, getDetails());
listView.setAdapter(chatAdaptor);
listView.setDivider(null);
ImageView imageView = (ImageView) findViewById(R.id.submit);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText objText = (EditText) findViewById(R.id.text_msg);
chatAdaptor.add(new ChatData(objText.getText().toString(), false));
listView.setSelection(chatAdaptor.getCount() - 1);
objText.setText(null);
// chatAdaptor.notifyDataSetChanged();
}
});
}
public ArrayList<ChatData> getDetails() {
data.add(new ChatData("Hi! Agent", false));
data.add(new ChatData("Hi! Agent ,Are you there ", false));
data.add(new ChatData("Yeah tell me how can I help you", true));
data.add(new ChatData("I am getting wrong balance", false));
return data;
}
}
Custom adapter:
public class ChatAdaptor extends ArrayAdapter<ChatData> {
private Activity activity;
String TAG = "bks";
private List<ChatData> originalData = null;
public ChatAdaptor(Activity activity, ArrayList<ChatData> objects) {
super(activity, 0, objects);
this.activity = activity;
this.originalData = objects;
}
#Override
//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// convertView = mInflater.inflate(R.layout.search_data, true,null);
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.test, null, false);
if (originalData.get(position).isAgentMsg())
convertView = inflater.inflate(R.layout.show_chat_agent, null, true);
else
convertView = inflater.inflate(R.layout.show_chat_client, null, true);
// Creates a ViewHolder and store references to the three views
// we want to bind data to.
holder = new ViewHolder();
holder.objMsg = ( TextView ) convertView.findViewById(R.id.msg) ;
// Bind the data efficiently with the holder.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
holder = (ViewHolder) convertView.getTag();
}
holder.objMsg.setText(originalData.get(position).getMsg());
return convertView;
}
static class ViewHolder {
TextView objMsg;
/** TextView objClientText, objAgentText;
ImageView objClientImage1, objClientImage2;
ImageView objAgentImage1, objAgentImage2;
*/
}
}
Thanks
Kalanidhi M
Just change this line in your getView method
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.test, parent, true);
if (originalData.get(position).isAgentMsg())
convertView = inflater.inflate(R.layout.show_chat_agent, parent, true);
else
convertView = inflater.inflate(R.layout.show_chat_client, parent, true);

ListView data repeating

Data from parsed XML is fed into an ArrayList itemList. Results meeting the criteria are then added to filteredList, which in turn is bound to the adapter to be displayed in my ListView. However, the data being added to filteredList is being repeated (twice) and I do not understand why. Could anyone perhaps point it out for me? I've been staring at it for a while now lol.
Here is the function from MainActivity.java
private void FilterList() {
ListView lv = (ListView) findViewById(R.id.info); //locate listview
for (int i = 0; i < itemList.size(); i++) //cycle arraylist
{
try {
//convert startdate, endDate
Date cDate = dateFormat.parse(currDate); //get/convert selected date
Date stDate = dateFormat.parse(itemList.get(i).startDate);
Date enDate = dateFormat.parse(itemList.get(i).endDate);
//compare, if active true then add
if ((cDate.after(stDate)) && (cDate.before(enDate))){
//add itemList element to filterList
filteredList.add(itemList.get(i));
Log.d("DEBUG CHECKME", "added to filteredList: ");
}
} catch (java.text.ParseException e) {
e.printStackTrace();
}
}
itemAdapter = new PostItemAdapter(mcontext, R.layout.postitem, filteredList); //pass results into adapter
lv.setAdapter(itemAdapter); //bind listview & adapter
Log.d("DEBUG CHECKME", "lv.setAdapter");
}
And here is my adapter class:
public class PostItemAdapter extends ArrayAdapter<Item>
{
private LayoutInflater inflater;
private ArrayList<Item> datas;
public PostItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects)
{
super(context, textViewResourceId, objects);
inflater = ((Activity) context).getLayoutInflater();
datas = objects;
}
//class to hold view object references
static class ViewHolder
{
TextView itemTitleView;
ImageView itemThumbView;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
//viewHolder.itemDateView = (TextView) convertView.findViewById(R.id.itemDateLabel);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (datas.get(position).itemThumbUrl == null)
{
viewHolder.itemThumbView.setImageResource(R.drawable.test);
}
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
return convertView;
}
}
try this i hope it will help you
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.postitem, parent, false);
} else {
row = convertView;
}
filteredList datas= rowItems.get(position);
try {
TextView itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
itemTitleView.setText(datas.itemTitle);
} catch (Exception e) {
}
try {
ImageView itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
itemThumbView.setImageResource(R.drawable.test);
} catch (Exception e) {
}
});
return row;
}

Android: How to use getChildFragmentManager() in an ArrayAdapter class?

How can i use the method getChildFragmentManager() in a custom adapter that extends ArrayAdapter<> ?
Here's my code if you need to see it:
MyAdapter.java:
public class MyAdapter extends ArrayAdapter<Content> {
Context context;
List<Content> myList;
public static final String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";
public MyAdapter(Context context, int resource, List<Content> objects) {
super(context, resource, objects);
this.context = context;
this.myList = objects;
}
#Override
public int getCount() {
if (myList != null)
return myList.size();
return 0;
}
#Override
public Content getItem(int position) {
if (myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if (myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
holder.contId = (TextView) convertView.findViewById(R.id.content_id);
holder.contType = (TextView) convertView.findViewById(R.id.type);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.txtTeaser = (TextView) convertView.findViewById(R.id.teaser);
holder.imageView = (ImageView) convertView.findViewById(R.id.cover_photo);
holder.youTubeVideo = (FrameLayout) convertView.findViewById(R.id.youTube_frame);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
Content content = getItem(position);
holder.contId.setText(content.getId());
holder.contType.setText(content.getType());
holder.txtTitle.setText(content.getTitle());
holder.txtTeaser.setText(content.getTeaser());
Picasso.with(context).load(Uri.parse(content.getImageUrl())).into(holder.imageView);
if(content.getCoverVideo() != null && !content.getCoverVideo().equals("")){
String cover_video = content.getCoverVideo();
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(cover_video);
if(matcher.find()){
cover_video = matcher.group();
}
Bundle youTubeBundle = new Bundle();
youTubeBundle.putString("link", cover_video);
FragmentManager manager = getChildFragmentManager(); // <------ Can't resolve method
FragmentTransaction transaction = manager.beginTransaction();
ArticleYouTubeFragment youtubeFragment = new ArticleYouTubeFragment();
youtubeFragment.setArguments(youTubeBundle);
transaction.replace(R.id.list_youTube_frame, youtubeFragment);
transaction.commit();
}
return convertView;
}
private class Holder {
TextView contId;
TextView contType;
TextView txtTitle;
TextView txtTeaser;
ImageView imageView;
FrameLayout youTubeVideo;
}
}
Thanks in advance.

Why I am not able to receive data in my adapter

This code is in my MainActivity.java and I am able to print title in log cat from here
feedsDataArray = feedsData.toArray(feedsDataArray);
for(int i = 0 ; i < feedsDataArray.length ; i++){
Log.v("Array",feedsDataArray[i].getTitle());
}
listview = (ListView) findViewById(R.id.list);
feedsAdapter = new FeedsAdapter(NewsFeedsActivity.this, R.layout.feeds_list,
feedsDataArray);
listview.setAdapter(feedsAdapter);
But when I am passing it to adapter then I am not able to get data there
public class FeedsAdapter extends ArrayAdapter<FeedsItems>{
Context context;
int id;
FeedsItems [] dataArray;
public FeedsAdapter(Context context, int id,FeedsItems [] dataArray) {
super(context,id);
this.context = context;
this.id = id;
this.dataArray = dataArray;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
FeedsHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(id, parent, false);
holder = new FeedsHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.feeds_image);
holder.txtTitle = (TextView) row.findViewById(R.id.feeds_title);
row.setTag(holder);
}
else{
holder = (FeedsHolder)row.getTag();
}
//holder.imgIcon.setImageResource();
holder.txtTitle.setText(data.get(position).getTitle());
Log.v("Title"+position,dataArray[position].getTitle());
return row;
}
static class FeedsHolder{
ImageView imgIcon;
TextView txtTitle;
}
}
Any suggestion or help will be appreciated. Thnx
you have to override getCount and let it returns dataArray.lenght or pass dataArray to the ArrayAdapter's super constructor.
Take a look the the documentation
just add this to your adapter class...
#Override
public int getCount() {
return dataArray.length;
}

Android Json array object populate textview

I have http json array object to my FeedItem Serializable class, and i use it to populate listview. Everything works fine, but on another fragment i have a textview which i want to be populate with one string from my downloaded json array i and tried many ways and still can't do it.
this is the void who is starting after json download
public void updateList() {
**TextView infoz = (TextView) getView().findViewById(R.id.infoz);**
infoz.setText(getString(feedList))
feedListView= (ListView)getActivity().findViewById(R.id.custom_list);
feedListView.setAdapter(new CustomListAdapter(getActivity(), feedList));
feedListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = feedListView.getItemAtPosition(position);
FeedItem Data = (FeedItem) o;
Intent intent = new Intent(getActivity(), FeedDetailsActivity.class);
intent.putExtra("feed", Data);
startActivity(intent);
}
});
}
feedList is an array with downloaded json. How should i use on of the strings and set it in textview?
Thanks
Custom List Adapter
public class CustomListAdapter extends BaseAdapter {
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.title);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.headlineView.setText(newsItem.getTitle());
holder.reportedDateView.setText(newsItem.getDate());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
}
return convertView;
}
static class ViewHolder {
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
}
}
json
public void parseJson(JSONObject json) {
try {
// parsing json object
if (json.getString("status").equalsIgnoreCase("ok")) {
JSONArray posts = json.getJSONArray("posts");
feedList = new ArrayList<FeedItem>();
for (int i = 0; i < posts.length(); i++) {
JSONObject post = (JSONObject) posts.getJSONObject(i);
FeedItem item = new FeedItem();
item.setTitle(post.getString("title"));
item.setDate(post.getString("description"));
item.setId(post.getString("id"));
item.setUrl(post.getString("url"));
item.setContent(post.getString("description"));
JSONArray attachments = post.getJSONArray("attachments");
;
if (null != attachments && attachments.length() > 0) {
JSONObject attachment = attachments.getJSONObject(0);
if (attachment != null)
item.setAttachmentUrl(attachment.getString("url"));
}
feedList.add(item);
}
}

Categories

Resources