Fragment ListView Inflation of new Row to Maintain onItemClick Selection - android

I have a ListView that is within a Fragment. In the onCreateView section I have set a onItemClickListener for the list, which highlights the selected item in the ListView. I have set two ImageButtons that navigate up and down the list. On selection a new Row is inflated that has its TextView's set to the content of the select item (for the purpose of retaining the highlighted selected state). However I am having difficulty adding that item back to the list. The app will not compile due to the line routines.add(selectedPos-1, str); it wants wants int location, Routine object. I believe the issue is with my construction of my SelectedAdapter class, but I have been unable to determine what to change/pass with regard to the Object.
IE:
public SelectedAdapter(Context context, int textViewResourceId,List objects) {
super(getActivity(), R.layout.listview_routines, routines); }
I would greatly appreciate any input as how to correct this issue; as well as any advice if there is a better way to maintain a selected state. Thanks for your help.
Fragment:
public static class FragmentRoutine extends Fragment {
DatabaseHandler db;
private ListView routineListView;
private List<Routine> routines = new ArrayList<Routine>();
ArrayAdapter<Routine> routineAdapter;
Routine longClickedItemRoutines;
private SelectedAdapter selectedAdapter;
public FragmentRoutine() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.routines,
container, false);
db = new DatabaseHandler(getActivity().getApplicationContext());
routineListView = (ListView) rootView.findViewById(R.id.routineList);
registerForContextMenu(routineListView);
db.closeDB();
if (db.getExerciseCount() != 0)
routines.clear();
routines.addAll(db.getAllRoutines());
populateList();
selectedAdapter = new SelectedAdapter(this.getActivity(), 0, routines);
selectedAdapter.setNotifyOnChange(true);
routineListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemRoutines = routines.get(position);
return false;
}
});
routineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
selectedAdapter.setSelectedPosition(position);
}
});
routineListView.post(new Runnable() {
#Override
public void run() {
routineListView.setItemChecked(0, true);
}
});
// move up event handler
ImageButton btnMoveUp = (ImageButton) rootView.findViewById(R.id.btnMoveUp);
btnMoveUp.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
moveUp();
}
});
// move down event handler
ImageButton btnMoveDown = (ImageButton) rootView.findViewById(R.id.btnMoveDown);
btnMoveDown.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
moveDown();
}
});
setHasOptionsMenu(true);
return rootView;
}
// Move selected item "up" in the ViewList.
private void moveUp(){
Routine currentToDoSave = routines.get(selectedAdapter.getSelectedPosition());
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos > 0 ){
routines.remove(selectedPos);
String str = currentToDoSave.getTagName();
//Problem Line Below
routines.add(selectedPos-1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos-1);
}
}
// Move selected item "down" in the ViewList.
private void moveDown(){
Routine currentToDoSave = routines.get(selectedAdapter.getSelectedPosition());
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos < routines.size()-1 ){
routines.remove(selectedPos);
String str = currentToDoSave.getTagName();
routines.add(selectedPos+1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos+1);
}
}
public class SelectedAdapter extends ArrayAdapter<Routine>{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(getActivity(), R.layout.listview_routines, routines);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi
= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
}
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
}else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
return(v);
}
}
private void populateList() {
routineAdapter = new SaveListAdapterT();
routineListView.setAdapter(routineAdapter);
}
public class SaveListAdapterT extends ArrayAdapter<Routine> {
public SaveListAdapterT() {
super(getActivity(), R.layout.listview_routines, routines);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.listview_routines, parent, false);
Routine currentToDoSave = routines.get(position);
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(currentToDoSave.getTagName());
return view;
}
}
}

Related

How to delete item from custom listview on long click in android?

I have a listview with custom base adapter which validate some items in listview. What i want is when i long click on item oflistview, a dialog should open stating "Yes" or "No" and when i tap on "Yes" it should delete that item from adapter.How can i do that.
Here is code of Adapter
private static final String TAG = CDealAppListingAdapter.class.getSimpleName();
private static final String DEAL_CODE = "DealCode";
private static final String HEADER_TEXT = "headerText";
private static final String LOGO_PATH = "logoPath";
private final Context m_Context;// declaring context variable
private final ArrayList<CDealAppDatastorage> s_oDataset;// declaring array list ariable
public CDealAppListingAdapter(Context m_Context, ArrayList<CDealAppDatastorage> mDataList) {
this.m_Context = m_Context;
s_oDataset = mDataList;
}
#Override
public int getCount() {// get total arraylist size
return s_oDataset.size();
}
#Override
public Object getItem(int position) {// get item position in array list
return s_oDataset.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressWarnings("deprecation")
#SuppressLint({"SetTextI18n", "InflateParams"})
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
viewHolder.mProgress = (ProgressBar) convertView.findViewById(R.id.progressBar3);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.m_getBtn.setOnClickListener(new View.OnClickListener() {// onclick getDeal Btn
#Override
public void onClick(View v) {//send to deal detail page onclick getDeal Btn
if (NetworkUtil.isConnected(m_Context)) {
Intent i = new Intent(v.getContext(), CDealAppListingDetails.class);
i.putExtra(DEAL_CODE, s_oDataset.get(position).getM_szsubHeaderText());// get deal code from deal data storage
i.putExtra(HEADER_TEXT, s_oDataset.get(position).getM_szHeaderText());// get deal name from deal dta storage
i.putExtra(LOGO_PATH, s_oDataset.get(position).getM_szLogoPath());
v.getContext().startActivity(i);
} else {
/*here I am getting error*/
CSnackBar.showSnackBarError(v, m_Context.getString(R.string.no_internet_connection), v.getContext());
}
}
});
CDealAppDatastorage m = s_oDataset.get(position);
viewHolder.m_Header.setText(m.getM_szHeaderText());
viewHolder.m_DummyText.setText(m.getM_szDetails());
viewHolder.m_getBtn.setText("GET " + m.getM_szDealValue() + " POINTS");// set deal button text
Picasso.with(m_Context).load(m.getM_szLogoPath()).into(viewHolder.m_logoImage, new Callback() {
#Override
public void onSuccess() {
Log.e(TAG, "OnSuccess Called::");
viewHolder.mProgress.setVisibility(View.INVISIBLE);
}
#Override
public void onError() {
Log.e(TAG, "OnError Called::");
}
});
return convertView;
}
private class ViewHolder {
public TextView m_Header, m_Subheader, m_DummyText;
public ImageView m_logoImage;
public Button m_getBtn;
public ProgressBar mProgress;
}
}
There are two ways to remove item on long press.
1 From class
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
return false;
}
});
2 From adapter
viewHolder.m_getBtn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
return false;
}
});
Sample code structure below;
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
//show dialog asking user option to delete or not
//On OK click, dataset.remove(position);
//adapter.notifyDatasetChanged();
return true;
}
});
Remove selected item from arraylist and referesh listview
s_oDataset.remove(youritemPostion);
notifyDataSetChanged();
after you click YES
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
//call Dialog builder class here.
}
});
First call setOnItemLongClickListener and inside that call Dialogbuilder class ,And inside that dialog builder set your buttons on that dialog using .setPositiveButton or .setNegativeButton methods

Update Listview item and get values using BaseAdapter Android

I have a listview of that have multiple items.I want to update the items value on click of button and want to get the the values of all the items on click of the button.But i am unable to do that.Please help me to get this functionality
Activity code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_ItemValue);
setContentView(R.layout.activity_iween_booking_page);
listView = (ListView) findViewById(R.id.passengerList);
showPassengerListView(passengerList);
}
getValue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(){
}else{
//Here i have to get all the value of the listview items in a array
}
}
});
}
private void showPassengerListView(final String[] passengerList) {
listView.setAdapter(new PassengerListUpdate(passengerList));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public void passengerInformationPopup(final String[] passengerList, final int position) {
final Dialog dialog= new Dialog(MainActivity.this,R.style.Dialog_Fullscreen);
dialog.setContentView(R.layout.passenger_details_dialog);
dialog.show();
setValueDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
isSuccess= true;
if(isSuccess){
dialog.cancel();
//Here i Have to Update the item at position with some value
}
}
});
}
public void goBack(View v) {
finish();
}
class PassengerListUpdate extends BaseAdapter {
String[] ItemValue;
public PassengerListUpdate(String[] text) {
ItemValue = text;
}
public int getCount() {
// TODO Auto-generated method stub
return ItemValue.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View vi;
vi = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView ItemValue;
ItemValue = (TextView) vi.findViewById(android.R.id.text1);
ItemValue.setText(ItemValue[position]);
return (vi);
}
}
Please help me to how to set a items value and how to get the listview values
[Want] to get the the values of all the items on click of the button
To get all values, create a method getAllValues() inside PassengerListUpdate:
public String[] getAllValues() {
return ItemValue;
}
Declare your adapter globally:
PassengerListUpdate myAdapter;
Initialize it in showPassengerListView(String[]):
myAdapter = new PassengerListUpdate(passengerList);
listView.setAdapter(myAdapter);
To get the list of all values on button click:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (myAdapter != null) {
String[] allValues = myAdapter.getAllValues();
}
}
});
I want to update the items value on click of button
To update the values, you can simply reinitialize and set myAdapter:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myAdapter = new PassengerListUpdate(updatedPassengerList);
listView.setAdapter(myAdapter);
}
});
First, in order to get whole the values inside ListView. You can get number of item in adapter, and then get value one by one by its position.
ArrayList<String> results = new ArrayList<String>();
final int count = mMyAdapter.getCount();
for( int i = 0; i < count; ++i ){
String item = mMyAdapter.getItem(i);
results.add(item);
}
return results;
Second, in order to add or update the value of ListView, in general extending ArrayAdapter is much easier than implementing BaseAdapter, because of ArrayAdapter support add / remove / insert method by itself. For example,
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context) {
super(context, 0);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View vi;
vi = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView ItemValue;
ItemValue = (TextView) vi.findViewById(android.R.id.text1);
ItemValue.setText(ItemValue[position]);
return (vi);
}
}
And you can updating adapter's value quiet directly,
mMyAdapter.add("Add Item");
mMyAdapter.remove("Remove Item");
mMyAdapter.insert("Insert Item", 0);
After finishing the jobs, you can call adapter's notifyDatSetChanged() method in order to update the UI, and I think this is more efficient way than setting up the new adapter every time whenever data is updated.
mMyAdapter.notifyDataSetChanged();

How do i determine the items populating listview?

im a noob to android and i am populating a listview with an arrayilst and custom adapter. I want setup my listview onClickListener to execute commands based to the the items populating the listview. the listview is dynamically populated with items from the arraylist. I have tried the position and id parameters with no luck. Any help is greatly appreciated.
How I instantiate listview, arraylist, and adapter
portfoliolist = (ListView) findViewById(R.id.listViewPortfolios);
users = new ArrayList<PortfolioRecord>();
portfoliolist.setAdapter(new UserItemAdapter(this, R.layout.simplerow, users));
portfoliolist.setOnItemClickListener(this);
My Custom Adapter.
public class UserItemAdapter extends ArrayAdapter<PortfolioRecord> {
private ArrayList<PortfolioRecord> users;
public UserItemAdapter(Context context, int textViewResourceId, ArrayList<PortfolioRecord> users) {
super(context, textViewResourceId, users);
this.users = users;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.simplerow, null);
}
PortfolioRecord user = users.get(position);
if (user != null) {
TextView portfolioname = (TextView) v.findViewById(R.id.portfolioname);
TextView currentvalue = (TextView) v.findViewById(R.id.currentvalue);
if (portfolioname != null) {
portfolioname.setText(user.portfolioname);
}
if(currentvalue != null) {
currentvalue.setText("Current Value: " + user.currentvalue );
}
}
return v;
}
}
public class PortfolioRecord {
public String portfolioname;
public String currentvalue;
public PortfolioRecord(String portfolioname, String currentvalue) {
this.portfolioname = portfolioname;
this.currentvalue = currentvalue;
}
}
How i add items to arraylist:
user1 = new PortfolioRecord(pn10, denomination10+portfoliovalue10);
users.add(user1);
I have added Usernull, user1 and user2 to my arraylist. How do i identify these items? I've tried the folowing code with no luck
public void onItemClick(AdapterView parent, View itemClicked, int position,
long id) {
TODO Auto-generated method stub
switch(parent.getId()){
case R.id.listViewPortfolios:
if(portfoliolist.getSelectedItem()==usernull){
openCustomDialog();
}else if(portfoliolist.getSelectedItem()==user1){
whichportfolio=1;
}else if(portfoliolist.getSelectedItem()==user2){
whichportfolio=2;
}
break;}}
No need to implement OnItemSelectedListener. Just use this within the existing OnItemClick method:
public void onItemClick(AdapterView parent, View itemClicked, int position,
long id) {
// TODO Auto-generated method stub
PortfolioRecord user = users.get(position);
// I'm not certain what this code block does...
if(user.equals(usernull)){
openCustomDialog();
}else if(user.equals(user1)){
whichportfolio=1;
}else if(user.equals(user2)){
whichportfolio=2;
}}
supposing your portfolioRecord object has the following: getTypeofUser()
portfoliolist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String typeofUser =users.get(position).getTypeofUser();
if(typeofUser == user1 ){
whichportfolio=1;
}
}
});
I recommend changing to a OnItemSelectedListener and then you could simply fetch the PortfolioRecord with:
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int position, long id) {
// Get the user (PortfolioRecord) that was selected
PortfolioRecord user = users.get(position);
// I'm not certain what this code block does...
if(user.equals(usernull)){
openCustomDialog();
}else if(user.equals(user1)){
whichportfolio=1;
}else if(user.equals(user2)){
whichportfolio=2;
}
}
});

ListView and List Adapter in android change color

I have a list activity which has a list showing results of a query. Well I want to be able to click on each item and the item changes color but it doesn't work. I want the item to remain selecetd state untill "accepte" button is pressed or item is pressed again. I know that is how text boxes work but i prefer to do it my own way.
Here is my code:
public void createList() {
if (ok == 1) {
//hay muachas possibilidades
if (sol.get(i).getMultiseleccion() != 0){
bt2.setVisibility(View.INVISIBLE);
}else {
//solo se clika en una
//lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
bt2.setVisibility(View.VISIBLE);
}
String hd1 = sol.get(i).getDescSolicitud();
tv2.setText(hd1);
ArrayList<SubSolicitud> sub = sol.get(i).getSubSol();
mAdapter = new EventAdapter(this, sub);
setListAdapter(mAdapter);
lv.setTextFilterEnabled(true);
lv.computeScroll();
lv.setDividerHeight(1);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
ok = 1;
//OnListClick(position, arg1);
if (sol.get(i).getMultiseleccion() != 0) {
// multiples respuestas
((EventEntryView)arg1).text1.setTextColor(Color.YELLOW);
guardarRespuesta();
}else {
buscarElementos();
}
}
});
}
// informar el usuario de que hay un error
else
buildAlertDialog();
}
and the other classes are:
public class EventAdapter extends BaseAdapter {
public ArrayList<SubSolicitud> mEvents = null;
public EventAdapter(Context c, ArrayList<SubSolicitud> subsol) {
mContext = c;
mEvents = subsol;
}
public int getCount() {
return mEvents.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
EventEntryView btv;
if (convertView == null) {
btv = new EventEntryView(mContext, mEvents.get(position));
} else {
btv = (EventEntryView) convertView;
String title1 = mEvents.get(position).getDescripcion();
if (title1 != null) {
btv.setText1Title(title1);
}
}
btv.setBackgroundColor(Color.BLACK);
return btv;
}
private Context mContext;
public void clearEvents() {
mEvents.clear();
notifyDataSetChanged();
}
public void addEvent(SubSolicitud e) {
mEvents.add(e);
}
}
public class EventEntryView extends LinearLayout {
// private View inflatedView;
private TextView text1;
// private TextView text2;
public EventEntryView(Context context, SubSolicitud subSolicitud) {
super(context);
this.setOrientation(VERTICAL);
text1=new TextView(context);
text1.setTextSize(20);
text1.setPadding(10, 10, 10, 10);
text1.setTextColor(Color.WHITE);
String t = subSolicitud.getDescripcion();
text1.setText(t);
addView(text1, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void setText1Title(String title1) {
// TODO Auto-generated method stub
text1.setText(title1);
}
}
As you can see I try to get the text in yellow but it doesn't work I click and it doesn't become yellow.
Is there a solution?
thanks
It doesn't work because there is not an EventEntryView for each item in the list - the same EventEntryView is reused to render each.
You need to add something on your SubSolicitud model object to indicate it's been selected (let's say a boolean "selected" property).
In your onItemClicked handler you would toggle this property -
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
// ...
SubSolicitud selectedSubSol = (SubSolicitud)adapterView.getAdapter().getItem(id);
boolean currentValue = selectedSubSol.isSelected();
selectedSubSol.setSelected(!currentValue); // toggle 'selected' on and off
// ...
}
(You also need to fix your EventAdapter getItem method to return mEvents.get(position) for this to work...)
Then in your EventAdapter getView method, you use the value of the "selected" property to render the text color -
public View getView(int position, View convertView, ViewGroup parent) {
// ...
if (mEvents.get(position).isSelected()) {
btv.text1.setTextColor(Color.YELLOW);
} else {
// you have to have an else to set it back to the default
// color, because the view is reused for all list items.
btv.text1.setTextColor(Color.WHITE);
}
// ...
}
This is how you change the color.
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
position = position - listView.getFirstVisibleItem();
((EditText)arg0.getChildAt(position).findViewById(R.id.myTextView)).setTextColor(Color.YELLOW);
}
But if you want to release the item from the color you should iterate through each item of the listview and change it back to normal or you can do it inside the getView() since it is called every time there is action on the listview

Android listview showing only last element

i created custom listview with text and two buttons, i set up arraylist and adapter but my listview is showing every element as last, for ex. if i add 3 elements: "text1","text2","text3" my listview shows "text3", "text3" "text3" and i dont have any idea why.
private ListView lista;
private List<Piosenka> listaPiosenek;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
lista = (ListView) findViewById(R.id.listView1);
lista.setClickable(true);
}
public void update_listy() throws MalformedURLException, IOException
{
final List<Piosenka> listaPiosenek = new ArrayList<Piosenka>();
listaPiosenek.add(new Piosenka("text1"));
listaPiosenek.add(new Piosenka("text2"));
listaPiosenek.add(new Piosenka("text3"));
PiosenkaAdapter adapter = new PiosenkaAdapter(this, listaPiosenek);
lista.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long index)
{
System.out.println("sadsfsf");
}
});
lista.setAdapter(adapter);
}
Edit: PiosenkaAdapter code
public class PiosenkaAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Piosenka> listapiosenek;
public PiosenkaAdapter(Context context, List<Piosenka> listapiosenek) {
this.context = context;
this.listapiosenek = listapiosenek;
}
public int getCount() {
return listapiosenek.size();
}
public Object getItem(int position) {
return listapiosenek.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Piosenka element = listapiosenek.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_element, null);
}
TextView tvTytul = (TextView) convertView.findViewById(R.id.tvTytul);
tvTytul.setText(Piosenka.getTytul());
Button btnPobierz = (Button) convertView.findViewById(R.id.btnPobierz);
btnPobierz.setFocusableInTouchMode(false);
btnPobierz.setFocusable(false);
btnPobierz.setTag(element);
Button btnPlay = (Button) convertView.findViewById(R.id.btnPlay);
btnPlay.setFocusableInTouchMode(false);
btnPlay.setFocusable(false);
btnPlay.setOnClickListener(this);
btnPlay.setTag(element);
// btnRemove.setId(position);
return convertView;
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnPobierz:
Piosenka entry = (Piosenka) view.getTag();
listapiosenek.remove(entry);
notifyDataSetChanged();
break;
case R.id.btnPlay:
entry = (Piosenka) view.getTag();
listapiosenek.remove(entry);
notifyDataSetChanged();
break;
}
}
}
Try this...
lista.setAdapter(adapter);
adapter.notifyDataSetChanged();
Can you paste you PiosenkaAdapter's code?
I don't know your language, but the Piosenka variable is fetched correctly in getView()
Piosenka element = listapiosenek.get(position);
But this looks strange to me
TextView tvTytul = (TextView) convertView.findViewById(R.id.tvTytul);
tvTytul.setText(Piosenka.getTytul());
Piosenka.getTytul() looks to me as a static method call, where you should do a regular method call to element.getTytul() instead.

Categories

Resources