I am working on an app with multiple TextViews in a ListView, but I seem to have encountered a problem. The list does not appear to get modified.
I'm attachig my code now
public class MainActivity extends ActionBarActivity {
private List<CustomItem> myItem = new ArrayList<>();
ListView listView;
EditText edit;
Button insert;
String getText;
StringTokenizer st;
private String text1,text2;
List<CustomItem> customItem = new ArrayList<CustomItem>();
ArrayAdapter<CustomItem> adapter_List;
CompleteAdapter completeAdapter;
CustomItem obj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
insert = (Button)findViewById(R.id.addBtn);
populate();
listView = (ListView) findViewById(R.id.listView);
completeAdapter = new CompleteAdapter(customItem, this);
listView.setAdapter(completeAdapter);
}
private void populate() {
customItem.add(new CustomItem("Testing","Application"));
}
public void addToList(View view) {
getText = edit.getText().toString();
st = new StringTokenizer(getText," ");
text1 = st.nextToken().toString();
text2 = st.nextToken().toString();
MainActivity.this.customItem.add(new CustomItem(text1,text2));
MainActivity.this.completeAdapter.notifyDataSetChanged();
}
/*
private class MyListAdapter extends ArrayAdapter<CustomItem> {
private MyListAdapter() {
super(MainActivity.this, R.layout.item_row,myItem);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView== null){
itemView = getLayoutInflater().inflate(R.layout.item_row,parent,false);
}
CustomItem customItem = myItem.get(position);
TextView t1 = (TextView) itemView.findViewById(R.id.text1);
TextView t2 = (TextView) itemView.findViewById(R.id.text2);
t1.setText(customItem.getName());
t2.setText(customItem.getAddress());
return itemView;
}
}*/
}
this is my adapter class
class CompleteAdapter extends ArrayAdapter<CustomItem>{
private List<CustomItem> items;
private Context context;
CompleteAdapter(List<CustomItem> items, Context context) {
super (context,R.layout.item_row,items);
this.items = items;
this.context = context;
}
#Override
public int getCount() {
return 0;
}
#Override
public CustomItem getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= convertView;
Holder holder = new Holder();
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_row, null);
TextView name_text = (TextView)v.findViewById(R.id.name);
TextView address_text = (TextView)v.findViewById(R.id.address);
holder.nameView = name_text;
holder.addView = address_text;
v.setTag(holder);
}
else
holder = (Holder)v.getTag();
CustomItem custom = items.get(position);
holder.nameView.setText(custom.getName());
holder.addView.setText(custom.getAddress());
return v;
}
private class Holder {
public TextView nameView;
public TextView addView;
}
}
This is my CustomIte.java
class CustomItem{
private String name;
private String address;
CustomItem(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}}
As you can see I've tried with 2 adapters with the same result. There is data contained in the Strings being passed, so I think the problem is with the adapter. (The ids of all the components are perfect) What changes should I make?
Should change in your Adapter
#Override
public int getCount() {
return 0;
}
#Override
public CustomItem getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
to
#Override
public int getCount() {
return items.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public CustomItem getItem(int position) {
return items.get(position);
}
The problem is in your adapter which return a null view (v)
#Override
public int getCount() {
return items.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public CustomItem getItem(int position) {
return items.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView == null){
convertView =LayoutInflater.from(context).inflate(R.layout.item_row, null);
TextView name_text = (TextView)v.findViewById(R.id.name);
TextView address_text = (TextView)v.findViewById(R.id.address);
holder = new Holder();
holder.nameView = name_text;
holder.addView = address_text;
v.setTag(holder);
}
else
holder = (Holder)v.getTag();
CustomItem custom = items.get(position);
holder.nameView.setText(custom.getName());
holder.addView.setText(custom.getAddress());
return convertView ;
}
Hop it helps ;)
Related
public class ListViewAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
public ListViewAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
list = new View(context);
list = inflater.inflate(R.layout.listview_layout, null);
TextView name = (TextView) list.findViewById(R.id.location_name);
TextView desc = (TextView) list.findViewById(R.id.location_desc);
ImageView img = (ImageView) list.findViewById(R.id.location_image);
name.setText(locationDetails[i].getLocationName());
desc.setText(locationDetails[i].getLocationDesc());
img.setImageResource(locationDetails[i].getLocationIcon());
} else {
list = (View) view;
}
return list;
}
}
This code is the code before I use viewholder, it works fine. Then I tried to modify it with the use of viewholder, and compile it with gradle, all the image and textviews disappeared, I don't know what's wrong with the code below. Why can't I get any thing shown on the screen?
public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;
if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);
vh = new ViewHolder();
vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);
list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
return list;
}
static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}
public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
Object model;
public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;
if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);
vh = new ViewHolder();
vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);
list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
model = getItem(i);
vh.name.setText(model.getLocationName());
vh.desc.setText(model.getLocationDesc());
// same as image...
return list;
}
static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}
UPDATE WITH VIEW HOLDER
public class DatabaseListAdapter extends BaseAdapter {
Context context;
ArrayList<DatabaseListItems> databaseList;
ArrayList<String> DeleteList = new ArrayList<>();
static class ViewHolder {
TextView tvFirstName;
TextView tvSecondName;
View row;
}
public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
this.context = context;
databaseList = list;
}
#Override
public int getCount() {
return databaseList.size();
}
#Override
public Object getItem(int position) {
return databaseList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final DatabaseListItems databaseListItems = databaseList.get(position);
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.custom_listview_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvFirstName = (TextView) rowView.findViewById(R.id.txtViewFirstName);
viewHolder.tvSecondName = (TextView) rowView.findViewById(R.id.txtViewSecondName);
viewHolder.row = rowView.findViewById(R.id.row);
rowView.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) rowView.getTag();
holder.tvFirstName.setText(databaseListItems.getFirstName());
holder.tvSecondName.setText(databaseListItems.getSecondName());
if (databaseListItems.getFirstRow() == true) {
holder.tvSecondName.setAlpha(0.0f);
holder.tvFirstName.setTextColor(Color.parseColor("#161616"));
holder.row.setBackgroundColor(Color.parseColor("#f7f7f7"));
holder.row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,OtherActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
} else {
holder.tvSecondName.setAlpha(1.0f);
}
}
return rowView;
}
}
in my android app, i have got an custom table list, where the data will be dynamic filled.
only the first row should a an static row, which i set programmatically.
i have this function to fill my data into the list
private void showList() {
boolean FirstRow = true;
ArrayList<DatabaseListItems> databaseList = new ArrayList<>();
databaseList.clear();
String query = "SELECT * FROM " + DatabaseHelper.DATABASE_TABLE;
Cursor c1 = dbHandler.selectQuery(query);
if (c1.moveToFirst()) {
do {
DatabaseListItems databaseListItems = new DatabaseListItems();
if (c1.getPosition() == 0 && FirstRow == true) {
databaseListItems.setFirstRow(true);
FirstRow = false;
databaseListItems.setFirstName("FIRST ROW");
databaseListItems.setSecondName(null);
c1.moveToPosition(-1);
} else {
databaseListItems.setFirstRow(false);
FirstRow = false;
databaseListItems.setFirstName(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_FIRSTNAME)));
databaseListItems.setSecondName(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_SECONDNAME)));
}
databaseList.add(databaseListItems);
} while (c1.moveToNext());
}
c1.close();
DatabaseListAdapter databaseListAdapter = new DatabaseListAdapter(getActivity(), databaseList);
ListView.setAdapter(databaseListAdapter);
}
DatabaseListItems
public class DatabaseListItems {
String firstname;
String secondname;
Boolean FirstRow;
public String getFirstName() {
return firstname;
}
public void setFirstName(String first name) {
this.firstname= firstname;
}
public String getSecondName() {
return secondname;
}
public void setSecondName(String secondname) {
this.secondname = secondname;
}
public Boolean getFirstRow() {
return FirstRow;
}
public void setFirstRow(Boolean FirstRow) {
this.FirstRow = FirstRow;
}
}
DatabaseListAdapter
public class DatabaseListAdapter extends BaseAdapter {
Context context;
ArrayList<DatabaseListItems> databaseList;
ArrayList<String> DeleteList = new ArrayList<>();
public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
this.context = context;
databaseList = list;
}
#Override
public int getCount() {
return databaseList.size();
}
#Override
public Object getItem(int position) {
return databaseList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
final DatabaseListItems databaseListItems = databaseList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_listview_item, null);
}
TextView tvFirstName = (TextView) convertView.findViewById(R.id.txtViewFirstName);
tvFirstName.setText(databaseListItems.getFirstName());
TextView tvSecondName = (TextView) convertView.findViewById(R.id.txtViewSecondName);
tvSecondName.setText(databaseListItems.getSecondName());
View row = convertView.findViewById(R.id.row);
ImageView Arrow = (ImageView) convertView.findViewById(R.id.Arrow);
// First Static Row
if (databaseListItems.getFirstRow() == true) {
tvSecondName.setAlpha(0.0f);
Arrow.setAlpha(1.0f);
tvFirstName.setTextColor(Color.parseColor("#161616"));
row.setBackgroundColor(Color.parseColor("#f7f7f7"));
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,OtherActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
} else {
tvSecondName.setAlpha(1.0f);
Arrow.setAlpha(0.0f);
}
return convertView;
}
}
On the first view, all looks fine.
but if i scroll down and up, random rows get the background color and the onClick function , which only the first row should get.
anyone an idea what i do wrong?
I am new to android programming. I am working on android app and I have a listview with multiple textViews out of that 2 textViews are Clickable. If user clicks on 'phone' textView it should call to that number and if user clicks on 'street_address' textView it should route it with google maps. Following is my code of my adapter class
class ListViewHolder {
TextView name;
TextView street_address;
TextView phone;
TextView distance;
ListViewHolder(View v)
{
name = (TextView) v.findViewById(R.id.name);
street_address = (TextView) v.findViewById(R.id.address);
phone = (TextView) v.findViewById(R.id.phone);
distance = (TextView) v.findViewById(R.id.distance);
}
}
public class ListDisplayAdapter extends BaseAdapter
{
Context mcontext = null;
public List<NameAddress> listData;
public ListDisplayAdapter(Context context, List<NameAddress> list)
{
mcontext = context;
this.listData = list;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int i)
{
return listData.get(i);
}
#Override
public long getItemId(int i)
{
return i;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View row = view;
ListViewHolder holder = null;
if (row == null)
{
LayoutInflater inflater =(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_row, viewGroup, false);
holder=new ListViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ListViewHolder) row.getTag();
}
NameAddress data = listData.get(position);
holder.name.setText(data.getName());
holder.phone.setText(data.getPhone());
holder.street_address.setText(data.toString());
holder.distance.setText(data.getDistance());
return row;
}
}
Following is hte code of activity class
public class ListDisplayActivity extends Activity
{
ListView listView;
public String tag_name;
public List<NameAddress> nameAddressList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_display);
listView = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
if(intent!= null)
{
tag_name = intent.getStringExtra("DashItemName");
}
nameAddressList = null;
try
{
XMLDOMParserHandler parser = new XMLDOMParserHandler(tag_name);
nameAddressList = parser.parseXML(getAssets().open("data.xml"));
ListDisplayAdapter listAdapter = new ListDisplayAdapter(this, nameAddressList);
listView.setAdapter(listAdapter);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Please tell me how can I code this
try this,
public class ListDisplayAdapter extends BaseAdapter
{
Context mcontext = null;
public List<NameAddress> listData;
public ListDisplayAdapter(Context context, List<NameAddress> list)
{
mcontext = context;
this.listData = list;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int i)
{
return listData.get(i);
}
#Override
public long getItemId(int i)
{
return i;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View row = view;
ListViewHolder holder = null;
if (row == null)
{
LayoutInflater inflater =(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ListViewHolder();
row = inflater.inflate(R.layout.list_row, viewGroup, false);
name = (TextView) v.findViewById(R.id.name);
holder.street_address = (TextView) v.findViewById(R.id.address);
holder.phone = (TextView) v.findViewById(R.id.phone);
holder.distance = (TextView) v.findViewById(R.id.distance);
row.setTag(holder);
}
else
{
holder = (ListViewHolder) row.getTag();
}
NameAddress data = listData.get(position);
holder.name.setText(data.getName());
holder.phone.setText(data.getPhone());
holder.phone.settag(data.getPhone());
holder.street_address.setText(data.toString());
holder.street_address.settag(data.toString());
holder.distance.setText(data.getDistance());
holder.phone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String phoneno = ((Textview)v).gettag();
}
});
holder.street_address.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String streetaddress = ((Textview)v).gettag();
}
});
return row;
}
class ListViewHolder {
TextView name;
TextView street_address;
TextView phone;
TextView distance;
}
}
I'm having a problem with returning my view to a listview with my custom adapter.
This is what I have in my main class which extends ListActivity.
lv = getListView();
ListAdapater itemAdapter = new ItemAdapter(MainActivity.this,itemList)
lv.setAdapter(itemAdapter);
And here's what I have for my custom adapter named ItemAdapter:
public class ItemAdapter extends BaseAdapter {
private static LayoutInflater inflater;
private Context context;
private ArrayList<HashMap<String,String>> newsItem;
public ItemAdapter(Context a, ArrayList<HashMap<String, String>> item){
super();
this.context = a;
this.newsItem = item;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
if(view==null){
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.news_item_layout,parent,false );
}
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView date = (TextView)vi.findViewById(R.id.date); // date
HashMap<String,String> nItem = newsItem.get(position);
title.setText(nItem.get(MainActivity.TITLE));
date.setText(nItem.get(MainActivity.DATE));
//UrlImageViewHelper.setUrlDrawable(thumb_image, nItem.get(MainActivity.IMAGE));
return vi;
}
I'm not sure why this adapter does not return any view at all? I've tried it with a SimpleAdapter with the same data and it works. I need to create a custom adapter to handle image data.
Any help would be greatly appreciated. Thanks
Based on count only your views will added to your listview, So change your getCount() method as
#Override
public int getCount() {
return newsItem.size();
}
And change
if(view==null)
to
if(vi==null)
I hope this will help you.
Here in your adapter you return 0 value.
#Override
public int getCount() {
return 0;
}
change it to
#Override
public int getCount() {
return item.size();
}
and in getView method change
if(view==null)
to
if(vi==null)
#Override
public int getCount() {
return newsItem.size();
}
#Override
public Object getItem(int i) {
return newsItem.get(i);
}
#Override
public long getItemId(int i) {
return newsItem.get(i).hashCode();
}
and
if(vi==null)
// try this way,hope this will help you...
public class ItemAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> newsItem;
public ItemAdapter(Context a, ArrayList<HashMap<String, String>> item) {
super();
this.context = a;
this.newsItem = item;
}
#Override
public int getCount() {
return newsItem.size();
}
#Override
public Object getItem(int i) {
return newsItem.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.news_item_layout,null,false);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.date = (TextView) convertView.findViewById(R.id.date);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.title.setText(newsItem.get(position).get(MainActivity.TITLE));
holder.date.setText(newsItem.get(position).get(MainActivity.DATE));
//UrlImageViewHelper.setUrlDrawable(thumb_image, nItem.get(MainActivity.IMAGE));
return view;
}
class ViewHolder{
TextView title;
TextView date;
}
}
This is my BaseAdapater class in which I am using ViewHolder.
public class CategoryAdapter extends BaseAdapter {
private Context mContext;
public int[] mThumbIds = {R.drawable.shoes,R.drawable.dress,R.drawable.purse,
R.drawable.shoes,R.drawable.dress,R.drawable.purse ,
R.drawable.shoes,R.drawable.dress,R.drawable.purse };
public WishbookCategoryAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.wishbook_gridview_items, null);
holder.Category = (TextView)convertView.findViewById(R.id.wishbook_grid_category);
holder.items = (TextView)convertView.findViewById(R.id.wishbook_grid_category_number);
holder.iv = (ImageView)convertView.findViewById(R.id.wishbook_category_image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.iv.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
holder.iv.setScaleType(ScaleType.FIT_XY);
holder.Category.setText("Category");
holder.items.setText("items");
return convertView;
}
private static class ViewHolder {
protected ImageView iv;
protected TextView Category;
protected TextView items;
}
}