Fragment dialog in particular row in android listview - android

Here is what I have done till now.
// Declaration
NearByFriendsAdapter adapter;
ListView lst_NearByFriends;
// Initialization
lst_NearByFriends= (ListView) getView().findViewById(R.id.lst_NearbyFriends);
list=new ArrayList<>();
for(int i=0;i<=10;i++)
{
NearByFriendsSupportClass nearByFriendsSupportClass=new NearByFriendsSupportClass();
nearByFriendsSupportClass.setFriendsName("Chirag Solanki");
list.add(nearByFriendsSupportClass);
}
adapter=new NearByFriendsAdapter(getActivity(),list,getFragmentManager());
lst_NearByFriends.setAdapter(adapter);
Here is my adapter with holder
holder.img_Infoimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InfoAlertDialog alertDialog=new InfoAlertDialog();
alertDialog.show(fragmentManager, NearByFriendsFragment.class.getName());
}
});
private static class NearbyHolder{
ImageView img_Infoimage;
}
I think this is enough to understand my problem.
Now when any user click on any row in listview, I want to open fragment dialog on that particular row. so please help me what to do to solve my problem.

Register an OnItemClickListener on your listview.

Related

setAdapter is showing NullPointerException

I want to open a dialog for user to choose from multiple items but I get an error when setAdapter() was going to execute...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Dialog cd = new Dialog(Main1.this);
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
ArrayAdapter adapter = new ArrayAdapter<String>(cd.getContext(), R.layout.lvlayout, mobileArray);
ListView listView = (ListView) findViewById(R.id.listviewID);
listView.setAdapter(adapter);
cd.setContentView(R.layout.dialogLayout);
cd.setTitle("MEOW");
cd.show();
}
});
ListView is in Dialog layout. whats Wrong here?
Simply there are two mistakes in your code, you are calling this line without prefixing it with cd. so that you have to do it like this,
ListView listView = (ListView) cd.findViewById(R.id.listviewID);
Another mistake you are doing is calling findViewById before calling setContentView(), This may also raise NPE.
So I suggest you to move this up and re-arrange like this
cd.setContentView(R.layout.dialogLayout);
ListView listView = (ListView) cd.findViewById(R.id.listviewID);
The problem is that your variable listView null is when you call setAdapter() on it. So the call to findViewById() returns null. Maybe you just used a wrong ID.
Just initialize your listview outside of onClick listener.
Initializing it in onCreate() is more preferable.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// initialize
ListView listView = (ListView) findViewById(R.id.listviewID);
}
You have some basic mistakes here.
The major mistake is calling the cd.setContentView(R.layout.dialogLayout); at the end of your code. You need to call before you want to find something in that content layout.
And the other mistake is trying to find the list. You need to get a view to find the list in that view. This might be something like this.
ListView listView = (ListView) cd.getView().findViewById(R.id.listviewID);
However, I do prefer the following solution for your problem. This might be an easier way for what you're trying to achieve.
Here's a nice example of implementing a custom dialogue. You can have a look here.
https://github.com/afollestad/material-dialogs
So, now as you're getting NullPointerException, findViewById is not returning any reference of your ListView I guess. So here by using the library mentioned above you can achieve this quite easily.
// Initialize your dialogue
MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
.title("MEOW")
.customView(R.layout.dialogLayout, true)
.positiveText(R.string.ok)
.negativeText(R.string.cancel)
.onNegative(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
dialog.dismiss();
}
})
.show();
// Now get the view of that dialogue.
View view = dialog.getCustomView();
// Initialize the array adapter
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
ArrayAdapter adapter = new ArrayAdapter<String>(cd.getContext(), R.layout.lvlayout, mobileArray);
// Get the list view and set the adapter
ListView listView = (ListView) view.findViewById(R.id.listviewID);
listView.setAdapter(adapter);

How to remove current row from listview

Now i am going on with the Listview in that i should delete the particular row which i selected here i select the row with the help of button in each row i have a button if i click the 2nd row button 2nd row should be deleted.
public class Adapter extends ArrayAdapter<Data> {
private final View.OnClickListener deleteButton = new View.OnClickListener() {
#Override
public void onClick(View v) {
selected = (Data)v.getTag();
}}
public class DetailsFragment extends Fragment implements Adapter
.Listener {
#Override
public void Deleted(Data list) {
int itemCount = adapter.getPosition(list);
for(int i=0;i<adapter.getCount();i++) {
Data present= adapter.getItem(i);
if(itemCount==i) {
adapter.remove(present);
}
}
}
other approach:
#Override
public void Deleted(Data list) {
for(int i=0;i<adapter.getCount();i++) {
Data present= adapter.getItem(i);
if(Adapter.selected ==present) {
adapter.remove(present);
}
}
}
Tried with many link few below:
http://wptrafficanalyzer.in/blog/deleting-selected-items-from-listview-in-android/
http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
My problem was when ever i try to delete the selected row it deletes from bottom of the list.
Here my position of the data ,id everything is assigned correct but it default removes from bottom
How can i solve this is there any other apporach to solve this problem.
Basicly there are two steps that you need to follow;
Delete the row data from the list.
There supposed to be a list which contains the data that you show in your listview. First you need to delete the data from this list.
Notify the adapter.
Your adapter is connected with the data list. After you implement changes on your data list, you need to inform your adapter about these changes which means;
adapter.notifydatasetchanged()
after you call this method adapter will reload the data to listview without the deleted items.
See here according to one of the tutorials you posted here is how the data is being deleted. After deleting the date you have to notify the adapter just like the tutorial and you are not doing that. and also take a look at you for loop
/** Defining a click event listener for the button "Delete" */
OnClickListener listenerDel = new OnClickListener() {
#Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
};
Hope it helps.... :)

OnItemClick doesn't work with custom adapter

I have a ListView with Custom Adapter. I have seen this thread where people asked if the items in the custom view had a clickable item. And Yes, I have a clickable ImageView in the listrow. So clicking anywhere else(other than that ImageView) should perform some other action. I gave an onItemClickListener to the ListView. However, it doesn't work on first click and works on two-three clicks.
Update:
In my adapter's getView method, I set onClick of ImageView like this:
holder.chatImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff here
}
});
It works fine, and in my activity, I gave onItemClickListener to listView likw this:
onlineListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
//Stuff here
}
});
PS: I didn't explicitly give any focus to anything.
Some time ago I've faced the same problem but with a CheckBox, I've resolve it by creating a onClickListener as member of the Custom Adapter. For example
public MyAdapter extends BaseAdapter{
// ... ..
// ... ...
private OnClickListener imageClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
/// your data .getTag()
// process onClickListener for image
}
};
}
And then, in your getView:
if (convertView == null){
//Create your views and register onClickListener
holder.chatImageView.setOnClickListener(imageClickListener);
}
Add android:focusable="false" to your image in xml.
Hope it helps, Here you have the entire example:

android refresh listview adapter within getview()

I have got a custom list view adapter and an image button in the adapter class. When i click on the image button, the listener should reload the list view. I need to reload the list view within getview() of adapter class. So I need to know other options than using notifyDataSetChanged() in my listActivity class.
Thanks
You want to refresh a cell inside the listview or do you want to refresh the whole listview, if a single row is loaded inside getView() ?
Check this out:
Android ListView Refresh Single Row
Create a static handler inside the activity which calls a method which reloads the listview and send a message to this handler from the adapter whenever required.
handler = new Handler() {
public void handleMessage(Message paramAnonymousMessage) {
switch (paramAnonymousMessage.what) {
case 1:
populateList();
break;
}
}
};
public void populateBill() {
MyBasketAdapter adapter = new MyBasketAdapter(this, basketList);
listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
Inside the adapter class. for example,
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Message msg = Message.obtain();
msg.what = 1;
MyActivity.handler.sendMessage(msg);
}
});
That is very Simple just write a method in your adapter class and call it get view when you deleting or adding anything in your list which you are binding to your adapter.and use notifyDataSetChanged after change in list
public void updateResults(ArrayList<CustomList> results) {
// assign the new result list to your existing list it will work
notifyDataSetChanged();
}

Multiple ListViews, with same content, on different layouts

Multiple ListViews, with same content, on different layouts
So basically what I have is two ListViews that are getting their content from SQLite DB. I have created a BaseActivity below to extend my other activities to access the same data. The problem I ran into is that I cannot display the data because their are two different layout that contain these ListViews, one in a Dialog and the other in a TabWidget, that are both in separate activities.
So basically....
I need to know how to display two ListViews with the same data that are in different activities (one in dialogBox and the other in TabWidget)
The error I am currently getting is from the layout in the SimpleCursorAdapter is only for one of the ListViews and it wont add the other because it cannot find the View
I am not extending ListActivity at any point
Thank you very much in advance. I will be standing by to edit or clarify.
Part of my Base Activity
public class BaseActivity extends Activity
{
private SimpleCursorAdapter contactAdapter;
public static final String ROW_ID = "row_id";
private static ListView study_guide_list_view;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] from = new String[] { "name" };
int[] to = new int[] { R.id.study_guide_item_in_listview };
contactAdapter = new SimpleCursorAdapter(BaseActivity.this, R.layout.study_guide_item_in_listview, null, from, to);
}
}
This segment is where I add the ListView to the TabWidget and it is currently working
study_guide_list_view = (ListView) findViewById(R.id.list);
contactAdapter = getSimpleCursorAdapter();
study_guide_list_view.setAdapter( contactAdapter );
study_guide_list_view.setOnItemClickListener(listview_item_listener);
Where I am trying to add the ListView in the Custom Dialog Box (does not work: error is on study_guide_dialog_list_view.setAdapter( contactAdapter ); )
public OnClickListener save_slide_page_to_guide_btn_listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
TabbedPagesActivity.getListViewAdapter();
dialog = new Dialog(PDFViewerActivity.this);
dialog.setContentView(R.layout.study_guide_custom_dialog_box);
dialog.setTitle("Select a Study Guide");
dialog.setCancelable(true);
study_guide_dialog_list_view = (ListView) findViewById(R.id.list);
contactAdapter = getSimpleCursorAdapter2();
study_guide_dialog_list_view.setAdapter( contactAdapter );
study_guide_dialog_list_view.setOnItemClickListener(listview_item_listener);
Button dialog_ok_btn = (Button) dialog.findViewById(R.id.dialog_ok_btn);//it says cancel though
dialog_ok_btn.setTextSize(20);
dialog_ok_btn.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/AGENCYR.TTF"));
dialog_ok_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
};
Wow so I finally figured it out. The problem lies within the custom Dialog. Instead of calling...
study_guide_dialog_list_view = (ListView) findViewById(R.id.list);
it needs to be....
study_guide_dialog_list_view = (ListView) dialog.findViewById(R.id.list);
If you do not do this the findViewById will return null, hence the NullPointerException

Categories

Resources