Contacts LoaderManager and AdapterView OnClick - java

I am trying to attach a basic onClick event to a list of contacts from the device. The list of contacts is generated correctly. A summary of my code is as follows:
public class Contacts extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor>,
AdapterView.OnItemClickListener
{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list_view);
//initialise loader
getSupportLoaderManager().initLoader(CONTACTS_LOADER_ID, null, this);
mContactsList = (ListView) findViewById(android.R.id.list);
//Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.contacts_list_item,
null,
FROM_COLUMNS, TO_IDS,
0);
//Sets the adapter for the ListView
mContactsList.setAdapter(mCursorAdapter);
}
}
//Followed by all the loader functions
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {}
...
public void onItemClick (AdapterView<?> parent, View view, int position, long id){
Toast.makeText(this,
"hello world",
Toast.LENGTH_SHORT).show();
}
My onItemClick does not however work. Would I first have to create an instance of OnItemClickListener. I have tried to do this inside the onCreate method and outside and neither work.
Subsequently I have tried the following within my onCreate method
mContactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
"hello world",
Toast.LENGTH_SHORT).show();
}
});

public class Contacts extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>,
public void onCreate(Bundle savedInstanceState) {
// Set screen content
setContentView(R.layout.main_screen_activity_layout);
// Find the list view
ListView mContactsList = findViewById(R.id.ID_OF_YOUR_LISTVIEW);
// Set the click listener
mContactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
"hello world",
Toast.LENGTH_SHORT).show();
}
});
// Create your adapter and set the adapter to your ListView via mContactsList.setAdapter();
}

Related

how to access from onCreateCiew to setOnItemSelectedListener method variable

I am not an expert in android studio with java and I would like to know how to obtain the value declared within the setOnItemSelectedListener method (variable option_name). I have declared a spinner which shows a series of options, what I want is to extract the option to use it in the onCreateView for later insert it in a database together with other texts that are obtained from editText. Greetings
my code:
public class genera_debate extends Fragment {
EditText date,time_hour;
TextView test;
Spinner list_of_interest;
Button PostDiscussion;
public String name_perfil,alias_perfil,option_interest,option_name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_genera_debate,container,false);
test =view.findViewById(R.id.test);
PostDiscussion= view.findViewById(R.id.button4);
list_of_interest =view.findViewById(R.id.subject_of_interest);
String [] subject = {"Tema de interés","Política","Deporte","Animales","Videojuegos","Economía","Medicina","Cultura","Ciencia","Tecnología","Música","Otros"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, subject);
list_of_interest.setAdapter(adapter);
list_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
option_name = (String ) parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}

onListitemClick is never used

I am using onListItemClick with simple_list_item_1
public class DrinkCategoryActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listDrinks = getListView();
ArrayAdapter<Drinks> listAdapter = new ArrayAdapter<Drinks>(this,
android.R.layout.simple_list_item_1,
Drinks.drinks);
listDrinks.setAdapter(listAdapter);
}
#Override
public void onListItemClick(ListView listView, View itemview, int position, int id){
Intent intent = new Intent(this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, id);
startActivity(intent);
}
}
It says that onListItemClick is never used
When I open my app it doesn't respond to click.
I don't want to use a new layout activity.
I am using list activity which doesn't always require layout
You should use setOnItemClickListener
setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = list.getItemAtPosition(position);
}
});
you can try below
public class DrinkCategoryActivity extends Activity implements AdapterView.OnItemClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_drink_category);
ListView listDrinks = (ListView)findViewById(R.id.your_list_view_id);
ArrayAdapter<Drinks> listAdapter = new ArrayAdapter<Drinks>(this,
android.R.layout.simple_list_item_1,
Drinks.drinks);
listDrinks.setAdapter(listAdapter);
listDrinks.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Callback logic here for clicked items
}
...
}
Try this
listDrinks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Just add
public class DrinkCategoryActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listDrinks = getListView();
ArrayAdapter<Drinks> listAdapter = new ArrayAdapter<Drinks>(this,
android.R.layout.simple_list_item_1,
Drinks.drinks);
listDrinks.setAdapter(listAdapter);
// Add this block
listDrinks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, id);
startActivity(intent);
}
});
}
Remove
#Override
public void onListItemClick(ListView listView, View itemview, int position, int id){
Intent intent = new Intent(this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, id);
startActivity(intent);
}

getListView() not working showing null pointer exception

I have a ListFragment OnItemClick is not working; it's basically not getting called. I have been unable to find any errors in my code. Is this somehow a problem with getListView?
I've explored Google and StackOverflow but did not find a solution. Here's my code:
public class FragmentSongs extends ListFragment implements OnFragmentCreatedListener,OnItemClickListener {
static ListView lv;
private static final String TAG = FragmentSongs.class.getSimpleName();
MediaMetadataRetriever metaRetriver1;
Vector<ForPLaylist> gtuMcaBean= new Vector<ForPLaylist>();
Forplaylistadapter adaptor;
String artist="";
static int i=0,f=0,k=0;;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
trial t=new trial();
t.onCreate();
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.playlist, container,false);
metaRetriver1 = new MediaMetadataRetriever();
lv=(ListView) root.findViewById(android.R.id.list);
//data calculation here
lv.setAdapter(adaptor);
// lv = getListView();//showing error here if i call it
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
int songIndex=position;
Intent in = new Intent(getActivity(), AndroidBuildingMusicPlayerActivity.class);
in.putExtra("songIndex",songIndex);
startActivityForResult(in, 100);
closefragment();
}
});
return root;
}
OnFragmentCreatedListener listener;
private void closefragment() {
getActivity().getFragmentManager().popBackStack();
}
#Override
public void onFragmentCreated(ArrayList<String> msg) {
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int songIndex=position;
Intent in = new Intent(null, AndroidBuildingMusicPlayerActivity.class);
in.putExtra("songIndex",songIndex);
startActivityForResult( in,100);
finish();
}
private void finish() {
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
if(savedInstanceState != null) {
Log.d(TAG, "saved variable number: " + savedInstanceState.getInt("number"));
}
super.onActivityCreated(savedInstanceState);
// lv=getListView();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// lv = getListView();
}
}
try move your getListView();to onActivityCreated and make sure you call super.onActivityCreated(savedInstanceState) first
I think you should check the code in onItemClick of ListFragment (not the code of onItemClick inside onCreateView method):
Intent in = new Intent(null, AndroidBuildingMusicPlayerActivity.class);
Just change it to:
Intent in = new Intent(getActivity(), AndroidBuildingMusicPlayerActivity.class);

Add clickableItems to Listview

I wanna add clickableItems to Listview. But my problem is, that my class extands Fragment and i can not do that easily. Additionaly i wanna by clicking the item change the page to another page. My code is:
public class MyCoursesFragment extends Fragment implements OnItemClickListener, OnClickListener {
private static final String TAG = MyCoursesFragment.class.getName();
private ListView listView;
String listItem[]={"Vl1", "VL2"};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.mycourses_layout, container, false);
listView = (ListView) layout.findViewById(R.id.Course);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent = new Intent(getActivity().getBaseContext(), DocumentFragment.class);
intent.putExtra("id", "1");
listView.getContext().startActivity(intent);
FrontPageFragment.this.startActivity(intent);
}
});
return layout;
}
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
}
}
can you guys pls help me.
THX
Change this line because your context is wrong:
listView.getContext().startActivity(intent);
FrontPageFragment.this.startActivity(intent);
You need to start a new activity in the following way:
Intent intent = new Intent(getActivity(),DocumentFragment.class);
startActivity(intent);
What you can to do is set up a callback Interface and control everything from your parent Activity.
Your Activity should implement the interface and your Fragment should call those methods when your View or list item gets clicked.
Something along the lines of:
interface
public interface OnListItemClickCallback {
public void onListItemShortClick(View view);
public void onListItemLongClick(View view);
}
Fragment
public MyListFragment extends ListFragment
implements OnItemClickListener, OnItemLongClickListener {
private OnListItemClickCallback clickCallback;
...
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// make sure the parent activity implements your interface
try
{
clickCallback = (OnListItemClickCallback) activity;
}
catch (ClassCastException e)
{
Log.e(TAG, "Parent Activity doesn't implement OnListItemClickCallback");
throw new ClassCastException(activity.toString()
+ " must implement OnListItemClickCallback");
}
}
#Override
public View onCreateView(Bundle b) {
...
getListView().setOnListItemClickListener(this):
getListView().setOnListItemLongClickListener(this):
}
#Override
public void onItemClick(...) {
clickCallback.onListItemShortClick(view)
}
// same idea for long click if you want it
}
Activity
public MyActivity extends Activity
implements OnListItemClickCallback {
...
#Override
public void onListItemShortClick(View view) {
// do something like replace your fragment
}
#Override
public void onListItemLongClick(View view) {
// do something else
}
}

how to create onitemclick listener to open another listview

hi here i displayed methods that are present in the class so now i want when i click
that methods it has to displays it's parameters in another layout so for that i
created another activity and layout but it is not working to connect with with
onitemclick.
public class Listview extends Activity{
private ListView listView;
// public ItemClickListener clicklistener = new ItemClickListener();
private ListView lv;
public static ArrayList<String> your_array_list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
lv = (ListView) findViewById(R.id.listView1);
Button backButton=(Button)findViewById(R.id.methodbackbutton1);
((ListView) findViewById (R.id.parameter_listview)).setOnItemClickListener (this);
OnItemClickListener clicklistener;
//listView.setOnItemClickListener(clicklistener);
//listView = (ListView) findViewById(R.id.parameter_listview);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
// In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View.
TextView txtName = (TextView)v.findViewById(R.id.paramlistView1);
String name = txtName.getText().toString();
}
}
);
backButton.setOnClickListener(new OnClickListener() {
You have to use intent for swithcing activity and use putExtra to pass value from current activity to next.
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
// In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View.
TextView txtName = (TextView)v.findViewById(R.id.paramlistView1);
String name = txtName.getText().toString();
Intent in = new Intent(this, NextActivity.class);
in.putExtra("name" , name);
startActivity(in);
}
}
);
In Second activity onCreate() you have to get value from intent like below:
String name = getIntent().getString("name");
Try out as below:
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
// In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View.
TextView txtName = (TextView)v.findViewById(R.id.paramlistView1);
String name = txtName.getText().toString();
startActivity(new Intent(this,SecondActivity.class).putExtra("value",name)));
}
}
);
In your second activity layout get the value from Intent in onCreate() and show in whichever View you want as below:
Write below code in your second activities onCreate()
String data=getIntent().getString("value");

Categories

Resources