Populate Custom List View in Fragment from API - android

I want to populate custom list view with Ted Talks from TED API, but nothing is shown. I have done all the efforts but nothing does it.
Here's my code
Fragment Class
public class TalksFragment extends ListFragment {
private ListView talkList;
private String TalkUrl = "https://api.ted.com/v1/talks.json?api-key=nm4nq9uyqg558m7z8axbu3be&order=created_at:desc&fields=media_profile_uris,photo_urls,speakers&offset=";
private HandleJSON obj;
private ProgressDialog m_ProgressDialog = null;
private ArrayList<TalksItems> TalkItemsArray = null;
private TalkListAdapter talkListAdapter;
private Runnable viewOrders;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View talks = inflater.inflate(R.layout.talk_list_frag, container, false);
talkList = (ListView) talks.findViewById(android.R.id.list);
return talks;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TalkItemsArray = new ArrayList<>();
this.talkListAdapter = new TalkListAdapter(getActivity(), android.R.layout.simple_list_item_1, TalkItemsArray);
talkList.setAdapter(this.talkListAdapter);
viewOrders = new Runnable(){
#Override
public void run() {
int Offset=0;
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("Talks");
while (obj.parsingComplete);
TalkItemsArray = obj.getTalkItems();
getActivity().runOnUiThread(returnRes);
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(getActivity(),
"Please wait...", "Retrieving data ...", true);
}
private Runnable returnRes = new Runnable() {
#Override
public void run() {
int size = TalkItemsArray.size();
if(TalkItemsArray != null && TalkItemsArray.size() > 0){
talkListAdapter.notifyDataSetChanged();
for(int i=0; i<size; i++)
talkListAdapter.add(TalkItemsArray.get(i));
}
m_ProgressDialog.dismiss();
talkListAdapter.notifyDataSetChanged();
}
};
private class TalkListAdapter extends ArrayAdapter<TalksItems>{
LayoutInflater inflater;
private ArrayList<TalksItems> items;
public TalkListAdapter(Context context, int textViewResourceId, ArrayList<TalksItems> items) {
super(context, textViewResourceId, items);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null)
view = inflater.inflate(R.layout.custom_list_talk, null);
else
view = convertView;
TalksItems TI = items.get(position);
if(TI != null){
TextView talkName = (TextView)view.findViewById(R.id.txtTalkName);
TextView speakerName = (TextView)view.findViewById(R.id.txtSpeakerName);
TextView talkDescription = (TextView)view.findViewById(R.id.txtTalkDescription);
ImageView talkImage = (ImageView)view.findViewById(R.id.talkImage);
talkName.setText(TI.getTalkName());
speakerName.setText(TI.getSpeakerName());
talkDescription.setText(TI.getTalkDescription());
talkImage.setImageURI(TI.getImageURL());
}
return view;
}
}
}
Where custom_list_talk is layout for custom list and talk_list_frag is layout for fragment having a listview with id "#android:id/list"
API is working correctly, data is coming correctly from API.
Problem is that it shows nothing in custom listview(or may be it is not inflating or etc)
EDIT
Foolish mistake. I didn't call getMethod.

As per your code you are using
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("Talks");
It should be
obj = new HandleJSON(TalkUrl + Offset);
obj.fetchJSON("talks");//Note the case of KEY
JSON is Case Sensitive.
**Edit:**
Make sure you are not populating the
TalkItemsArray
In your code.

Related

Drawing a listview through ListItem.add returns always a position = 0

I have an array of 3 elements which I try to draw in a listview. The issue is that it only draws the first entry because getView always returns a position = 0.
Why is that? What do I do wrong?
my main java (fragment):
public class PSGlobalFragment extends Fragment {
List<PSGitem> listPSGitem;
ListView list;
PSGadaptater psgAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.psglobal, container, false);
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String ip;
listPSGitem = new ArrayList<>();
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
listPSGitem.clear();
StoreDevDiscovery store = new StoreDevDiscovery();
// this is where I store the data
int count = store.getMax();
for(int i=0;i<count;i++){
ip = store.getIPDiscovery(i);
PSGitem item = new PSGitem();
item.setIp(ip);
listPSGitem.add(item);
list.setAdapter(psgAdapter);
}
}
and my adapter:
public class PSGadaptater extends BaseAdapter {
private int size = 0;
private List<PSGitem> listIp;
private LayoutInflater layoutInflater;
Context context;
public PSGadaptater(Context c, List<PSGitem> objects) {
context = c;
listIp = objects;
layoutInflater = LayoutInflater.from(context);
}
#Override
public void notifyDataSetChanged() {
size = listIp.size();
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return listIp.size();
}
public Object getItem(int position) {
return listIp.get(position);
}
public long getItemId(int position) {
return position;
}
private class ViewIPHolder {
TextView ip_psg;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewIPHolder viewHolder;
if(convertView == null) {
viewHolder = new ViewIPHolder();
convertView = layoutInflater.inflate(R.layout.listview_item_psg, null);
viewHolder.ip_psg = (TextView) convertView.findViewById(R.id.ipaddr_psg);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewIPHolder) convertView.getTag();
}
viewHolder.ip_psg.setText(listIp.get(position).getIpaddr());
// position always = 0 this is my issue
return convertView;
}
}
and the PSCitem.java:
public class PSGitem {
private String ip1;
public String getIp(){
return ip1;
}
public void setIp(String ip){
ip1 = ip;
}
}
The problem is that you are creating your Adapter from an empty set of items:
listPSGitem = new ArrayList<>();
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
If you wish to add items to the adapter later, you should add the items to the adapter listIp list variable, and then let the adapter know about this change with notifyDataSetChanged() method.
Change your onActivityCreated method like below.
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String ip;
listPSGitem = new ArrayList<>();
listPSGitem.clear();
StoreDevDiscovery store = new StoreDevDiscovery();
// this is where I store the data
int count = store.getMax();
for(int i=0;i<count;i++){
ip = store.getIPDiscovery(i);
PSGitem item = new PSGitem();
item.setIp(ip);
listPSGitem.add(item);
}
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
list.setAdapter(psgAdapter);
}

Why memory keeps increasing when you scroll in a List on Android

I am watching the monitor on Android and I notice that the memory keeps increasing when you scroll down and up.
I am using an Adaper. It's supposed to reuse the views, but it seems that it doesn't work.
As you can see in the images below the memory starts at 9.94 MB and I could increase it at 25.82 MB just by scrolling down and up.
You can see the code that I use.
public class ListMainFragment
extends Fragment
implements LoaderManager.LoaderCallbacks<List<Earthquake>> {
public ListMainFragment() {
// Required empty public constructor
}
ListView listView;
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_mainlist, container, false);
final ArrayList<Earthquake> earthquake =
(ArrayList<Earthquake>) new EarthquakeController(new EarthquakesJson()).getListOfEarthquakes();
listView = (ListView)viewRoot.findViewById(R.id.list_view);
EarthquakeAdapter noteAdapter = new EarthquakeAdapter(getActivity(), earthquake);
listView.setAdapter(noteAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//final String mensaje = notas.get(position).getMag();
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(earthquake.get(0).getMapURL()));
Log.w("ListView", "Se despliega la siguente URL " + earthquake.get(0).getMapURL());
startActivity(intent);
}
});
return viewRoot;
}
}
The Adapter:
public class EarthquakeAdapter extends ArrayAdapter <Earthquake> {
public EarthquakeAdapter(Context context, ArrayList<Earthquake> notas) {
super(context, 0, notas);
}
private static final String LOCATION_SEPARATOR = " of";
View view;
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
/*if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
*/
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
Log.w("AppList", "converView es null");
}
Earthquake note = getItem(position);
Date dateObject = new Date(note.getTimeInMilliseconds());
TextView locationOffset = (TextView)convertView.findViewById(R.id.listItem_tv_locationOffset);
TextView place = (TextView)convertView.findViewById(R.id.listItem_tv_place);
TextView date = (TextView)convertView.findViewById(R.id.lisItem_tv_date);
TextView time = (TextView)convertView.findViewById(R.id.lisItem_tv_time);
TextView mag = (TextView) convertView.findViewById(R.id.listItem_tv_mag);
GradientDrawable magnitudCicle = (GradientDrawable) mag.getBackground();
magnitudCicle.setColor(getMagnitudColor(note.getMag()));
String str_locationOffset, str_place;
if (note.getPlace().contains(LOCATION_SEPARATOR)) {
String [] locations = note.getPlace().split(LOCATION_SEPARATOR);
str_locationOffset = locations[0];
str_place = locations[1];
}
else {
str_place = note.getPlace();
str_locationOffset = getContext().getString(R.string.near_the);
}
mag.setText( new DecimalFormat("0.0").format(note.getMag()));
date.setText(formatDate(dateObject));
place.setText(str_place);
time.setText(formatTime(dateObject));
locationOffset.setText(str_locationOffset);
/* final String mensaje = title.getText().toString();
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),mensaje,Toast.LENGTH_SHORT).show();
}
});*/
return convertView;
}
}
You should use Recycler View for better performance.
From: https://developer.android.com/training/material/lists-cards.html

Why do I have to scroll to refresh my list view?

I'm new to android. I'm trying to get my list view to update, I've tried everything...from calling notifydatasetchanged on the ui thread to just simply recreating my list adapter but for whatever reason when I update, no matter which method I use I have to scroll to see the changes. By this I mean that the data updates (say 13:01 changes to 13:02 in the list), it will update, but to see the change I have to scroll so that 13:01 goes off screen and then move back and it will have updated visually.
Why is this? (I can't post code right now as I'm on my phone but if required I will post later.)
EDIT: Here's the relevant code...sorry it took so long I haven't been at my computer for a couple of days.
Relevant parts of ListFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.match_fragment, container, false);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MatchAdapter adapter = (MatchAdapter) this.getListAdapter();
if(futureMatches)
adapter = new MatchAdapter (this.getActivity(), ((MainActivity)this.getActivity()).getMatches(), futureMatches);
else
adapter = new MatchAdapter (this.getActivity(), ((MainActivity)this.getActivity()).getPastMatches(), futureMatches);
setListAdapter(adapter);
}
public void refresh()
{
MatchAdapter adapter;
//Update array in mainactivity
if(futureMatches)
MainActivity.refreshMatches((MainActivity) getActivity());
else
MainActivity.refreshPastMatches((MainActivity) getActivity());
//put updated entries in the adapter
if(futureMatches)
adapter = new MatchAdapter (getActivity(), ((MainActivity)getActivity()).getMatches(), futureMatches);
else
adapter = new MatchAdapter (getActivity(), ((MainActivity)getActivity()).getPastMatches(), futureMatches);
setListAdapter(adapter);
updateList();
}
public void updateList(){
this.getActivity().runOnUiThread(new Runnable() {
public void run() {
((BaseAdapter) getListAdapter()).notifyDataSetChanged();
getListView().refreshDrawableState();
getListView().invalidate();
}
});
}
public void onViewStateRestored(Bundle savedInstanceState)
{
super.onViewStateRestored(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
refresh();
}
My adapter class:
public class MatchAdapter extends BaseAdapter
{
private final Activity context;
private LayoutInflater inflater;
private boolean time = false;
private boolean futureMatchAdapter = true;
private ArrayList<String> matchList;
public MatchAdapter(Context cont, ArrayList<String> matches, boolean isFutureMatchAdapter)
{
matchList = matches;
futureMatchAdapter = isFutureMatchAdapter;
context = (Activity) cont;
inflater = LayoutInflater.from(context);
}
public int getCount()
{
return MatchAdapter.size();
}
#Override
public Object getItem(int position)
{
return MatchAdapter.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
String curPos = "";
curPos = MatchAdapter.get(position);
//times, future matches and past matches are handled differently
if(curPos.contains("Last updated:"))
time = true;
else
time = false;
if (convertView == null)
{
holder = new ViewHolder();
if(time)
{
convertView = inflater.inflate(R.layout.time_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_time);
}
else
{
if(futureMatchAdapter)
{
convertView = inflater.inflate(R.layout.feed_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_title);
}
else
{
convertView = inflater.inflate(R.layout.past_feed_item, null);
holder.title = (TextView) convertView.findViewById(R.id.item_title_past);
}
}
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
if(futureMatchAdapter)
holder.title.setText(matchList.get(position));
else
{
String matchString = matchList.get(position);
String alwaysVisible = matchString.replace("<", "vs");
alwaysVisible = alwaysVisible.replace(">", "vs");
if(!time)
alwaysVisible = alwaysVisible.substring(0, alwaysVisible.length() - 1);
holder.title.setText(alwaysVisible);
if(matchString.contains(">"))
{
String winner = matchString.substring(0, matchString.indexOf(">")) + "won!";
alwaysVisible = alwaysVisible.concat(winner);
}
else if(matchString.contains("<"))
{
String winner = matchString.substring(matchString.indexOf("<") + 2, matchString.indexOf("\n")) + " won!";
alwaysVisible = alwaysVisible.concat(winner);
}
holder.title.setOnClickListener(new pastMatchesOnclickListener(alwaysVisible)
{
public void onClick(View v)
{
((TextView) v).setText(matchWinner);
}
});
}
return convertView;
}
static class ViewHolder
{
TextView title;
TextView time;
}
}
did you try to update your list using the adapter?
dataadapter.clear();
dataadapter.addAll(allDataResult);
To show and update content in a ListView you should:
Create or find your ListView
Create your ListAdapter
Add your ListAdapter to your ListView
Add data to your ListAdapter
Call notifyDataSetChanged() on your ListAdapter.
Example:
ListView listView = (ListView) findViewById(R.id.listview);
MyListAdapter myListAdapter = new MyListAdapter();
listView.setAdapter(myListAdapter);
myListAdapter.add("Hello");
myListAdapter.add("Hi");
myListAdapter.notifyDataSetChanged();
Note: if you're using a subclass of ArrayAdapter the notifyDataSetChanged() will be called for you when you use the methods add(), addAll(), etc.

listView doesn't get filled?

I'm trying to do an app with listView, which is need to be filled from another class... I'm basicly follow this site: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ but somehow my adapter doesn't work right... And do not fill my listView...
I create a new object from my main class like: fillLeftMenu lmenu = new fillLeftMenu(menu);
I used this line for having started the activity, if it's not wrong:
startActivity(new Intent("com.yahya.training.FILLLEFTMENU"));
On my AndroidManifest.xml, i added this following lines:
<activity
android:name=".fillLeftMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.yahya.training.FILLLEFTMENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And the fillLeftMenu class is:
public class fillLeftMenu extends ListActivity {
private ProgressDialog myProgressDialog = null;
private View menu;
ImageView findLocation;
Spinner cityList;
ListView leftList;
String [] textIndex;
Drawable [] imageIndex;
ArrayList<LeftListItems> Left;
listAdapter lAdapter;
public fillLeftMenu(View Lmenu) {
this.menu = Lmenu;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findLocation = (ImageView) menu.findViewById(R.id.image_findLocation);
leftList = (ListView) menu.findViewById(R.id.list);
// add items to listView
Left = new ArrayList<LeftListItems>();
textIndex = new String[] {"Bugünün Fırsatları", "Son Dakika Bonusları", "Kadın", "Aile", "Çocuk", "Alışveriş", "Şehirden Kaçış", "Kışa Özel"};
imageIndex = new Drawable[] {leftList.getResources().getDrawable(R.drawable.kucukicon_01),leftList.getResources().getDrawable(R.drawable.kucukicon_02),leftList.getResources().getDrawable(R.drawable.kucukicon_03),leftList.getResources().getDrawable(R.drawable.kucukicon_04),leftList.getResources().getDrawable(R.drawable.kucukicon_05),leftList.getResources().getDrawable(R.drawable.kucukicon_06),leftList.getResources().getDrawable(R.drawable.kucukicon_07),leftList.getResources().getDrawable(R.drawable.kucukicon_08)};
lAdapter = new listAdapter(menu.getContext(), R.layout.list_item, Left);
leftList.setAdapter(lAdapter);
Runnable viewOrders = new Runnable(){
public void run() {
getIndex();
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
myProgressDialog = ProgressDialog.show(fillLeftMenu.this,"Please wait...", "Retrieving data ...", true);
leftList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
private Runnable returnRes = new Runnable() {
public void run() {
if(Left != null && Left.size() > 0){
lAdapter.notifyDataSetChanged();
for(int i=0;i<Left.size();i++)
lAdapter.add(Left.get(i));
}
myProgressDialog.dismiss();
lAdapter.notifyDataSetChanged();
}
};
private void getIndex() {
try{
LeftListItems item = new LeftListItems();
for(int i=0; i<textIndex.length; i++) {
item.setText(textIndex[i]);
item.setImage(imageIndex[i]);
Left.add(item);
}
}
catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
private class listAdapter extends ArrayAdapter<LeftListItems> {
private ArrayList<LeftListItems> items;
private Context ctx;
public listAdapter(Context ctx, int textViewResourceId, ArrayList<LeftListItems> items) {
super(ctx, textViewResourceId, items);
this.ctx = ctx;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
// LayoutInflater inflater = LayoutInflater.from(ctx);
// v = inflater.inflate(R.layout.list_item, null);
LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
LeftListItems index = items.get(position);
if(index != null) {
TextView text = (TextView) v.findViewById(R.id.text);
ImageView img = (ImageView) v.findViewById(R.id.icon);
if(text != null)
text.setText(index.getText());
if(img != null)
img.setBackgroundDrawable(index.getImage());
}
return v;
}
}
}
I think you are getting stuck in the loop in returnRes.
for(int i=0;i<Left.size();i++)
lAdapter.add(Left.get(i));
The Left variable is the data set in lAdapter and it has already been updated in the getIndex method. You don't need the loop here at all.
You don't have to create an object for the Activity. You should first learn how Activity works. Activity Fundamental
Take a look at this - Creating an object of Android Activity class
And please try to follow the tutorial exactly.
Here are some other tutorial you can start with. http://www.vogella.de/articles/AndroidListView/article.html
You'll find samples in Android SDK directory.

Tab Application Development

I wish to create a tab application in Android.
I wish to basically have four tabs in my application.
Namely, Home, Compose, Inbox, Sent.
I started with a TabContainer extending TabActivity, is this a correct approach.
Since i will be having MenuItem, which i used be having multiple views for Inbox, as such List of inbox, i sud be able to read, and then delete and all.
I wish to know how do I approach?
Basically i wish to make an application similar to gmail.
Any ideas, or direction might help me
public class Inbox extends ListActivity{
private ProgressDialog progressDialog = null;
private ArrayList<EmailElement> emailElement = null;
private InboxAdapter inboxAdapter;
private CheckBoxWithInboxList newInboxAdapter;
private Runnable beforeFetchingEmail;
private TextView emailFromTextView = null;
private TextView emailSubjectTextView = null;
ListView listView;
private static int mode = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
mode = this.getIntent().getIntExtra("EDIT_MODE", -1);
Log.i("EDITMODE", String.valueOf(mode));
emailElement = new ArrayList<EmailElement>();
if(mode ==0 || mode==-1){
this.inboxAdapter = new InboxAdapter(this, R.layout.inbox_row, emailElement);
setListAdapter(inboxAdapter);
}else{
this.newInboxAdapter = new CheckBoxWithInboxList(this, R.layout.inbox_row, emailElement);
setListAdapter(newInboxAdapter);
}
beforeFetchingEmail = new Runnable() {
#Override
public void run() {
getEmails();
}
};
Thread thread = new Thread(null, beforeFetchingEmail, "MagentoBackground");
thread.start();
progressDialog = ProgressDialog.show(Inbox.this,"Please wait...",
"Retrieving Emails ...", true);
listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parentView, View view,
int position, long id) {
}
});
}
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(emailElement != null && emailElement.size() > 0){
if(mode==0 || mode==-1){
inboxAdapter.notifyDataSetChanged();
for(int i=0;i<emailElement.size();i++){
inboxAdapter.add(emailElement.get(i));
}
progressDialog.dismiss();
inboxAdapter.notifyDataSetChanged();
}else{
newInboxAdapter.notifyDataSetChanged();
for(int i=0;i<emailElement.size();i++){
newInboxAdapter.add(emailElement.get(i));
}
progressDialog.dismiss();
newInboxAdapter.notifyDataSetChanged();
}
}
}
};
private void getEmails(){
try{
emailElement = new ArrayList<EmailElement>();
EmailElement ee0 = new EmailElement();
ee0.setFrom("Robin Thapa");
ee0.setSubject("Urgent Meeting");
emailElement.add(ee0);
EmailElement ee1 = new EmailElement();
ee1.setFrom("Deepak Thapa");
ee1.setSubject("Staff meeting #Sunday");
emailElement.add(ee1);
Thread.sleep(5000);
}catch(Exception ex){
}
runOnUiThread(returnRes);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.inbox_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.inboxEdit:
Intent intent = new Intent(this,EmailClient.class);
intent.putExtra("tabId", 2);
intent.putExtra("EDIT_MODE", 1);
startActivityForResult(intent, Intent.FILL_IN_DATA);
return true;
case R.id.inboxRefresh:
return true;
case R.id.inboxNext:
return true;
case R.id.inboxPrevious:
return true;
}
return false;
}
private class InboxAdapter extends ArrayAdapter<EmailElement>{
private ArrayList<EmailElement> items;
public InboxAdapter(Context context,int textViewResourceId,
ArrayList<EmailElement> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.inbox_row, null);
}
EmailElement o = items.get(position);
if (o != null) {
CheckBox emailCheckBox = (CheckBox)view.findViewById(R.id.inboxCheckBoxId);
emailCheckBox.setVisibility(View.INVISIBLE);
emailFromTextView = (TextView) view.findViewById(R.id.inboxEmailFrom);
emailSubjectTextView = (TextView) view.findViewById(R.id.inboxEmailSubject);
if (emailFromTextView != null){
emailFromTextView.setText("From: "+o.getFrom());
}
if(emailSubjectTextView != null){
emailSubjectTextView.setText("Sub: ["+ o.getSubject()+"]");
}
}
return view;
}
}
private class CheckBoxWithInboxList extends ArrayAdapter<EmailElement>{
private ArrayList<EmailElement> items;
public CheckBoxWithInboxList(Context context,int textViewResourceId,
ArrayList<EmailElement> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.inbox_row, null);
}
EmailElement o = items.get(position);
if (o != null) {
CheckBox emailCheckBox = (CheckBox)view.findViewById(R.id.inboxCheckBoxId);
emailCheckBox.setVisibility(View.VISIBLE);
emailFromTextView = (TextView) view.findViewById(R.id.inboxEmailFrom);
emailSubjectTextView = (TextView) view.findViewById(R.id.inboxEmailSubject);
if (emailFromTextView != null){
emailFromTextView.setText("From: "+o.getFrom());
}
if(emailSubjectTextView != null){
emailSubjectTextView.setText("Sub: ["+ o.getSubject()+"]");
}
}
return view;
}
}
}
For tablayout I recommend : http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
you might want to read up on this Tab Layout | Android Development
it's a good starting point

Categories

Resources