Android ArrayList Adapter doesn't show the ListView - android

I have an ArrayList to represent in a ListView by an Adapter:
private ArrayList <Contact> listContacts = new ArrayList <Contact> ();
This is the (simple) Contact Class:
public class Contact {
String pic;
String name;
String surname1;
String surname2;
String phonenumber;
}
And this is the Adapter Class:
public class ContactsAdapter extends ArrayAdapter<Contact> {
private static class ViewHolder {
ImageView pic;
TextView name;
TextView surname1;
TextView surname2;
TextView phonenumber;
}
Context context;
public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.layout_contact, parent, false);
holder = new ViewHolder();
holder.pic= (ImageView) convertView.findViewById(R.id.pic);
holder.name= (TextView) convertView.findViewById(name);
holder.surname1= (TextView) convertView.findViewById(R.id.surname1);
holder.surname2= (TextView) convertView.findViewById(R.id.surname2);
holder.phonenumber= (TextView) convertView.findViewById(R.id.phonenumber);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Contact item = getItem(position);
if (item!= null) {
int idImage = context.getResources().getIdentifier(item.pic, "drawable", context.getPackageName());
holder.pic.setImageResource(idImage);
holder.name.setText(item.name);
holder.surname1.setText(item.surname1);
holder.surname2.setText(item.surname2);
holder.phonenumber.setText(item.phonenumber);
}
return convertView;
}
}
The MainActivity is like this:
public class MainActivity extends ListActivity {
private ArrayList <Contact> listContacts = new ArrayList <Contact> ();
ContactsAdapter contactsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean result = loadListFromFiles();
if (result) {
loadContacts();
}
}
private void loadContacts() {
contactsAdapter = new ContactsAdapter(getApplicationContext(), R.layout.activity_main, listContacts);
setListAdapter(contactsAdapter);
}
The loadListFromFiles code:
private boolean loadListFiles(){
boolean result = false;
Context context = getApplicationContext();
File path = context.getFilesDir();
File[] files = path.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
result = loadFile(files[i].getName());
}
return resultado;
}
private boolean loadFile(String fileName) {
String[] arrayContact = new String[4];
try
{
BufferedReader fin =
new BufferedReader(
new InputStreamReader(
openFileInput(fileName)));
int i = 0;
String line= "";
while ((line= fin.readLine()) != null) {
arrayContact[i] = linea;
i++;
}
fin.close();
Contact contact = new Contact();
contact.pic = arrayContact[3];
contact.name= arrayContact[0];
contact.surname1 = arrayContact[1];
contact.surname2 = arrayContact[2];
contact.phonenumber= arrayContact[3];
listContacts.add(contact);
return true;
}
catch (Exception ex)
{
Log.e("Files", "Error to read file by memory");
return false;
}
}
The ArrayList is right, but the layout don't show anything.

Override getCount() and getItem() methods to return count and contact item.
#Override
public int getCount() {
if(items == null)
return 0;
return items.size();
}
#Override
public Contact getItem(int i) {
return items.get(i);
}
Also, create one variable items in Adapter class and assign it to parameter passed to adapter constructor.
ArrayList<Contact> items;
public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
}

Related

android ListView is empty Logcat shows nothing

i am trying to display json data in listview but listview is empty logcat shows no error and app also don't get crashed i am unable to find the error here is the method that adds json data to the adapter
public void setTextToTextView(JSONArray jsonArray) {
contactAdapter = new ContactAdapter(this,R.layout.row_layout);
listView.setAdapter(contactAdapter);
try {
int count = 0;
String stop;
while(count < jsonArray.length())
{
JSONObject JO = jsonArray.getJSONObject(count);
stop = JO.getString("stop");
Contacts contacts = new Contacts(stop);
contactAdapter.add(contacts);
count++;
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
here is the Adapter class
public class ContactAdapter extends ArrayAdapter{
List list = new ArrayList();
public ContactAdapter(Context context, int resources){
super(context,resources);
}
public void add(Contacts object)
{
super.add(object);
list.add(object);
}
#Override
public int getCount() {return list.size();}
#Override
public Object getItem(int position) {return list.get(position);}
#Override
public View getView(int position,View convertView,ViewGroup parent)
{
View row;
row = convertView;
ContactHolder contactHolder;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_stop = (TextView)row.findViewById(R.id.tx_stop);
row.setTag(contactHolder);
}
else
{
contactHolder = (ContactHolder)row.getTag();
}
Contacts contacts = (Contacts)this.getItem(position);
contactHolder.tx_stop.setText(contacts.getStop());
return row;
}
static class ContactHolder
{
TextView tx_stop;
}
}
Call
contactAdapter.notifyDataSetChanged();
after your while loop.
You can create ArrayList<Contacts> contactsArray = new ArrayList<Contacts>(); then push all objects in it after that to set your adapter with passed array. Your method should look like:
public void setTextToTextView(JSONArray jsonArray) {
ArrayList<Contacts> contactsArray = new ArrayList<Contacts>();
try {
int count = 0;
String stop;
while(count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
stop = JO.getString("stop");
Contacts contacts = new Contacts(stop);
contactsArray.add(contacts);
count++;
}
contactAdapter = new ContactAdapter(this,R.layout.row_layout, contactsArray);
listView.setAdapter(contactAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
In your adapter do the following:
ArrayList<Contacts> list = new ArrayList<Contacts>();
public ContactAdapter(Context context, int resources, ArrayList<Contacts> list){
super(context,resources);
this.list = list;
}
That's all. Hope to help!

How can I pass the user input number from EditText and set it on the 2nd number in for each inside my _createListView()?

How can I pass the user input number from EditText in android and set it on the 2nd number in for statement inside my _createListView()?
public class MainActivity extends Activity {
private ListView _listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
Intent intent = getIntent();
int int_text = intent.getIntExtra("name", 100);
this._listView = (ListView) this.findViewById(R.id.listview);
this._createListView();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void _createListView() {
List<RowItem> rowItems = new ArrayList<RowItem>();
for (int i = 1; i <= 100 ; i++) {
String indexAsString = String.valueOf(i);
if (i % 3 == 0 && i % 5 == 0) {
rowItems.add(new RowItem(indexAsString, "FizzBuzz"));
} else if (i % 3 == 0) {
rowItems.add(new RowItem(indexAsString, "Fizz"));
} else if (i % 5 == 0) {
rowItems.add(new RowItem(indexAsString, "Buzz"));
} else {
rowItems.add(new RowItem(indexAsString, indexAsString));
}
}
RowItemArrayAdapter arrayAdapter = new RowItemArrayAdapter(this,
R.layout.activity_main_list_row, rowItems);
this._listView.setAdapter(arrayAdapter);
}
private static class RowItem {
private String _index;
private String _value;
public RowItem(String index, String value) {
this._index = index;
this._value = value;
}
public String getIndex() {
return this._index;
}
public String getValue() {
return this._value;
}
}
private static class ViewHolder {
public TextView indexTextView;
public TextView valueTextView;
}
private static class RowItemArrayAdapter extends ArrayAdapter<RowItem> {
private LayoutInflater _layoutInflater;
private int _textViewResourceId;
public RowItemArrayAdapter(Context context, int resource, List<RowItem> objects) {
super(context, resource, resource, objects);
this._layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this._textViewResourceId = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final RowItem rowItem = (RowItem) this.getItem(position);
ViewHolder viewHolder;
if (null == convertView) {
convertView = this._layoutInflater.inflate(this._textViewResourceId, null);
viewHolder = new ViewHolder();
viewHolder.indexTextView = (TextView) convertView.findViewById(R.id.index);
viewHolder.valueTextView = (TextView) convertView.findViewById(R.id.value);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.indexTextView.setText(rowItem.getIndex());
viewHolder.valueTextView.setText(rowItem.getValue());
return convertView;
}
}
}
Get the value in a string and call it whereever you want orelse if you want to store it somewhere use sharedpreference.
Store values with sharedpreference :
SharedPreference mypref = getActivity().getSharedPreferences("name_key", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= mypref.edit();
editor.putString("name_key",name_string);
editor.apply();
To retrieve it from anywhere in your program:
SharedPreference mypref = getActivity().getSharedPreferences("name_key", Context.MODE_PRIVATE);
String av = mypref.getString("name","");

Cannot show image in listview from json

May be this question many times.
i am getting some data from server and showing in listview . every thing working fine but i am getting problem to show image in list view.
Here is my example code
public class MainActivity extends ListActivity {
private static String url = null;
private static final String book_name = "b_name";
private static final String book_detail = "b_publisher";
private static final String book_image = "b_image";
ProgressDialog progressDialog;
ListView lv;
String cus_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
url = getResources().getString(R.string.url);
/*----Receiving data from Splash Activity-----*/
Bundle b = getIntent().getExtras();
cus_id = b.getString("custom_id");
new ProgressTask(MainActivity.this).execute();
}
class ProgressTask extends AsyncTask<String, Integer, Boolean> {
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
public ProgressTask(ListActivity activity) {
context = activity;
}
private Context context;
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Processing...");
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
#Override
protected void onProgressUpdate(Integer... values) {
// set the current progress of the progress dialog
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(final Boolean success) {
progressDialog.dismiss();
}
protected Boolean doInBackground(final String... args) {
url = url + "?custom_iid=" + cus_id;
Log.d("Passing Url", url);
CustomListAdapter jParser = new CustomListAdapter();
JSONArray json = jParser.getJSONFromUrl(url);
if (json != null) {
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String b_image = c.getString("b_image");
String b_name = c.getString("b_name");
String b_detail = c.getString("b_publisher");
Log.d("detail", "" + b_image);
setBookImageUrl(b_image);
setBookName(b_name);
setBookDetail(b_detail);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
public String getBookImageUrl() {
return book_image;
}
public CharSequence getBookName() {
return book_name;
}
public CharSequence getBookDetail() {
return book_detail;
}
public void setBookImageUrl(String imgeUrl) {
book_image = imgeUrl;
}
public void setBookName(String b_name) {
book_name = b_name;
}
public void setBookDetail(String b_detail) {
book_detail = b_detail;
}
}
BookListAdapter class:
public class BookListAdapter extends ArrayAdapter<MainActivity> {
private ArrayList<MainActivity> bookModels;
private Context context;
public BookListAdapter(Context context, int resource,
ArrayList<MainActivity> bookModels) {
super(context, resource, bookModels);
this.bookModels = bookModels;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.bookIcon = (ImageView) rowView.findViewById(R.id.icon);
viewHolder.bookName = (TextView) rowView.findViewById(R.id.b_name);
viewHolder.bookDetail = (TextView) rowView
.findViewById(R.id.b_detail);
rowView.setTag(viewHolder);
}
final MainActivity bookModel = bookModels.get(position);
ViewHolder holder = (ViewHolder) rowView.getTag();
Picasso.with(context).load(bookModel.getBookImageUrl())
.into(holder.bookIcon);
holder.bookName.setText(bookModel.getBookName());
holder.bookDetail.setText(bookModel.getBookDetail());
return rowView;
}
#Override
public int getCount() {
return bookModels.size();
}
static class ViewHolder {
public ImageView bookIcon;
public TextView bookName;
public TextView bookDetail;
}
}
i can show book name and book detail in listview finely but image is not showing ..
i am getting value for book_image is http:\/\/X.X.X.X\/admin\/book_images\/232513pic9.png how to show in listview from that path..
I think you will need to implement your own adapter and use some library to display the image from URL.
My recommendation is Picasso
This is an example to implement your own adapter
BookListAdapter.java
public class BookListAdapter extends ArrayAdapter<BookModel> {
private ArrayList<BookModel> bookModels;
private Context context;
public BookListAdapter(Context context, int resource, ArrayList<BookModel> bookModels) {
super(context, resource, bookModels);
this.bookModels = bookModels;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.book_child_list, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.bookIcon = (ImageView) rowView
.findViewById(R.id.bookIcon);
viewHolder.bookName = (TextView) rowView
.findViewById(R.id.bookName);
viewHolder.bookDetail = (TextView) rowView
.findViewById(R.id.bookDetail);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
final BookModel bookModel = bookModels.get(position);
Picasso.with(context).load(bookModel.getBookImageUrl()).into(viewHolder.bookIcon);
viewHolder.bookName.setText(bookModel.getBookName());
viewHolder.bookDetail.setText(bookModel.getBookDetail());
return rowView;
}
#Override
public int getCount() {
return bookModels.size();
}
static class ViewHolder {
public ImageView bookIcon;
public TextView bookName;
public TextView bookDetail;
}
}
BookModel.java
public class BookModel {
private String bookName;
private String bookDetail;
private String bookImageUrl;
public BookModel() {
bookName = "";
bookDetail = "";
bookImageUrl = "";
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookDetail() {
return bookDetail;
}
public void setBookDetail(String bookDetail) {
this.bookDetail = bookDetail;
}
public String getBookImageUrl() {
return bookImageUrl;
}
public void setBookImageUrl(String bookImageUrl) {
this.icons = bookImageUrl;
}
}
Where BookModel class is a class where you can wrap your data (book name, book detail, book image) and pass it as a list to the adapter.
for example :
protected Boolean doInBackground(final String... args) {
url = url + "?custom_iid=" + cus_id;
Log.d("Passing Url", url);
CustomListAdapter jParser = new CustomListAdapter();
JSONArray json = jParser.getJSONFromUrl(url);
ArrayList<BookModel> bookModelList = new ArrayList<BookModel>();
if (json != null) {
for (int i = 0; i < json.length(); i++) {
try {
BookModel bookModel = new BookModel();
JSONObject c = json.getJSONObject(i);
String b_image = c.getString("b_image");
String b_name = c.getString("b_name");
String b_detail = c.getString("b_publisher");
Log.d("detail", "" + b_image);
bookModel.setBookName(b_name);
bookModel.setBookDetail(b_detail);
bookModel.setBookImageUrl(b_image);
bookModelList.add(bookModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if(bookModelList.size()>0) {
BookListAdapter bookListAdapter = new BookListAdapter(MainActivity.this, R.id.yourlistview, bookModelList );
}
return null;
}
I hope my answer can help you!
lv.setAdapter(adapter);
use AQuery lib.
and just write this code in your custom adapter:
AQuery aQuery = new AQuery(context);
aQuery.id(your image id).image(your url,true,true);

indexoutofbound exception while adding item in listview android

Hello I am new in android
I have some dificulty to add element in daynamic listview.
When I click on TextView(tv) it should be add element at the end of the ArrayList but when i scroll down to the listview it crashed with indexoutofbound exception.
public class PosterList extends Activity
{
MyCustomAdapter dataAdapter = null;
ArrayList<Country> countryList = new ArrayList<Country>();
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.posterlist);
//Click on textview to add element in contrylist
tv = (TextView) findViewById(R.id.myFilter);
tv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Country country = new Country("df","df","df","m");
countryList.add(country);
dataAdapter.notifyDataSetChanged();
}
});
displayListView();
}
private void displayListView()
{
//Parse my JSON and store it in to different arrays
for(int k=0;k<len;k++)
{
Country country = new Country(subcategory[k],caseid[k],time[k],newpost[k]);
countryList.add(country);
}
dataAdapter = new MyCustomAdapter(this,R.layout.country_info, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Country country = (Country) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
country.getContinent(), Toast.LENGTH_SHORT).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Country>
{
private ArrayList<Country> originalList;
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
this.originalList = new ArrayList<Country>();
this.originalList.addAll(countryList);
}
private class ViewHolder
{
TextView code;
TextView name;
TextView continent;
TextView region;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.continent = (TextView) convertView.findViewById(R.id.continent);
holder.region = (TextView) convertView.findViewById(R.id.region);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.code.setText(country.getCode());
holder.name.setText(country.getName());
holder.continent.setText(country.getContinent());
holder.region.setText(country.getRegion());
return convertView;
}
}
}
Here is my Country class,can anybody help me out what is wrong with this code?
`enter code here`public class Country {
String code = null;
String name = null;
String continent = null;
String region = null;
public Country(String code, String name, String continent, String region) {
super();
this.code = code;
this.name = name;
this.continent = continent;
this.region = region;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
#Override
public String toString() {
return code + " " + name + " "
+ continent + " " + region;
}
Get rid of these fields in your adapter
private ArrayList<Country> originalList;
private ArrayList<Country> countryList;
You are modifying the countryList that is being passed to the super call in your adapter but you are reading from the countryList that is stored as a field inside of your adapter which never gets modified when you add a new country!
You are creating a copy of your original countrylist.. but you never modify the copy after creating it

Android resource cannot be resolved to a variable

I am trying to Create a Array Adapter to set content from an Array of text to the adapter but i am not able to set the adapter from the Activity. Am not sure what i should pass for the resource as Int
PlacesListAdapter.java
public class PlacesListAdapter extends ArrayAdapter<Place> {
public Context context;
private List<Place> places;
public PlacesListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public PlacesListAdapter(Context context, int resource, List<Place> places) {
super(context, resource, places);
this.context = context;
this.places = places;
// imageLoader = new ImageLoader(context.getApplicationContext());
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
Place p = places.get(position);
if (convertView == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
view = viewInflater.inflate(R.layout.item_place, null);
holder = new ViewHolder();
holder.placeTitle = (TextView) view.findViewById(R.id.place_title);
holder.placeDistance = (TextView) view
.findViewById(R.id.place_distance);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.placeTitle.setText(p.getPlaceTitle());
holder.placeDistance.setText("200");
holder.placeCategoryIcon.setImageResource(R.drawable.marker);
return view;
}
static class ViewHolder {
TextView placeId;
TextView placeTitle;
TextView placeDistance;
ImageView placeCategoryIcon;
}
}
Place.java
public class Place {
String placeId = "", placeTitle = "", placeDistance = "",
placeCategoryIcon = "";
public Place(String placeId, String placeTitle, String placeDistance,
String placeCategoryIcon) {
this.placeId = placeId;
this.placeTitle = placeTitle;
this.placeDistance = placeDistance;
this.placeCategoryIcon = placeCategoryIcon;
}
public String getPlaceId() {
return placeId;
}
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
public String getPlaceTitle() {
return placeTitle;
}
public void setPlaceTitle(String placeTitle) {
this.placeTitle = placeTitle;
}
public String getPlaceDistance() {
return placeDistance;
}
public void setPlaceDistance(String placeDistance) {
this.placeDistance = placeDistance;
}
public String getPlaceCategoryIcon() {
return placeCategoryIcon;
}
public void setPlaceCategoryIcon(String placeCategoryIcon) {
this.placeCategoryIcon = placeCategoryIcon;
}
}
Now in MainActiivty i am trying to set the adapter so that it populates the list from the Array
public class MainActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener {
private final String[] places = new String[] { "Mysore", "Bangalore", "Mangalore",
"Wayanad", "Bandipur National Park", "Chickmaglur",
"Bandipura", "Coorg", "Kodaikanal", "Hampi",
"Ghati Subramanya", "Mekedatu", "Muththathhi", "Shivasamudram",
"Talakadu", "Savana Durga" };
public SearchView mSearchView;
private TextView mStatusView;
private Menu mainMenu = null;
PlacesListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Nomad", "onCreate");
ListView listView = (ListView) findViewById(R.id.place_list);
adapter = new PlacesListAdapter(this, resource, places);
}
}
I am not sure what to set for resources in adapter = new PlacesListAdapter(this, resource, places);
Initialize the resource variable:
// it doesn't matter what values you assing to the resource variable because you build
// the row layout yourself in the getView method of the adapter
int resource = android.R.layout.simple_list_item_1;
adapter = new PlacesListAdapter(this, resource, places);
Edit:
List<Place> thePlaces = new ArrayList<Place>();
for (int i = 0; i < places.length; i++) {
Place pl = new Place("NO_ID", places[i], "NO_DISTANCE", "NO_CATEGORYICON");
thePlaces.add(pl);
}
int resource = android.R.layout.simple_list_item_1;
adapter = new PlacesListAdapter(this, resource, thePlaces);
pass row layout id instead of default android layout if your are creating custom adapter by extending ArrayAdapter:
adapter = new PlacesListAdapter(this,R.layout.item_place, places);
instead of
adapter = new PlacesListAdapter(this, resource, places);

Categories

Resources