Refreshing listview to update the data? - android

I've a listview with some static data. When the refresh activity is done, some data need to be updated in listview. The problem is once the refresh gets completed, the new items are not updated.
MainActivity.class
public class MainActivity extends ActionBarActivity {
SwipeRefreshLayout swipeLayout;
String[] players = {"Dravid","Ganguly","Sachin","Irfan pathan","Balaji","Bhuvi","Praveen"};
ListView listView;
ArrayAdapter<String> adapter = null;
ArrayList al = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
for (int i =0; i<players.length; i++)
{
Bean b = new Bean();
b.setName(players[i]);
al.add(b);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,players);
listView.setAdapter(adapter);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeLayout.setRefreshing(true);
new updateItems().execute();
}
});
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
class updateItems extends AsyncTask<Void, Void, Void>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
String[] newitems = {"Ishant sharma","Ramesh power","Kaif"};
// adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,newitems);
for (int j=0;j<newitems.length;j++)
{
Bean bb = new Bean();
bb.setName(newitems[j]);
al.add(bb);
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
listView.invalidateViews();
}
});
swipeLayout.setRefreshing(false);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Bean.class
public class Bean {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Where i am making mistake?

This is the adapter that you're setting initially :
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,players);
Inside your doInBackground() method, you update the a1 ArrayList but your players Array on which the adapter depends remains the same hence no changes are reflected in the listview even when you do adapter.notifyDataSetChanged().
Secondly, the onPostExecute method runs on the ui thread so you do not need to do that explicitly.

Related

Android - RecyclerView NullPointerException getItemCount?

I have a recyclerView that it get me crash :
Here is my StartActivity :
public class StartActivity extends AppCompatActivity {
TextView txtTest;
private ProgressDialog pDialog;
// These tags will be used to cancel the requests
private String tag_json_obj = "jobj_req", tag_json_arry = "jarray_req";
private RecyclerView.Adapter mAdapter;
RecyclerView UserCode_Recycler;
private LinearLayoutManager mLayoutManager;
List<Marketing_Code> userCodeList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
txtTest = (TextView) findViewById(R.id.txtTest);
UserCode_Recycler = (RecyclerView) findViewById(R.id.UserCode_Recycler);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (fab != null) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
makeJsonArryReq();
userCodeList = new ArrayList<>();
// create an Object for Adapter
mAdapter = new UserCodeList_Adapter(userCodeList, StartActivity.this);
// set the adapter object to the Recyclerview
UserCode_Recycler.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
UserCode_Recycler.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
// use a linear layout manager
UserCode_Recycler.setLayoutManager(mLayoutManager);
}
private void showProgressDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideProgressDialog() {
if (pDialog.isShowing())
pDialog.hide();
}
/**
* Making json array request
*/
private void makeJsonArryReq() {
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest(Const.Marketing_List,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("MYData", response.toString());
userCodeList = MarketingCode_JSONParser.parseFeed(response.toString());
/* // create an Object for Adapter
mAdapter = new UserCodeList_Adapter(userCodeList, StartActivity.this);
// set the adapter object to the Recyclerview
Search_Recycler.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
//txtTest.setText(response.toString());*/
mAdapter.notifyDataSetChanged();
hideProgressDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Custom Log Error", "Error: " + error.getMessage());
hideProgressDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_arry);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And my adapter :
public class UserCodeList_Adapter extends RecyclerView.Adapter<UserCodeList_Adapter.ViewHolder> {
private List<Marketing_Code> ucList;
public static Activity activity;
public UserCodeList_Adapter(List<Marketing_Code> userCodeList, Activity activity) {
this.ucList = userCodeList;
this.activity = activity;
}
#Override
public UserCodeList_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listmarketing_cardview, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(UserCodeList_Adapter.ViewHolder viewHolder, int position) {
String userCode =String.valueOf(ucList.get(position).getMarketCode());
viewHolder.txtUserID.setText(userCode);
}
#Override
public int getItemCount() {
return ucList.size();
//return ucList == null ? 0 : ucList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtUserID;
public Marketing_Code items;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtUserID = (TextView) itemLayoutView.findViewById(R.id.txtUserID);
// Onclick event for the row to show the data in toast
itemLayoutView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
}
You haven't initialized your userCodeList.
Seeing your code, you based your adapter to that list while it is not yet initialized. Consequently, when the adapter tried to learn how much item your list have it throws a NullPointerException.
Change your declaration of userCodeList to one like below:
List<Marketing_Code> userCodeList = new ArrayList<>();
Seeing your updated question, you seem to base your data from a JSON response. If that's the case, what you're currently doing is almost correct.
Observe this slightly modified snippet of your code:
private void makeJsonArryReq() {
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest(Const.Marketing_List,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("MYData", response.toString());
/* YOUR OLD CODE -> */ // userCodeList = MarketingCode_JSONParser.parseFeed(response.toString());
/* HOW IT SHOULD'VE BEEN */ userCodeList.addAll(MarketingCode_JSONParser.parseFeed(response.toString()));
mAdapter.notifyDataSetChanged();
hideProgressDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Custom Log Error", "Error: " + error.getMessage());
hideProgressDialog();
}
});
// ...
Look at the commented part of the above snippet. Doing so should solve your problem and make the data visible on screen.
Two ways to handle this:
You need to check if the list is NULL before requesting to check its size
public int getItemCount () {
if(ucList == null)
return 0;
return ucList.size();
}
Alternatively, and this is the recommended approach to this matter, is to initialise it to an empty list at the start or make sure that it is always initialised before it is accessed by the associated adapter.
List<Marketing_Code> userCodeList = new ArrayList<>();
Hope this helps.
I think you need to use replaceAll function for the list ,instead of directly equating.
Here
userCodeList = MarketingCode_JSONParser.parseFeed(response.toString());
Try checking if userCodeList contains any data before initialzing the adapter.
Please do the following and try
// create an Object for Adapter
userCodeList = new ArrayList<>();
mAdapter = new UserCodeList_Adapter(userCodeList, StartActivity.this);

android initialize two times in two different activities

Not able to initialize parse two times in two another activity to call data from two classes of parse and put them in different list views. at second time when opening contact activity by action item then the app stops
Main Activity.java
public class MainActivity extends ActionBarActivity {
private CountryAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, "0FgKGokshcBPQSpY**********", "f1hZ9W4c***********");
ParseObject.registerSubclass(Country.class);
mAdapter = new CountryAdapter(this, new ArrayList<Country>());
ListView mListView = (ListView) findViewById(R.id.country_list);
mListView.setAdapter(mAdapter);
updateData();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_contact) {
Intent i = new Intent(this, ContactActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
public void updateData() {
ParseQuery<Country> query = ParseQuery.getQuery(Country.class);
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
query.findInBackground(new FindCallback<Country>() {
#Override
public void done(List<Country> countrys, com.parse.ParseException e) {
if (countrys != null) {
mAdapter.clear();
for (int i = 0; i < countrys.size(); i++) {
mAdapter.add(countrys.get(i));
}
}
}
});
}
}
ContactActivity.java
public class ContactActivity extends ActionBarActivity {
private ContactAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
Parse.initialize(this, "0FgKGoksh********************", "f1hZ9W4cKO2Ag*******************");
ParseObject.registerSubclass(Contact.class);
mAdapter = new ContactAdapter(this, new ArrayList<Contact>());
ListView mListView = (ListView) findViewById(R.id.contact_list);
mListView.setAdapter(mAdapter);
updateData();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_contact, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public void updateData() {
ParseQuery<Contact> query = ParseQuery.getQuery(Contact.class);
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
query.findInBackground(new FindCallback<Contact>() {
#Override
public void done(List<Contact> contact, com.parse.ParseException e) {
if (contact != null) {
mAdapter.clear();
for (int i = 0; i < contact.size(); i++) {
mAdapter.add(contact.get(i));
}
}
}
});
}
}
You should initialize parse in a class which extends Application class like this
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "xxxxxxxxxxxx", "xxxxxxxxx");
}
}
and put application class name in manifest file like
<application
android:name=".MyApplication" />

Asynctask onPostExecutive method assigning a value to instance variable but it is not showing that in mainActivity class.

I have a instance variable(art) of main activity class getting assigned with a value in onPostExecution method.Log statements prints the value of art in onPostExecute method.
But the value of art is null after the onPostExecute method.
public class MainActivity extends ActionBarActivity implements OnItemClickListener {
ListView listView1;
String[] dummyData = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday","sunday","monday","tuesday",
"wednesday","thursday","friday","saturday","sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
ArrayAdapter<String> adapter ;
ArrayList<String> summery = new ArrayList<String>(4);
ArrayList<String> links = new ArrayList<String>(4);
Elements art;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String stringUrl = "https://techcards.wordpress.com";
Fetch fetch = new Fetch();
fetch.execute(stringUrl);
try{
for (int i =0;i<art.size() ;i++ ) {
Log.v("articles after post executive",art.get(i).toString());
links.add(i,art.get(i).getElementsByTag("a").toString());
Log.v("links",art.toString());
summery.add(i,art.get(i).getElementsByTag("p").text());
}
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("art is null");
}
listView1 = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,R.layout.single_row,R.id.textView2, links);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
TextView t = (TextView) view.findViewById(R.id.textView2);
String text =(String) t.getText();
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();//this makeText is a static method
}
// Fetch AsyncTask
private class Fetch extends AsyncTask<String, Void, Elements> {
#Override
protected Elements doInBackground(String... params) {
Elements articles = null;
try {
// Connect to the web site
Document doc = Jsoup.connect(params[0]).get();
Element main =doc.getElementById("content").getElementById("primary").
getElementById("main");
articles = main.getElementsByClass("entry-summary");
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO exception");
}
return articles;
}
protected void onPostExecute(Elements result) {
art = result;
for (int i =0;i<art.size() ;i++ ) {
Log.v("links in post executive",art.get(i).getElementsByTag("a").toString());
Log.v("summery in post executive",art.get(i).getElementsByTag("p").text());
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
art is giving null pointer exception when i'm trying to use it to update links and summery.
onPostExecute method run when doInBackground execution complete. so add data in adapter in onPostExecute instead of just after starting AsyncTask :
#Override
protected void onPostExecute(Elements result) {
super.onPostExecute(result);
// 1. Add data in summery from result
// 2. Pass summery to Adapter constructor
adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.single_row,
R.id.textView2, links);
listView1.setAdapter(adapter);
}
AsyncTask works asynchronously. Those lines after:
fetch.execute(stringUrl);
doesn't work after onPostExecute but it works simultaneously (or so) with the code inside AsyncTask. That's why AsyncTask is called AsyncTask.
And that's why your art variable is null since onPostExecute doesn't get called yet.

Android AsyncTask Activity Crash RSS Reader

I'm trying to build an RSS reader and put the rss feed fetch as ansyctask,
that returns a feed in list view, or returns a text view saying "no internet connection"
but the app still crashes, I don't know what's wrong, can you help please.
here is the code:
package rss;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import com.enporan.polytechoran.R;
public class RSSActivity extends ActionBarActivity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
rssfeedget alpha = new rssfeedget();
alpha.execute();
}
private class rssfeedget extends AsyncTask<String, Void, FeedSource> {
protected void onPreExecute() {
}
#Override
protected FeedSource doInBackground(String... params) {
FeedSource f = new HttpFeedSource();
if(f!=null)
return f;
else {
return null;
}
}
#Override
protected void onPostExecute(FeedSource result){
ListView rssItemList = (ListView) findViewById(R.id.rssListview);
rssItemList.setVerticalFadingEdgeEnabled(true);
if(doInBackground()==null){
TextView tv= (TextView) findViewById(R.id.textView2);
tv.setText("No internet Connection...");
}
else{
RSSItemAdapter adapter = new RSSItemAdapter(getApplicationContext(), R.layout.rssitem, doInBackground().getFeed());
rssItemList.setAdapter(adapter);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_news, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As #coelho pointed out, the FeedSource.getFeed() shouldn't be executed in the UI thread. You must now that the onPreExecute and onPostExecute methods are executed inside the UI thread, while the doInBackground method isn't.
Here's what you can do: in your AsyncTask class, add a private member: private List<RSSItem> result; (replace RSSItem here by the type of the collection returned by getFeed).
Then, update doInBackground:
FeedSource f = new HttpFeedSource();
if (f != null)
return f;
else {
this.result = f.getFeed(); // Execute getFeed in doInBackground
return null;
}
Then, in the onPostExecute method, you'll be able to use this private member as this:
RSSItemAdapter adapter = new RSSItemAdapter(getApplicationContext(), R.layout.rssitem, this.result);
Here is the code:
private class rssfeedget extends AsyncTask<String, Void, List<RSSItem>> {
private List<RSSItem> result;
protected void onPreExecute() {
}
#Override
protected List<RSSItem> doInBackground(String... params) {
FeedSource f = new HttpFeedSource();
if(f.getFeed()==null)
return null;
else {
this.result = f.getFeed(); // Execute getFeed in doInBackground
return result;
}
}
#Override
protected void onPostExecute(List<RSSItem> result){
if(doInBackground()==null){
TextView tv= (TextView) findViewById(R.id.textView2);
tv.setText("No internet Connection...");
}
else{
ListView rssItemList = (ListView) findViewById(R.id.rssListview);
rssItemList.setVerticalFadingEdgeEnabled(true);
RSSItemAdapter adapter = new RSSItemAdapter(getApplicationContext(), R.layout.rssitem, this.result);
rssItemList.setAdapter(adapter);
}
}
}

Load data from json to list view

I have an application which load data from json to android as below
public class MainActivity extends Activity {
private final String URL_SERVICE = "http://92.253.101.239:81/sudandoctors/api.aspx?op=5&GovernorateId=&SpecializationId=&DoctorName=&LastDoctorId=0";
private final String URL_IMAGE_BASE = "http://92.253.101.239:81/sudandoctors/UploadedFiles/";
TextView tv;
DoctorInfo doctorObj = new DoctorInfo();
ArrayList<DoctorInfo> doctorsInfoList = new ArrayList<DoctorInfo>();
ListView lv = (ListView)findViewById(R.id.mylist);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncHttpClient client = new AsyncHttpClient();
client.get(URL_SERVICE, new AsyncHttpResponseHandler() {
#Override
public void onStart() {
super.onStart();
// show loading bar
Log.d("onStart", "onStart");
}
#Override
public void onSuccess(String response) {
super.onSuccess(response);
Log.d("onSuccess", "onSuccess");
DoctorsModel doctorModel = new DoctorsModel();
ArrayList<DoctorInfo> doctorsInfoList = new ArrayList<DoctorInfo>();
try {
// convert response to JSON
JSONObject json = new JSONObject(response);
// get JSON Items
doctorModel.setDoctorCnt(json.getInt("DoctorsCount"));
doctorModel.setOp(json.getString("op"));
JSONArray doctorsArray = json.getJSONArray("Doctors");
Log.d("dr arrat", doctorsArray.toString());
for (int i = 0; i < doctorsArray.length(); i++) {
JSONObject doctorJSON = doctorsArray.getJSONObject(i);
doctorObj .setId(doctorJSON.getString("Id"));
Log.d("id", doctorJSON.getString("Id"));
doctorObj.setName(doctorJSON.getString("DoctorName"));
doctorObj.setGovernorateId(doctorJSON.getString("GovernorateId"));
doctorObj.setGovernorateName(doctorJSON.getString("GovernorateName"));
doctorObj.setSpecializationName(doctorJSON.getString("SpecializationName"));
doctorObj.setHospitalId(doctorJSON.getString("HospitalId"));
doctorObj.setHospitalName(doctorJSON.getString("HospitalName"));
doctorObj.setImageName(URL_IMAGE_BASE + doctorJSON.getString("ImageName"));
doctorObj.setMobile(doctorJSON.getString("Mobile"));
doctorObj.setSpecializationId(doctorJSON.getString("SpecializationId"));
doctorObj.setWeekendDays(doctorJSON.getString("WeekendDays"));
doctorObj.setWorkingDays(doctorJSON.getString("WorkingDays"));
doctorObj.setWorkingTime(doctorJSON.getString("WorkingTime"));
doctorsInfoList.add(doctorObj);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Throwable e, String message) {
super.onFailure(e, message);
// show error message
}
#Override
public void onFinish() {
super.onFinish();
// remove loading bar
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.activity_list_item);
// Assign adapter to ListView
lv.setAdapter(adapter);
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And I want to show the data in list view, how can I do that ?
Use an Adapter - http://developer.android.com/reference/android/widget/Adapter.html
Create an adapter with your data, then set that adapter as the ListView's adapter.

Categories

Resources