android custom table list with a static row - android

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?

Related

ViewHolder doesn't work

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;
}
}

How to get selected item from GridView and show in ListView

I have GridView which contain the number of items, If user click on save button then selected value show in ListView, I tried but not got succeied, Can any one help to solve this problem?
MainActivity.java
fillDataMyProperty();
if (myPropertyArrayList != null && myPropertyArrayList.size() > 0) {
myPropertListAdapter = new MyPropertListAdapter(MainActivity.this, myPropertyArrayList, txtSubmit);
gridMyProperty.setAdapter(myPropertListAdapter);
}
My GridAdapter is
public class MyPropertListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ItemModel> arrayList;
private boolean check = false;
private View view;
private TextView txtSubmit;
public MyPropertListAdapter(Context context, ArrayList<ItemModel> arrayList, TextView txtSubmit) {
this.context = context;
this.arrayList = arrayList;
this.txtSubmit = txtSubmit;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
#SuppressLint("InflateParams")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.rlMain = (RelativeLayout) convertView.findViewById(R.id.rlMain);
viewHolder.imgCheck = (ImageView) convertView.findViewById(R.id.imgCheck);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
} else if (arrayList.get(position).isCheck() == false) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
}
viewHolder.rlMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view = v;
if (arrayList.get(position).isCheck() == false) {
Log.d("if---------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
viewHolder.imgCheck.setVisibility(View.VISIBLE);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
arrayList.get(position).setCheck(true);
check = true;
} else if (arrayList.get(position).isCheck() == true) {
Log.d("else-------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
arrayList.get(position).setCheck(false);
check = false;
}
}
});
txtSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showItemsDilaog();
}
});
return convertView;
}
public class ViewHolder {
TextView txtItem;
RelativeLayout rlMain;
ImageView imgCheck;
}
private void showItemsDilaog() {
final Dialog mDialog = new Dialog(context);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.dialog_list_item);
ImageView imgCancel = (ImageView) mDialog.findViewById(R.id.imgCancel);
ListView listItem = (ListView) mDialog.findViewById(R.id.listItem);
if (arrayList != null && arrayList.size() > 0) {
ItemsAdapter mAdapter = new ItemsAdapter(context, arrayList);
listItem.setAdapter(mAdapter);
}
imgCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}}
ItemsAdapter.Java
class ItemsAdapter extends BaseAdapter {
// private String[] mStrings = new String[]{"Elevator", "Air Conditionor", "Electricity", "Houe Keeping", "Garden", "All"};
private ViewHolder viewHolder;
private Context context;
private ArrayList<ItemModel> mArrayList;
public ItemsAdapter(Context context, ArrayList<ItemModel> maintananceModelArrayList) {
this.context = context;
this.mArrayList = maintananceModelArrayList;
}
#Override
public int getCount() {
return mArrayList.size();
}
#Override
public Object getItem(int position) {
return mArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.lineSeperator = (View) convertView.findViewById(R.id.lineSeperator);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
Log.d("Items:::::::::::::::",arrayList.get(position).getItem_name());
}
if (position == arrayList.size() - 1) {
viewHolder.lineSeperator.setVisibility(View.GONE);
} else {
viewHolder.lineSeperator.setVisibility(View.VISIBLE);
}
return convertView;
}
public class ViewHolder {
TextView txtItem;
View lineSeperator;
}
}

ListView Adapter - ItemID

I have the following ArrayAdapter:
public class CriteriosAdapter extends ArrayAdapter<Criterio> {
private Context context;
public CriteriosAdapter(Context context, List<Criterio> objects) {
super(context, 0, objects);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Criterio criterio = getItem(position);
final CriterioViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_row, parent, false);
viewHolder = new CriterioViewHolder();
viewHolder.txtCriterio = (TextView)convertView.findViewById(R.id.txtCriterio);
viewHolder.tgIrregular = (ToggleButton)convertView.findViewById(R.id.tgIrregular);
viewHolder.btnCam = (ImageView) convertView.findViewById(R.id.btnCam);
convertView.setTag(viewHolder);
}
else {
viewHolder = (CriterioViewHolder)convertView.getTag();
}
viewHolder.txtCriterio.setText(criterio.nome);
viewHolder.txtCriterio.setTextColor(context.getColor(R.color.white));
viewHolder.tgIrregular.setChecked(false);
viewHolder.tgIrregular.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.tgIrregular.isChecked()) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
}
}
});
viewHolder.btnCam.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
FragmentActivity activity = (FragmentActivity)(context);
android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
FotosFragment alertDialog = new FotosFragment();
alertDialog.show(fm, "fragment_alert");
}
});
return convertView;
}
class CriterioViewHolder {
TextView txtCriterio;
ToggleButton tgIrregular;
ImageView btnCam;
}
}
Therefore, I want to set the itemID of each item as the ID stored in the database. Because, when the toggle button is setted to ON, I wish to get that value.
Could someone help on it?
public class CriteriosAdapter extends ArrayAdapter<Criterio> {
private Context context;
public CriteriosAdapter(Context context, List<Criterio> objects) {
super(context, 0, objects);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Criterio criterio = getItem(position);
final CriterioViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_row, parent, false);
viewHolder = new CriterioViewHolder();
viewHolder.txtCriterio = (TextView)convertView.findViewById(R.id.txtCriterio);
viewHolder.tgIrregular = (ToggleButton)convertView.findViewById(R.id.tgIrregular);
viewHolder.btnCam = (ImageView) convertView.findViewById(R.id.btnCam);
convertView.setTag(viewHolder);
}
else {
viewHolder = (CriterioViewHolder)convertView.getTag();
}
viewHolder.txtCriterio.setText(criterio.nome);
viewHolder.txtCriterio.setTextColor(context.getColor(R.color.white));
viewHolder.tgIrregular.setChecked(false);
viewHolder.tgIrregular.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.tgIrregular.isChecked()) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
}
}
});
viewHolder.btnCam.setOnClickListener(new Listener(position,criterio));
return convertView;
}
class CriterioViewHolder {
TextView txtCriterio;
ToggleButton tgIrregular;
ImageView btnCam;
}
class Listener implements OnClickListener {
private int position;
Criterio data;
Listener(int position,Criterio data) {
this.position = position;
this.data = data;
}
#SuppressLint("UseValueOf")
#Override
public void onClick(View v) {
// you get the unique id of every item and perform your desired operation
id = data.getId();
}
}
}

Update a ListView using SharedPreference

I want to send multiple rows from the 1st list when the star button is pressed to the 2nd Activity list
My code works only for one single row . For example if I'm pressing the first star ,on my SecondActivity it will be only the first row but if I press the first and the last star ,only the last row it will be shown on the SecondActivity
1st list :http://i.stack.imgur.com/6lD8j.png
1st adapter:
public class AdapterExploreListView extends BaseAdapter implements Filterable {
ArrayList<Track> tracks;
Context context;
private ArrayList<Track> items;
private ArrayList<Track> orig;
public static final String Title = "titleKey";
public static final String Username = "usernameKey";
public AdapterExploreListView(Context context, ArrayList<Track> tracks) {
this.tracks = new ArrayList<>();
this.tracks = tracks;
this.context = context;
getFilter();
}
#Override
public int getCount() {
return tracks.size();
}
#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 rowView = view;
// reuse views
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.row_list_explore, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) rowView.findViewById(R.id.titleTextView);
viewHolder.userName = (TextView) rowView.findViewById(R.id.userNameTextView);
viewHolder.listImageView = (ImageView) rowView.findViewById(R.id.favbutton);
rowView.setTag(viewHolder);
}
// fill data
final ViewHolder holder = (ViewHolder) rowView.getTag();
Track track = tracks.get(i);
holder.title.setText(track.getTitle());
holder.userName.setText(track.getUsername());
holder.listImageView.setImageResource(R.drawable.favoritespic);
holder.listImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,ForthActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
SharedPreferences sp = context.getSharedPreferences("Save",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
String n = holder.title.getText().toString();
String e = holder.userName.getText().toString();
editor.putString(Title, n);
editor.putString(Username, e);
editor.commit();
}
});
// if(!track.getArtworkUrl().equalsIgnoreCase("null"))
// {
// ImageLoader.getInstance().displayImage(track.getArtworkUrl(), holder.listImageView);
// } else {
// ImageLoader.getInstance().displayImage(track.getAvatarUrl(), holder.listImageView);
// }
return rowView;
}
static class ViewHolder {
TextView title;
TextView userName;
ImageView listImageView;
}
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Track> results = new ArrayList<Track>();
if (orig == null) orig = items;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final Track g : orig) {
if (g.getTitle().toLowerCase().contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
items = (ArrayList<Track>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
boolean notifyDataSetChanged = true;
}
}
2nd adapter:
public class AdapterExploreListView2 extends BaseAdapter {
private ArrayList<CustomObject> objects;
Context context;
public static final String Title = "titleKey";
public static final String Username = "usernameKey";
public AdapterExploreListView2(Context context, ArrayList<CustomObject> objects) {
this.objects = new ArrayList<>();
this.objects = objects;
this.context = context;
}
#Override
public int getCount() {
return objects.size();
}
public CustomObject getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View rowView = view;
// reuse views
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.row_list_forthact, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) rowView.findViewById(R.id.titleTV);
viewHolder.userName = (TextView) rowView.findViewById(R.id.userNameTV);
// viewHolder.listImageView = (ImageView) rowView.findViewById(R.id.listImageView);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.title.setText(objects.get(position).getProp1());
holder.userName.setText(objects.get(position).getProp2());
return rowView;
}
static class ViewHolder {
TextView title;
TextView userName;
// ImageView listImageView;
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
boolean notifyDataSetChanged = true;
}
}
2nd activity:
public class ForthActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String Title = "titleKey";
public static final String Username = "usernameKey";
CustomObject obj;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.forthact);
// title = (TextView) findViewById(R.id.etitle);
// username = (TextView) findViewById(R.id.eemail);
sharedpreferences = getSharedPreferences("Save",
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Title)) {
obj.setProp1(sharedpreferences.getString(Title, ""));
}
if (sharedpreferences.contains(Username)) {
obj.setProp2(sharedpreferences.getString(Username, ""));
}
ArrayList<CustomObject> object = new ArrayList<CustomObject>();
AdapterExploreListView2 customAdapter = new AdapterExploreListView2(ForthActivity.this, object);
ListView lv = (ListView) findViewById(R.id.forthlistview);
lv.setAdapter(customAdapter);
}
}
CustomObject class:
public class CustomObject {
private String prop1;
private String prop2;
public CustomObject(String prop1, String prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
public String getProp1() {
return prop1;
}
public String getProp2() {
return prop2;
}
}

How to make listView with multiple clickable textViews

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;
}
}

Categories

Resources