I want to make OnItemClickListener to a dynamic listview. The listview shouldn't have a fixed size. Instead it's the size of the rows in a database. But how do I create the listener concidering I can't use the current method I have since I don't know the size of the listview.
THE PROBLEM IS IN THE INNER CLASS if(position==0) { ... }
public class VisaUtgiftFragment extends Fragment {
private MainActivity ui;
private TextView tvDatabase;
private MyDBHandler dbHandler;
private ListView lvUtgift;
public VisaUtgiftFragment() {
// Required empty public constructor
}
public void setActivity(MainActivity ui){
this.ui = ui;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_visa_utgift, container, false);
tvDatabase = (TextView)view.findViewById(R.id.tvDatabaseTest);
dbHandler = new MyDBHandler(getActivity(), null, null, 1);
tvDatabase.setText(dbHandler.databaseToString());
lvUtgift = (ListView)view.findViewById(R.id.lvUtgift);
// lvUtgift.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, [the rows in the database]));
lvUtgift.setAdapter(new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1,
dbHandler.getAllItemsInCursor(),
new String[]{"_titel"},
new int[]{android.R.id.text1},
0));
lvUtgift.setOnItemClickListener(new ListViewClicked());
return view;
}
private class ListViewClicked implements android.widget.AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0) { //Should be the clicked listview and execute the code
Cursor cursor = (Cursor) lvUtgift.getItemAtPosition(position);
String string = cursor.getString(cursor.getColumnIndexOrThrow("_titel"));
Toast.makeText(getActivity().getApplicationContext(), string, Toast.LENGTH_SHORT).show();
ui.dVisaInkomst();
}
}
}
}
Remove if condition from onClickItem.
private class ListViewClicked implements android.widget.AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor)lvUtgift.getItemAtPosition(position);
if(cursor != null){
String string = cursor.getString(cursor.getColumnIndexOrThrow("_titel"));
Toast.makeText(getActivity().getApplicationContext(), string, Toast.LENGTH_SHORT).show();
ui.dVisaInkomst();
}
}
}
Related
I have 1 ImageView , 1 textview and 1 seekbar in listview items. Seekbar by default visiility is GONE. Now i want to set visibility of seekbar as per database value. If database value is YES then it will display seekbar on long press and if its No then it will do nothing on long press. Currently if i long press on any item it is showing seekbar.
ApplianceAdapter
public class ApplianceAdapter extends CursorAdapter {
public ApplianceAdapter(Context context, Cursor c) {
super(context, c, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.appliancelist, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtApplianceName = (TextView) view.findViewById(R.id.txtApplianceName);
final LinearLayout btnApp = (LinearLayout) view.findViewById(R.id.btnApp);
final SeekBar slider = (SeekBar) view.findViewById(R.id.slider);
String appliance = cursor.getString(cursor.getColumnIndexOrThrow("AppName"));
String dimmer = cursor.getString(cursor.getColumnIndexOrThrow("Dimmer"));
txtApplianceName.setText(appliance);
if (dimmer.contains("YES")){
btnApp.setLongClickable(true);
}else if (dimmer.contains("NO")){
btnApp.setLongClickable(false);
}
}
}
Appliance Activity
public class Appliance extends AppCompatActivity {
ListView listView;
ArrayList<HashMap<String,String>> applianceList;
DBOpenHelper db;
String room;
String type;
Cursor appliance;
ApplianceAdapter applianceAdapter;
SeekBar slider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appliance);
Intent intent = getIntent();
String r = intent.getStringExtra("roomname");
room = r.replace("{name=", "").replace("}","");
db = new DBOpenHelper(this);
listView = (ListView) findViewById(R.id.listAppliance);
appliance = db.getAppliance(room);
applianceAdapter = new ApplianceAdapter(this, appliance);
listView.setAdapter(applianceAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
slider = listView.getChildAt(position).findViewById(R.id.slider);
slider.setVisibility(View.VISIBLE);
return false;
}
});
}
}
You can try
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
youradapter.lastclicked=position;
youradapter.notifyDataSetChanged();
return false;
}
});
And in your adapter,
if (dimmer.contains("YES") && cursor.getPosition()==lastclicked){//initially lastclicked = -1
slider.setVisibility(View.VISIBLE);
}else if (dimmer.contains("NO")){
slider.setVisibility(View.GONE);
}
I have a ListFragment's list that contains complex layouts as items, these items contain custom spinners (MaterialSpinner).
MaterialSpinner chooseSubjectSpinner = adapter.getItem(position).findViewById(R.id.myCustomSpinner);
chooseSubjectSpinner.setAdapter(spinnerAdapter);
I've tried getChildAt(position), adapter.getItem(postition), adapter.getView(position, null, parent), and list.get(position) with the added prefix of .findViewById(R.id.myview), but it doesn't work. (It either returns NullPointerException at runtime or doesn't do anything at all!).
EDIT:
if you guys want the whole fragment code:
private ArrayList<View> listViews = new ArrayList<>();
private ViewAdapter adapter;
private List<String> spinnerItems = new ArrayList<>();
private MaterialSpinnerAdapter<String> spinnerAdapter;
public FragmentUnitTest() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_unit_test, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState){
spinnerItems.add("Hey!");
spinnerItems.add("Hello!");
spinnerItems.add("Hi!");
spinnerAdapter = new MaterialSpinnerAdapter<>(getContext(), spinnerItems);
adapter = new ViewAdapter(listViews, view.getContext());
setListAdapter(adapter);
addView(R.layout.cardview_with_spinner);
MaterialSpinner spinner = adapter.getItem(0).findViewById(R.id.custom_spinner);
RLogger.e("FragmentUnitTest", spinner);
spinner.setAdapter(spinnerAdapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Clicked: " + position + " .", Toast.LENGTH_SHORT).show();
}
Create a Custom spinner like this:
public class PTCustomSpinner extends ArrayAdapter {
public PTCustomSpinner(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
int count = super.getCount();
return count>0 ? count-1 : count ;
}
Handle it like below // BELOW XML LAYOUT CONTAINS ONLY TEXTVIEW WITH ID "temp"
PTCustomSpinner spinnerArrayAdapter = new PTCustomSpinner(PTExceptionMainPage.this, R.layout.xml_with_only_textview);
spinnerArrayAdapter.addAll(team_array);
spinnerArrayAdapter.add("Pick Team");
spinnerTeam.setAdapter(spinnerArrayAdapter);
spinnerTeam.setSelection(spinnerArrayAdapter.getCount());
spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (spinnerTeam.getSelectedItem() != "Pick Team") {
teamIdSelected = apiResponseTeam.get(i).getTeamID();
}
else if( spinnerTeam.getSelectedItem().equals("Pick Team"))
{
View v = spinnerTeam.getSelectedView();
((TextView) v).setTextColor(getResources().getColor(R.color.ash));
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I have created a class PickedDropsAdapter that extends ArrayAdapter for a listView that lists Objects from a List
Adding and removing basically works fine except that position in getView is always 0 and I just can't figure out, why.
I found a few similar question but none of the answers was really helping
Maybe important to mention is that listView is in a Fragment which again is in a ViewPager with two pages.
PickedDropsAdapter.java
public class PickedDropsAdapter extends ArrayAdapter {
private LayoutInflater myInflater;
private List<PickedDrop> pickedList;
public PickedDropsAdapter(Context context, int resource, List objects){
super(context, resource, objects);
myInflater = LayoutInflater.from(context);
setData(objects);
}
public void setData(List list){
this.pickedList = list;
}
#Override
public int getPosition(Object item) {
return super.getPosition(item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView profileName;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.picked_drop, null);
profileName = (TextView)convertView.findViewById(R.id.profileName);
Log.i("TAG", "Name in adapter: " + pickedList.get(position).name);
Log.i("TAG", "Postion in adapter: " + position);
profileName.setText(pickedList.get(position).name);
pickedList.get(position).makeActive(convertView);
}
return convertView;
}
}
PickedDrop
public class PickedDrop extends Fragment {
int id, listLoc;
String name;
RelativeLayout pickedLayout;
PickedDropsFrag parentFrag = null;
public static PickedDrop newInstance(String nameParam, int idParam) {
final PickedDrop fragment = new PickedDrop();
fragment.name = nameParam;
Log.i("TAG", "Drop name: " + fragment.name);
fragment.id = idParam;
return fragment;
}
public PickedDrop() {
// Required empty public constructor
}
public void openDrop(){
MainActivity mainActivity = (MainActivity)parentFrag.getActivity();
mainActivity.openDrop(this);
}
public void openDrop(int i){
listLoc = i;
MainActivity mainActivity = (MainActivity)parentFrag.getActivity();
mainActivity.openDrop(this);
}
public void makeActive(View convertView){
pickedLayout = (RelativeLayout)convertView.findViewById(R.id.pickedLayout);
}
}
Part of PickedDropsFrag (The fragment that contains the listView)
public class PickedDropsFrag extends Fragment {
private OnFragmentInteractionListener mListener;
PickedDropsAdapter pickedAdapter;
ListView pickedListView;
List<PickedDrop> pickedList;
public PickedDropsFrag() {
// Required empty public constructor
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pickedListView = (ListView)getView().findViewById(R.id.pickedListView);
pickedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
PickedDrop drop = (PickedDrop) adapterView.getItemAtPosition(i);
drop.openDrop(i);
}
});
pickedList = new ArrayList<>();
pickedAdapter = new PickedDropsAdapter(
getActivity(),
R.layout.picked_drop,
pickedList
);
pickedListView.setAdapter(pickedAdapter);
}
public void addToPicked(PickedDrop drop){
drop.parentFrag = this;
//pickedList.add(drop);
pickedAdapter.add(drop);
drop.listLoc = pickedList.size() - 1;
//pickedAdapter.notifyDataSetChanged();
drop.openDrop();
}
That should be everything important, I hope I didn't forget anything.
Update following methods in your Adapter:
public class PickedDropsAdapter extends ArrayAdapter {
#Override
public int getCount() {
return pickedList.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView profileName;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.picked_drop, null);
}
profileName = (TextView)convertView.findViewById(R.id.profileName);
Log.i("TAG", "Name in adapter: " + pickedList.get(position).name);
Log.i("TAG", "Postion in adapter: " + position);
profileName.setText(pickedList.get(position).name);
pickedList.get(position).makeActive(convertView);
return convertView;
}
}
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;
}
}
}
The problem is as follows: When I click on the row, dialog opens where I enter new row value and click ok to save changes. The row is updated, but I need to exit view and go back again to see changes. How can I refresh view automatically when the sava button is clicked?
Here is my code for Dialog:
class EditListItemDialog extends Dialog implements View.OnClickListener {
MyDB dba;
private View editText;
private DiaryAdapter adapter;
private SQLiteDatabase db;
public EditListItemDialog(Context context) {
super(context);
dba = new MyDB(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
dba.open();
}
private List<String> fragment_monday;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Position is the number of the item clicked
//You can use your adapter to modify the item
adapter.getItem(position); //Will return the clicked item
}
public EditListItemDialog(Context context, DiaryAdapter adapter, int position) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
dba = new MyDB(context);
}
#Override
public void onClick(View v) {
fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
dismiss();
try {
saveItToDB();
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveItToDB() {
dba.open();
dba.updateDiaryEntry(((TextView) editText).getText().toString(), 2);
dba.close();
((TextView) editText).setText("");
}
}
And here is the code where I want to refresh view instead of doing it manually:
public class Monday extends ListActivity {
private SQLiteDatabase db;
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
}
public String title;
public String content;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
public class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
ListView list = getListView();
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(Monday.this, null, position).show();
return true;
}
});
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
#Override
public int getCount() {return fragment_monday.size();}
public MyDiary getItem(int i) {return fragment_monday.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}
I hope it's clear enough what I am looking for. Where my list is, I want the list to be refreshed automatically after updating row so the new content of the row shows up immediately instead of navigating to other view and going back. I will be appreciated for your help.
At some point, after you call EditListItemDialog and it finishes, you need to run NotifyDataSetChanged() on your listview adapter. onResume() is a good place to do it,