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;
}
}
Related
I am new on Android, when i filter listview with baseadapter than i got true results but after click on this item,i can't get related values of that item.
Like original list : A - B -C -D
After filtered list : B - C - D
when i click "B" in filtered list, program streaming A's value. I have no idea, how to handle, I tried many solutions, but i could'n make it. I need help.
My adapter class
public class MyListAdapter extends BaseAdapter implements Filterable {
MediaPlayer mPlayer=new MediaPlayer() ;
private String[] gelenLink;
private int[] IMAGE_ID = {R.drawable.police};
private List<String>originalData = null;
private List<String>filteredData = null;
private LayoutInflater mInflater;
private ItemFilter mFilter = new ItemFilter();
public MyListAdapter(Context context, List<String> data, String[] linkler) {
this.filteredData = data ;
this.originalData = data ;
this.gelenLink= linkler;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView=null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.radio_listview, null);
holder= new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
holder.img.setImageResource(IMAGE_ID[0]);
holder.title = (TextView) convertView.findViewById(R.id.list_item_text);
holder.button = (Button) convertView.findViewById(R.id.list_item_btn);
holder.stop_button = (Button) convertView.findViewById(R.id.list_item_stop_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(filteredData.get(position));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
radioLinks(gelenLink[position]);
} catch (IOException e) {
e.printStackTrace();
}
}
public void radioLinks(String city) throws IOException {
if(mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
}
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(city);
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
});
}
});
holder.stop_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.stop();
mPlayer.reset();
}
});
return convertView;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}}
My viewHolder class :
public class ViewHolder {
ImageView img;
TextView title;
Button button;
Button stop_button;}
MainActivity part:
ArrayList<String> radioname = new ArrayList<String>();
ArrayList<String> tmp = new ArrayList<String>();
private String[] radiolinks;
MyListAdapter adapter;
EditText editText;
/////////////////
//onPostExecute
////////////////
editText = (EditText) findViewById(R.id.search_tv);
adapter = new MyListAdapter(getApplicationContext(), tmp, radiolinks);
listView1.setAdapter(adapter);
listView1.setTextFilterEnabled(true);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
Change this:
public MyListAdapter(Context context, List<String> data, String[] linkler) {
this.filteredData = data ;
this.originalData = data ;
this.gelenLink= linkler;
mInflater = LayoutInflater.from(context);
}
To this:
public MyListAdapter(Context context, List<String> data, String[] linkler) {
super(context, 0, data);
this.filteredData = data ;
this.originalData = data ;
this.gelenLink= linkler;
mInflater = LayoutInflater.from(context);
}
Also, once you passed the data to the adapter, getView is the only function you need to implement:
public View getView(int position, View convertView, ViewGroup parent);
Here is a full example of a ArrayAdapter:
public class MyAdapterWithHolder extends ArrayAdapter<FileInfo> {
ArrayList<FileInfo> dataSet;
LayoutInflater inflater;
ListView listView;
public MyAdapterWithHolder(Context context, ArrayList<FileInfo> objects, ListView listView) {
super(context, 0, objects);
dataSet = objects;
inflater = LayoutInflater.from(context);
this.listView = listView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder rowHolder = null;
if( convertView == null ){//create new row
convertView = inflater.inflate( R.layout.row, null );
rowHolder = new Holder();
rowHolder.image = (ImageView) convertView.findViewById( R.id.imageView1 );
rowHolder.name = (TextView) convertView.findViewById( R.id.folderName1 );
rowHolder.count = (TextView) convertView.findViewById( R.id.itemCount1 );
rowHolder.date = (TextView) convertView.findViewById( R.id.date1 );
convertView.setTag(rowHolder);
}else{
rowHolder = (Holder) convertView.getTag();
}
final FileInfo p = getItem( position );
rowHolder.name.setText(p.fileName);
rowHolder.date.setText(p.creationDate);
rowHolder.image.setImageResource(p.icon);
if( p.isDirectory() )
rowHolder.count.setText( p.size == -1 ? "" : ( "" + p.size + " items" ) );
else
rowHolder.count.setText( p.fileSize );
return convertView;
}
static class Holder{
ImageView image;
TextView name;
TextView count;
TextView date;
}
}
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?
Can you give me an example of passing data from ListView to another ListView.
For example I have my MainActivity.java that has ListView and when i delete item on it. i want to achieve is to pass and add it to another ListView activity.
MainActivity.java | OnitemClick
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String member_name = rowItems.get(position).getMember_name();
int product_icons = rowItems.get(position).getProfile_pic_id();
String status = rowItems.get(position).getStatus();
// passing data
Intent i = new Intent(getBaseContext(), CarActivity.class);
i.putExtra("member_name", member_name);
i.putExtra("product_icons", product_icons);
i.putExtra("status", status);
RowItem item = rowItems.get(position);
rowItems.remove(item);
newadapter = new CustomAdapter(this, rowItems);
mylistview.setAdapter(newadapter);
newadapter.notifyDataSetChanged();
Toast.makeText(this.getApplicationContext(),
"Added to Cart: " + member_name, Toast.LENGTH_SHORT).show();
}
CartActivity.java | onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
String member_names = extras.getString("member_name");
int product_icons = extras.getInt("product_icons");
String status = extras.getString("status");
}
rowItems = new ArrayList<RowItem_cart>();
RowItem_cart item = new RowItem_cart(member_names, product_icons, status);
rowItems.add(item);
mylistview = (ListView) findViewById(R.id.list);
mylistview.setItemsCanFocus(true);
//
final CustomerAdapter_Cart adapter = new CustomerAdapter_Cart(this,
rowItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
mylistview.setScrollingCacheEnabled(false);
mylistview.invalidateViews();
CustomAdapter_cart
public class CustomerAdapter_Cart extends BaseAdapter {
Context context;
List<RowItem_cart> rowItems;
public CustomerAdapter_Cart(Context context, List<RowItem_cart> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
public CustomerAdapter_Cart() {
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
ImageView product_icons;
TextView member_name;
TextView status;
Button addtocart;
EditText txtQuantity;
TextView lblQty;
}
ViewHolder holder = null;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_cart, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.product_icons = (ImageView) convertView
.findViewById(R.id.product_icons);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.addtocart = (Button) convertView
.findViewById(R.id.btnaddtocart);
holder.txtQuantity = (EditText) convertView
.findViewById(R.id.txtQuantity);
holder.lblQty = (TextView) convertView.findViewById(R.id.lblQty);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RowItem_cart row_pos = rowItems.get(position);
holder.product_icons.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
holder.addtocart.setText(" add to cart");
return convertView;
}
}
RowItem_cart
public class RowItem_cart {
private String member_name;
private int profile_pic_id;
private String status;
public RowItem_cart(String member_name, int profile_pic_id, String status) {
this.member_name = member_name;
this.profile_pic_id = profile_pic_id;
this.status = status;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
public int getProfile_pic_id() {
return profile_pic_id;
}
public void setProfile_pic_id(int profile_pic_id) {
this.profile_pic_id = profile_pic_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
In your MainActivity you probably have an onItemSelected() listener. In there you can get the item that was removed from the active item list. Assuming you have a field for the deleted items list you would simply call add() on that list with the item you just deleted.
Without some of your actual code that's about as far as we're going to be able to take you ;)
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 am android developer, I have a custom listview with a checkbox. This layput also contain a delete button. I want when I click on cheakbox all the item in a particular row is selected and on click of delete it is deleted.
The problem is when I click on delete button I get a list of +1 row value.
Initially I already define :
int position=0;
btmsgdelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("request send for message delete");
for(Message msg:almsg) {
if(msg.isSelected()) {
CheckBox chk = (CheckBox)findViewById(R.id.checkBox1);
System.out.println("msg is selected");
msgid=almsg.get(position).getEmpid();
System.out.println(msgid);
empname=almsg.get(position).getEmpname();
System.out.println(empname);
msgheader=almsg.get(position).getHeader();
System.out.println(msgheader);
}
}
Try this sample code
public class MainActivity extends Activity {
private int textViewResourceId;
private ArrayList<CompareListData> searchResults;
private ListView lst;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchResults = GetSearchResults();
lst = (ListView) findViewById(R.id.list);
findViewById(R.id.delete).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < searchResults.size(); i++) {
if (searchResults.get(i).getSelected()) {
searchResults.remove(i);
}
}
lst.setAdapter(new Adapter(MainActivity.this, textViewResourceId,
searchResults));
}
});
System.out.println("size " + searchResults.size());
lst.setAdapter(new Adapter(MainActivity.this, textViewResourceId,
searchResults));
}
private ArrayList<CompareListData> GetSearchResults() {
ArrayList<CompareListData> results = new ArrayList<CompareListData>();
CompareListData sr1 = new CompareListData();
sr1.setName("John Smith");
results.add(sr1);
sr1 = new CompareListData();
sr1.setName("Jane Doe");
results.add(sr1);
sr1 = new CompareListData();
sr1.setName("Steve Young");
results.add(sr1);
sr1 = new CompareListData();
sr1.setName("Fred Jones");
results.add(sr1);
return results;
}
}
CompareListData.java
public class CompareListData {
String name;
boolean selected;
public String getName() {
return name;
}
public void setName(String Name) {
name = Name;
}
public boolean getSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
Adapter.java
public class Adapter extends BaseAdapter{
public static int count = 0;
public LayoutInflater inflater;
public static ArrayList<CompareListData> selectedId;
public ArrayList<CompareListData> listObjects;
Context contex;
public Adapter(Context context, int textViewResourceId,
ArrayList<CompareListData> objects) {
super();
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listObjects = objects;
this.contex = context;
}
public static class ViewHolder
{
TextView txtViewLoanName;
CheckBox chkSelected;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView==null)
{
final ViewHolder holder = new ViewHolder();
view = inflater.inflate(R.layout.row_comparelist, null);
holder.txtViewLoanName= (TextView) view.findViewById(R.id.rowcomparelist_tv_loanname);
holder.chkSelected= (CheckBox) view.findViewById(R.id.rowcomparelist_chk_selected);
holder.chkSelected.setId(position);
holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CompareListData element = (CompareListData) holder.chkSelected.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(holder);
holder.chkSelected.setTag(listObjects.get(position));
}
else{
view = convertView;
((ViewHolder) view.getTag()).chkSelected.setTag(listObjects.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtViewLoanName.setText(listObjects.get(position).getName());
holder.chkSelected.setChecked(listObjects.get(position).getSelected());
return view;
}
public int getCount() {
return listObjects.size();
}
public Object getItem(int position) {
return listObjects.get(position);
}
public long getItemId(int position) {return position;
}
}