How to make my ListView items clickable? - android

I am extremely new in android. I am making a listview and every time i click on a list's item, it should open a new activity. In this case Settings activity. I have tried many ways but none have worked. Any kind of help would be appreciated. This is my code:
package com.alex.mylist;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class Settings extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ListView lstSettings = (ListView) findViewById(R.id.lstSettings);
lstSettings.getChildAt(1).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent mainIntent = new Intent(v.getContext(), Register.class);
startActivity(mainIntent);
}
});
}
}

Use listView.setOnItemClickListener to make listview items clickable
lstSettings.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Intent mainIntent = new Intent(Settings.this,
Register.class);
startActivity(mainIntent);
}
});

Set an ItemSelectedListener or ItemClickListener for your listview.
listView.setOnItemSelectedListener(new ListView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// call Activity
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// call activity
}
});

Related

ListView setOnItemLongClickListener not working android

I have an android app, which shows a list of items. By pressing an item it should perform an action like delete item. The problem is that, when I press the item, nothing happens, it is like the screen does not recognize my finger press. But, down on page, I have a button, that works fine, when I press it.
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView listView;
public static FirebaseUtil firebaseUtil;
public static CostumeAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseUtil = new FirebaseUtil();
setContentView(R.layout.activity_main);
final Context context = this;
if (firebaseUtil.getmFirebaseUser() == null) {
loadLogInView();
} else {
firebaseUtil.setmUserId(firebaseUtil.getmFirebaseUser().getUid());
listView = (ListView) findViewById(R.id.listView);
adapter = new CostumeAdapter(this);
listView.setAdapter(adapter);
fillData();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Costume car = adapter.getCostumes().get(position);
final String uuid = car.getUuid();
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete")
.setMessage("This car will be deleted.")
.setPositiveButton("Delete", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
firebaseUtil.remove(uuid, car);
}
})
.setNegativeButton("Cancel", null)
.show();
return true;
}
});
The xml files are ok, I guaranteed.
The OnItemLongClickListener that you have implemented would work only if the else part is executed. Is firebaseUtil.getmFirebaseUser() == null false?
I used this, and it worked, You can try out with some Log.d() to see if the clickListener is executed.
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated: on View Created");
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> adapterView,View view,final int position, long id) {
new AlertDialog.Builder(getContext())
.setTitle(R.string.attention)
.setMessage(R.string.confirm_to_delete)
.setNegativeButton(R.string.cancel,null)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Cursor c= adapter.getCursor();
c.moveToPosition(position);
int itemID = c.getInt(c.getColumnIndex("_id"));
dbWrite.delete("diary","_id=?",new String[]{""+itemID});
refreshList();
}
}).show();
return true;
}
});
}

I want to store spinner value in String and display it on a Textview

my code is working and I am getting the value on textview but its not changing on first click, suppose my spinner pops up and I select other value than at the same time my textview value doesn't changes it changes on the next click.
package com.vedicrishiastro.kundli.Screens.Extras;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.vedicrishiastro.kundli.R;
import com.vedicrishiastro.kundli.Screens.AbstractActivity;
public class Settings extends AbstractActivity implements View.OnClickListener {
private LinearLayout linearSelectLang,linearSetDefault,linearSelectPanch;
private TextView txtSelectLang,txtSetDefault,txtSelectPanch;
private Spinner spinner1,spinner2,spinner3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
linearSelectLang = (LinearLayout)findViewById(R.id.LinearSelectLang);
linearSetDefault = (LinearLayout)findViewById(R.id.LinearSetDefault);
linearSelectPanch = (LinearLayout)findViewById(R.id.LinearSelectPanch);
spinner1 = (Spinner)findViewById(R.id.settingSpinner1);
spinner2 = (Spinner)findViewById(R.id.settingSpinner2);
spinner3 = (Spinner)findViewById(R.id.settingSpinner3);
txtSelectLang = (TextView)findViewById(R.id.selectLangtext);
txtSetDefault = (TextView)findViewById(R.id.setdefaulttext);
txtSelectPanch = (TextView)findViewById(R.id.selectPanchtext);
linearSelectLang.setOnClickListener(this);
linearSetDefault.setOnClickListener(this);
linearSelectPanch.setOnClickListener(this);
spinner1.setVisibility(View.GONE);
spinner2.setVisibility(View.GONE);
spinner3.setVisibility(View.GONE);
}
public void onClick(View view){
int id = view.getId();
switch (id){
case R.id.LinearSelectLang:
{
spinner1.performClick();
String text = spinner1.getSelectedItem().toString();
txtSelectLang.setText(text);
spinner1.setVisibility(View.GONE);
}
break;
case R.id.LinearSetDefault:
{
spinner2.performClick();
String text = spinner2.getSelectedItem().toString();
txtSetDefault.setText(text);
spinner2.setVisibility(View.GONE);
}
break;
case R.id.LinearSelectPanch:
{
spinner3.performClick();
String text = spinner3.getSelectedItem().toString();
txtSelectPanch.setText(text);
spinner3.setVisibility(View.GONE);
}
break;
}
}
}
I tried this but it isn't working
case R.id.LinearSelectLang:
{
spinner1.performClick();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0)
{
txtSelectLang.setText("English");
}
else
{
txtSelectLang.setText("हिंदी");
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
break;
How about you use the spinner's OnItemSelected event?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Thing is, if you call performClick, the spinner pops out, but this call is not blocking. So you need the OnItemSelectedListener to get an async response with the input made by the user.
Calling getSelectedItem right after performClick (which opens the spinner?) will return the previously set element - which is the error you are facing.
Create an onItemSelectListener for your Spinner and change the text every time an item is selected.
spinner.setOnItemSelectedListener(new OnItemSelectListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
// DO it here
}
});

Wants to fetch data in my listview on other activity

I have a listview in my activity. I want to show the data of it when clicked on it on second activity. But is not able to do so. Help. This is the code. What to do so that my data is fetched from listview and is shown in next activity?
ListActivity.java
package com.example.task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends Activity implements OnItemClickListener{
ListView list1;
LoginDataBaseAdapter loginDataBaseAdapter;
ArrayList<HashMap<String, String>> datalist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
list1=(ListView)findViewById(R.id.list);
datalist=loginDataBaseAdapter.getAllAnimals();
ArrayAdapter<HashMap<String, String>> adapter=new ArrayAdapter<HashMap<String,String>>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,datalist);
list1.setAdapter(adapter);
list1.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
you can simple use this:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String mydata= datalist.get(arg2).toString();
// pass this data to your second activity
Intent n = new Intent(YourActivityName.this ,SecondActivity.class);
n.putExtra("key", mydata);
startActivity(intent);
}
Now retrieve in your SecondActivity on oncreate() method:
Intent n= getIntent();
String data = intent.getStringExtras("key");
String yourdata;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
yourdata= list1.getItemAtPosition(arg2).toString();
// pass this data to your second activity
}
I think you are trying to achieve like this..
String DATA;
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long arg3) {
DATA= YOUR_LIST.getItemAtPosition(pos).toString();
Intent intent = new Intent(getApplicationContext(),SECOND_ACTIVITY.class);
intent.putExtra("DATA",DATA);
startActivity(intent);
}
Hope it will help you..!!
Well apart from passing data by intents, you can also use Global Variables.
You can check out an example here.
You can set your global variable/s to the value that is applicable when list item is selected (using your onItemClick listner) and then access it in other activity.
Eg:
CLASS MyApp
class MyApp extends Application
{ public String whatever;}
CODE: ACTIVITY 1
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
MyApp as = ((MyApp)getApplicationContext());
as.whatever = position + "Clicked";
}
CODE: ACTIVITY 2
class Blah extends Activity
{
#Override
public void onCreate(Bundle b){
...
MyApp as = ((MyApp)getApplicationContext());
Log.d(as.whatever);
...}
}
MainActivity
String data;
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
data = adapter.getItem(position);
Intent intent = new Intnet(MainActivity.this, NextActivity.class);
i.putExtra("data", data);
startActivity(intent);
}
to open your data on NextActivity
Intent intent = getIntent();
String passData= intent.getExtras().getString("data");

How to use the seletedItem from the AutoCompleteTextView dropdown list?

Friends, I am using AutoCompleteTextView. The Suggestion :
String[] recipes={ "Fish", "Chicken", "Mutton"};
How do I do this:
When I select one of the item from the dropdown list, it will go to another event?
For example, I type Fi, it will come out Fish from the dropdown list and then I select Fish, it will go to another Activity.
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.view.View;
import android.content.Intent;
public class AutoCompleteTextActivity extends Activity {
String[] recipes ={
"Nasi Lemak With Ikan Bilis",
"Steamed Cod Fish"
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, recipes);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtRecipes);
textView.setThreshold(3);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View view, int index, long id){
Intent i=new Intent(this,Activity2.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
});
}
}
You can use a class that implements TextWatcher and override following methods:
#Override
public void afterTextChanged(final Editable editable) {
// check if entered text is "fish" and if yes then start the new activity.
}
#Override
public void beforeTextChanged(final CharSequence string,
final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence string, final int start,
final int before, final int count) {
}
}
receipesBelow Snippet will help you.
autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> av, View view,
int index, long id)
{
//index will give you item which you selected
Now start another activity here
Intent i=new Intent(context,SecondActivity.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
})
SecondActivity.java
You can retrieve that value is onCreate like below
String selectedItem=getIntent().getStringExtra("item");
You should have used setOnItemClickListener instead of setOnItemSelectedListener.

In Eclipse setting classes as list view forces close

After following code and showing no errors when I use my button to access my list view it forces close my code is as follows
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Listview extends ListActivity {
String classNames[] = {"home1", "Sweet", "tutorial2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classNames));
}#Override
protected void onListItemClick (ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
String openClass = classNames[position];
try{
Class selected = Class.forName("us.beats.with." + openClass);
Intent selectedIntent = new Intent(this, selected);
startActivity(selectedIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
where the button was set up I had Button Listview = (Button) findViewById(R.id.Listview);
Listview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("us.beats.with.Mylist"));
mpButtonClick.start();
but start activity was us.beats.with.Listview should have been the above
Change your classname and run it will work fine
public class MyList extends ListActivity {
String classNames[] = {"home1", "Sweet", "tutorial2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classNames));
}#Override
protected void onListItemClick (ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
String openClass = classNames[position];
try{
Class selected = Class.forName("us.beats.with." + openClass);
Intent selectedIntent = new Intent(this, selected);
startActivity(selectedIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
Now run the above code

Categories

Resources