Updating data in custom data in custom listview (Android) - android

I followed the following tutorial to create a listview that has both an image and text in it. [http://www.debugrelease.com/2013/06/24/android-listview-tutorial-with-images-and-text/
It looks like this..
[http://i.stack.imgur.com/l7ukU.png
I modified it slighlty to contain only three items. What I would like to do is change the text in the listview based on data that I will be receiving over USB. How do I go about changing the data from within my main activity?
Here is the code for my model for the list items...
public class Item
{
public int Id;
public String IconFile;
public String Name;
public Item(int id, String iconFile, String name)
{
Id = id;
IconFile = iconFile;
Name = name;
}
}
Here is the code for my arraylist
public class Model extends Globals
{
public static ArrayList<Item> Items;
public static void LoadModel()
{
Items = new ArrayList<Item>();
Items.add(new Item(1, "alarm.png", "Alarm"));
Items.add(new Item(2, "calc.png", "Calculator"));
Items.add(new Item(3, "play.png", "Play"));
}
public static Item GetbyId(int id)
{
for(Item item : Items)
{
if (item.Id == id)
{
return item;
}
}
return null;
}
}
Here is the code for the custom adapter...
public class ItemAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] Ids;
private final int rowResourceId;
public ItemAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.Ids = objects;
this.rowResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
int id = Integer.parseInt(Ids[position]);
String imageFile = Model.GetbyId(id).IconFile;
textView.setText(Model.GetbyId(id).Name);
// get input stream
InputStream ims = null;
try {
ims = context.getAssets().open(imageFile);
} catch (IOException e) {
e.printStackTrace();
}
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imageView.setImageDrawable(d);
return rowView;
}
And lastly my main activity code...
public class MainActivity extends Globals
{
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Model.LoadModel();
listView = (ListView) findViewById(R.id.listView);
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++)
{
ids[i] = Integer.toString(i+1);
}
ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
listView.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.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Exiting", Toast.LENGTH_SHORT)
.show();
//finish();
break;
default:
break;
}
return true;
}

When you receive data, you need to set adapter again and call notifitydatachange.

Related

how to save rating of rating bar in list view

i need help to save the rating of rating bar in my list view for each item.My question is how can i do it because the OnItemClickListener does not work.
this is how my custom array adapter looks like
public class CustomArrayAdapter extends ArrayAdapter<String> {
public ArrayList<String> ratings;
private Activity context;
private int[] imageID;
private String[] stdName;
private double[] GPA;
private String[] course;
private String[] rollNo;
public float cRating;
public CustomArrayAdapter(Activity context, int[] imageID, String[] stdName, double[] GPA, String[] course, String[] rollNo) {
super(context, R.layout.row_design, stdName);
this.context = context;
this.imageID = imageID;
this.stdName = stdName;
this.GPA = GPA;
this.course = course;
this.rollNo = rollNo;
}
static class ViewContainer
{
public ImageView stdImage;
public ImageView possibleImage;
public TextView primaryTV;
public TextView secondryTV;
public RatingBar ratingBar;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewContainer viewContainer;
View rowView = convertView;
if(rowView == null) {
//converting the XML file into view objects
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_design, null, true);
viewContainer = new ViewContainer();
//getting the refrence of widgets
viewContainer.stdImage = (ImageView) rowView.findViewById(R.id.student_image);
viewContainer.possibleImage = (ImageView) rowView.findViewById(R.id.possibility_image);
viewContainer.primaryTV = (TextView) rowView.findViewById(R.id.primary_text_view);
viewContainer.secondryTV = (TextView) rowView.findViewById(R.id.secondry_text_view);
viewContainer.ratingBar = (RatingBar) rowView.findViewById(R.id.rating_bar);
rowView.setTag(viewContainer);
} else {
viewContainer = (ViewContainer) rowView.getTag();
viewContainer.ratingBar.setRating(0.0f);
rowView.setBackground(null);
}
viewContainer.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
cRating = rating;
}
});
//setting the possible values to widgets
viewContainer.stdImage.setImageResource(imageID[position]);
viewContainer.primaryTV.setText(stdName[position] + " (GPA:" + GPA[position] + ")");
viewContainer.secondryTV.setText(course[position] + " (RollNo:" + rollNo[position] + ")");
if (GPA[position] < 3.0) {
rowView.setBackgroundResource(android.R.color.holo_red_light);
viewContainer.possibleImage.setImageResource(R.drawable.cross_small);
} else {
viewContainer.possibleImage.setImageResource(R.drawable.tick_small);
}
return rowView;
}
}
and this is my activity where i want to get the rating
public class StudentSelectionActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_selection);
//getting the refrence of list view
ListView listView = (ListView) findViewById(R.id.list);
//creating object of Rating activity to access it's public instances
final MainActivity obj = new MainActivity();
//creating custom adapter
final CustomArrayAdapter adapter = new CustomArrayAdapter(this, obj.imageID, obj.stdName, obj.GPA, obj.course, obj.rollNo);
listView.setAdapter(adapter);
//handling events on click listener of list view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(StudentSelectionActivity.this, "rating = " + adapter.cRating, Toast.LENGTH_SHORT).show();
}
});
}
#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_selected_students, 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);
}
}
Your setOnItemClickListener did not work for adapter class as you are using another clickable class inside adapter. So one option is to use click listener inside adapter class.
As
rowView.setOnClickListener(new View.onClickListener()
{
//Perform your operation.
});
Create an Entry class. This class holds your attributes, like cRating.
Then extend the CustomArrayAdapter for ArrayAdapter<Entry> instead of ArrayAdapter<String>. Change the constructor to something like this
public CustomArrayAdapter(Context context, int layoutResourceID, List<Entry> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.itemList = listItems;
this.layoutResID = layoutResourceID;
}
In your getView method use
EventDetailEntry item = this.itemList.get(position);
to get the corresponding item. Use this item to set the cRating. Then your onItemClicked method should work as expected. Hope that is clear enough.

add a row in custom ListView and get the data from intent.putExtra()

I have a custom listView that is empty in first time , from other side i have an activity which contains a product details such as picture, title, model .in this activity there is a button , so when user press this button i want to add the product into my custom listView and shows all the product details there,
my listView is:
public class ListViewShoppingBasketActivity extends Activity {
BaseAdapter adapter;
ArrayList<SingleRow> list;
Context context;
String[] titles;
Bitmap[] images;
String[] descriptions;
TextView title;
TextView description;
ImageView image;
String rowTitles;
String rowDescription;
Bitmap rowImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_shopping_basket_layout);
final ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new MyAdapter(this));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
if (id == 0) {
} else if (id == 1) {
} else if (id == 2) {
} else if (id == 3) {
} else if (id == 4) {
}
}
});
}
class SingleRow {
String rowTitles;
String rowDescription;
Bitmap rowImage;
public SingleRow(String rowTitles, String rowDescription, Bitmap rowImage) {
this.rowTitles = rowTitles;
this.rowDescription = rowDescription;
this.rowImage = rowImage;
}
}
class MyAdapter extends BaseAdapter {
public MyAdapter(Context c) {
context = c;
list = new ArrayList<SingleRow>();
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.shopping_basket_listview_row,
viewGroup, false);
title = (TextView) row.findViewById(R.id.textView1);
description = (TextView) row.findViewById(R.id.textView2);
image = (ImageView) row.findViewById(R.id.imageView1);
SingleRow temp = list.get(i);
title.setText(temp.rowTitles);
description.setText(temp.rowDescription);
image.setImageBitmap(temp.rowImage);
return row;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_view_shopping_basket, menu);
return true;
}
// // **********baraye add kardane item jadid be listview
public void addItems(View v) {
list.add(new SingleRow(rowTitles, rowDescription, rowImage));
adapter.notifyDataSetChanged();
}
}
and this is implementation of my button in other activity
b_add_to_cart = (Button) findViewById(R.id.b_add_to_cart);
b_add_to_cart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(BarcodeDetailsActivity.this,
ListViewShoppingBasketActivity.class);
intent.putExtra("productID", proId);
intent.putExtra("productModel", proModel);
intent.putExtra("productTitle", proTitle);
intent.putExtra("productPictureUrl", mIcon11);
startActivity(intent);
}
});
my problem is , i dont know how and where i can assign my product details in my listview
You can put the next lines to the code of the ListViewShoppingBasketActivity:
Intent i = getIntent();
if (i.getExtras() != null){
int prodId = i.getExtras().getInt("productID");
....
}
You can put theese to an ArrayList, and use an BaseAdapter (http://developer.android.com/reference/android/widget/BaseAdapter.html) to display the data in the list.

Highlighting row in listview that uses arrayadapter

I have created a custom lisview with images and text by using an array adapter. I would like to highlight one of the row manually, but I cannot seem to do this. I have tried using setItemChecked and setSelection(1);. I have made sure to disable touch events by calling listView.setEnabled(false);, as I read that manual selection might not work if touch events are enabled. Any insight into this matter would be greatly appreciated. I have included my source code below.
Main Activity
public class MainActivity extends Activity
{
// GUI
int Update_Frequency = 1000;
double ack = 0;
// Sensor Constants
public static String temperature = "--";
public static String humidity = "--";
public static String lpg = "--";
public static String alcohol = "--";
// Layout
ListView listView;
ItemAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize Interface
Model.LoadModel();
listView = (ListView) findViewById(R.id.listView);
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++)
{ids[i] = Integer.toString(i+1);}
this.adapter = new ItemAdapter(this,R.layout.row, ids);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setEnabled(false);
listView.setItemChecked(1, true);
listView.setSelection(1);
Model.LoadModel();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
GUI_Management();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_connect:
break;
case R.id.action_disconnect:
break;
case R.id.action_settings:
break;
case R.id.action_about:
break;
case R.id.action_exit:
break;
default:
break;
}
return true;
}
private void GUI_Management()
{
new Thread()
{
public void run()
{
//Replace with a changeable variable
while (true)
{
try
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
Model.LoadModel();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
Thread.sleep(Update_Frequency);
}
catch (InterruptedException e)
{
}
}
}
}.start();
}
}
Array Adapter
public class Model extends MainActivity
{
public static ArrayList<Item> Items;
public static void LoadModel()
{
Items = new ArrayList<Item>();
Items.add(new Item(1, "temperature_icon.png", "Temperature/Humidity " + temperature + "°F / " + humidity + "%"));
Items.add(new Item(2, "gas_icon.png", "LPG " + lpg +" ppm"));
Items.add(new Item(3, "alcohol_icon.png", "Alcohol " + alcohol + " ppm"));
}
public static Item GetbyId(int id)
{
for(Item item : Items)
{
if (item.Id == id)
{
return item;
}
}
return null;
}
}
class Item
{
public int Id;
public String IconFile;
public String Name;
public Item(int id, String iconFile, String name)
{
Id = id;
IconFile = iconFile;
Name = name;
}
}
class ItemAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] Ids;
private final int rowResourceId;
public ItemAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.Ids = objects;
this.rowResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
int id = Integer.parseInt(Ids[position]);
String imageFile = Model.GetbyId(id).IconFile;
textView.setText(Model.GetbyId(id).Name);
// get input stream
InputStream ims = null;
try {
ims = context.getAssets().open(imageFile);
} catch (IOException e) {
e.printStackTrace();
}
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imageView.setImageDrawable(d);
return rowView;
}
}
put setonclick listener in rootview object which are inflate by you in getview method this will solve your issue and on click event you can change the background of rootview and many more you can do with it.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
int id = Integer.parseInt(Ids[position]);
String imageFile = Model.GetbyId(id).IconFile;
textView.setText(Model.GetbyId(id).Name);
if (position == selectedItem)
{
rootView /*or you can use any viewgroup*/
.setBackgroundResource(R.drawable.background_dark_blue);
}
else
{
rootView
.setBackgroundResource(R.drawable.border02);
}
return rowView;
}
private int selectedItem;
public void setSelectedItem(int position) {
selectedItem = position;
}
i know this is not good solutionbut might this help you out you can use setSelectedItem method to highlight row
and please use static placeholder class for efficiency and use position field in static placeholder class to get position of selected row

Remove a single item from listview rows

I have a listview. Each row of a listview contains two texts (name and aaddress), one button and one radio button. I am using CustomAdapter for that. Now I have a condition; if a text1(name) is equal to "Pramod", then I have to delete the radio button from entire rows.
What is happening exactly? The radio button of only that row is deleting.
I have to delete the radio button of all rows. How do I fix that?
Here is my code of customAdapter class:
public class CustomAdapter extends ArrayAdapter<Item> {
private final Context context;
// private boolean userSelected = false;
public static RadioButton mCurrentlyCheckedRB;
private final ArrayList<Item> itemList;
int selected_itemindex=-1;
static String abc="";
public CustomAdapter(Context context, ArrayList<Item> itemList) {
super(context, R.layout.row_item, itemList);
this.context = context;
this.itemList = itemList;
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
final View rowView = inflater.inflate(R.layout.row_item, parent, false);
// 3. Get the two text view from the rowView
Button btn = (Button) rowView.findViewById(R.id.button1);
TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
final TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
final RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);
// 4. Set the text for textView
tv1.setText(itemList.get(position).getName());
tv2.setText(itemList.get(position).getAddress());
// This is the code I am using to delete the radio button from the entire row
if (itemList.get(position).getName().toString().equals("Pramod")) {
radio.setVisibility(View.INVISIBLE);
}
if (itemList.get(position).isSelected()) {
radio.setChecked(true);
rowView.setBackgroundColor(Color.CYAN);
}
else {
rowView.setBackgroundColor(Color.WHITE);
radio.setChecked(false);
}
// The radio button I am using to select a particular row one at a time.
radio.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (itemList.get(position).isSelected()) {
}
else
{
int selected_previousval = -1;
for (int i=0; i<itemList.size(); i++) {
if (itemList.get(i).isSelected()) {
selected_previousval = i;
break;
}
}
String name2=itemList.get(position).getName();
String add2=itemList.get(position).getAddress();
if (selected_previousval==-1) {
itemList.remove(position);
itemList.add(position,new Item(name2, add2, true));
rowView.setBackgroundColor(Color.CYAN);
Toast.makeText(context, "selected a 3 row",2000).show();
radio.setChecked(true);
}
else {
itemList.remove(position);
itemList.add(position, new Item(name2, add2, true));
name2 = itemList.get(selected_previousval).getName();
add2 = itemList.get(selected_previousval).getAddress();
itemList.remove(selected_previousval);
itemList.add(selected_previousval, new Item(name2, add2, false));
MainActivity.lv.setAdapter(new CustomAdapter(context, itemList));
}
}
}
});
return rowView;
}
}
And this is my Arraylist class:
public class Item {
private String Name;
private String Address;
public boolean selected;
public boolean isSelected() {
return this.selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public Item(String Name, String Address,boolean selected) {
super();
this.Name = Name;
this.Address = Address;
this.selected=selected;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
UPDATE
Question: I want to remove the radiobutton from every row of listview if text=pramod.
Answer:
MainActivity class
ArrayList<Item> items;
CustomAdapter adapter;
items = new ArrayList<Item>();
ArrayList<String> temparray;
items.add(new Item("Pramod", "Ballia", false, 0));
items.add(new Item("Pankaj", "Mau", false, 1));
items.add(new Item("Pradeep", "Ranchi", false, 1));
items.add(new Item("Jitendra", "Varansi", false, 1));
items.add(new Item("Amresh", "Sonbhadra", false, 1));
items.add(new Item("Anil", "Sarnath", false, 1));
temparray = new ArrayList<String>();
for(int i=0; i<items.size(); i++)
{
temparray.add(items.get(i).getName());
}
if (temparray.contains("Pramod"))
{
Log.d("temparray", "contains Pramod");
Global.show=0;
}
else
{
Log.d("temparray", "not contain");
Global.show=1;
}
lv = (ListView)dp.findViewById(R.id.listView1);
Spinner sp = (Spinner) dp.findViewById(R.id.spinner1);
adapter = new CustomAdapter(this, items);
lv.setAdapter(adapter);
customAdapter.class
if (Global.show == 0)
{
radio.setVisibility(View.INVISIBLE);
}
else
{
radio.setVisibility(View.VISIBLE);
if (itemList.get(position).isSelected()) {
radio.setChecked(true);
rowView.setBackgroundColor(Color.CYAN);
}
else {
rowView.setBackgroundColor(Color.WHITE);
radio.setChecked(false);
}
}
Global.class
public static class Global {
Public static int show;
}

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