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);
}
Related
This is my code. What can I change?
Notification fragment
public class NotificationsFragment extends Fragment {
private ArrayList<String> arrayList;
private ListView listView;
private RequestQueue queue;
private ArrayAdapter arrayAdapter;
private List<Earthquake> quakeList;
private int contentView;
private View view;
I think I have problem with this listview and this line of code: listView = getActivity().findViewById(R.id.listview) . I try to fix this but no success.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_notifications);
quakeList = new ArrayList<>();
listView = getActivity().findViewById(R.id.listview);
queue = Volley.newRequestQueue(getActivity().getApplicationContext());
arrayList = new ArrayList<>();
getAllQuakes(Constants.URL);
}
The rest of the code.What can i return to show list?
private void setContentView(int fragment_notifications) {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_notifications, container, false);
return view;
}
void getAllQuakes(String url) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Earthquake earthquake = new Earthquake();
How can I send from console to listview? Should I change something?
try {
JSONArray features = response.getJSONArray("features");
for (int i = 0; i < Constants.LIMIT; i++) {
JSONObject properties = features.getJSONObject(i).getJSONObject("properties");
JSONObject geometry = features.getJSONObject(i).getJSONObject("geometry");
JSONArray coordinates = geometry.getJSONArray("coordinates");
double lon = coordinates.getDouble(0);
double lat = coordinates.getDouble(1);
//Log.d("Quake", lon + ", " + lat);
Log.d("Lat", properties.getString("place"));
earthquake.setPlace(properties.getString("place"));
earthquake.setType(properties.getString("type"));
earthquake.setTime(properties.getLong("time"));
earthquake.setMagnitude(properties.getDouble("mag"));
earthquake.setLon(lon);
earthquake.setLat(lat);
arrayList.add(earthquake.getPlace());
}
ArrayAdapter:
final ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), "Clicked " + position, Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Clicked"+ position,Toast.LENGTH_LONG).show();
}
});
arrayAdapter.notifyDataSetChanged();
you know the life cycle of fragment is differnt with activity..
so in onCreate fragmnet dont need do any thing:
write your code in onViewCreated()
it is work for me :
public class BlankFragment extends Fragment {
ListView listView;
ArrayList<String> arrayList;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
listView=getActivity().findViewById(R.id.list);
arrayList=new ArrayList<>();
getAllQuakes("");
}
void getAllQuakes(String url) {
for(int i=0;i<10;i++){
arrayList.add("vaa"+i);
}
final ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), "Clicked " + position, Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "Clicked"+ position,Toast.LENGTH_LONG).show();
}
});
adapter.notifyDataSetChanged();
}
}
Just change listView.setAdapter(arrayAdapter) to listView.setAdapter(adapter).
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();
}
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);
I got my MainActivity(FragmentActivity) with ViewPager with FragmentPagerAdapter which creates OrdersFragment in which i have ListView. Now i want to start new Activity OnItemClickListener but i can't figure out how to do this.
This is OrdersFragment class:
public class OrdersFragment extends Fragment {
ListView lv;
private ArrayList<Order> d = new ArrayList<Order>();
private ordersAdapter adapter;
public OrdersFragment (){
}
public OrdersFragment setArguments(ArrayList<Order> orders){
if(adapter == null) return this;
adapter.setData(orders);
Log.d("fragment", "zmiana");
adapter.notifyDataSetChanged();
return this;
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//d.add(new Order());
Log.d("fragment", "orders fragment created" + d.toString());
lv = (ListView) inflater.inflate(R.layout.orders_list, container, false);
lv.setOnItemClickListener(itemClicked);
adapter = new ordersAdapter(inflater, d);
lv.setAdapter(adapter);
return lv;
}
public ArrayList<Order> getOrders(){
return d;
}
OnItemClickListener itemClicked = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(OrdersFragment.this, OrderActivity.class));
}
};
}
It says that Intent constructor is undefined and also when i move over startActivity it says Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found. And it states that this fuction is defined in android.support.v4.app.Fragment.startActivity but I'm acually import this:
import android.app.Activity;
import android.content.Intent;
What I'm doing wrong?
Try this pass context as getActivity() to intent
OnItemClickListener itemClicked = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(getActivity(), OrderActivity.class));
}
};
In Fragment we have to pass context as getActivity(), replace your code with this below code it will work.
OnItemClickListener itemClicked = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(getActivity(), OrderActivity.class));
}
};
OnItemClickListener itemClicked = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(getActivity() != null) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(getActivity(), OrderActivity.class));
}
}
};
getActivity() will give you the Activity context that you can use to start the new Activity. In some rare cases getActivity() can return null so checking getActivity() is not null might not harm in your case.
I'm facing some issue with getting spinning values from list view.. in list view i have textview and related to spinner to it..
this is my Result.java
public class Result extends ListActivity{
SpinnerWrapper wrapper=new SpinnerWrapper();
Spinner sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
MyAdapter adap=new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules));
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setListAdapter(adap);
Button b=(Button) findViewById(R.id.button12);
//final Spinner sp=(Spinner) findViewById(R.id.spinner1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Result.this,Cal.class);
for(int i=0;i<15;i++){
//String text= sp1.getSelectedItem().toString();
//Log.d("This", text);
}
//Spinner sp1=og.getSp();
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(wrapper);
intent.putExtra("values", sp.getSelectedItem().toString());
//intent.putExtra("values", sp.setOnItemSelectedListener(wrapper));
startActivity(intent);
}
});
}
public void getObject(Spinner sp){
this.sp=sp;
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.item_layout, parent,false);
String[] items=getResources().getStringArray(R.array.modules);
TextView tv=(TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
Spinner sp=(Spinner)row.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
getObject(sp);
//og.setSp(sp);
//sp.setOnItemSelectedListener(wrapper);
//int dd=row.getId();
return row;
}
}
}
My Spinner Wrapper class is looks like this..
public class SpinnerWrapper implements OnItemSelectedListener {
public SpinnerWrapper() {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String text=parent.getSelectedItem().toString();
int intee=parent.getSelectedItemPosition();
String pos=Integer.toString(intee);
String tex=parent.getItemAtPosition(position).toString();
Log.d("Selected", text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I want to get spinning value of each spinner ..how can i do that??
//sp.setOnItemSelectedListener(wrapper);
I see your code is commented. so the OnItemSelectedListener is not called.
You call different listener for different spinner.
I don't see why you can't get the value by
String tex=parent.getItemAtPosition(position).toString();
Log.d("You have selected", tex);
You can create an ArrayList<String> and add the selected value to that ArrayList. It will be something like this.
static ArrayList<String> sth = new ArrayList<String>();
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"OnItemSelectedListener : "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
for (int i = 0; i < parent.getItemIdAtPosition(pos); i++) {
sth.add(parent.getItemAtPosition(pos).toString());
}
Log.w(getClass().getSimpleName(), "sth size " + sth.size());
}
You can pass that ArrayList around since the selected values are stored.