Intent in ListView - android

How do I click a ListView. I mean how to make intent on it.
JAVA
public class Login extends ActionBarActivity {
ListView listView;
ArrayAdapter<String> adapter;
String[] grocery_categories = {"Beverages", "Bakery", "Canned Goods", "Condiments", "Dairy", "Snacks", "Frozen Foods",
"Meat", "Produce", "Cleaners", "Paper Goods", "Personal Care", "Others"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
}
}
XMS
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.mobilegroceryapp.Login"
android:id="#+id/rl_login">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/login_bckgrnd"
android:src="#drawable/login_bckgrnd"
android:scaleType="centerCrop"
/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
I'm an student by the way and exploring Android Studio for my thesis.

You can use an OnItemClickListener to determine which list item gets clicked.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemChosen = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("groceryItem", itemChosen);
startActivity(intent);
}
}

set on itemclick listener to your listview
you can explore more about listview here http://developer.android.com/guide/topics/ui/layout/listview.html
if you want use intent on click of listview just put your code in listview click listener

try
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
String grocery = (String) listView.getAdapter().getItem(position);
Intent intent = new Intent(listView.getContext(),/*Your activity*/);
listView.getContext().startActivity(intent);
//or create other intents here
}
});
Now your code should look like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
String grocery = (String) listView.getAdapter().getItem(position);
Intent intent = new Intent(listView.getContext(),/*Your activity*/);
listView.getContext().startActivity(intent);
//or create other intents here
}
});
}

Related

Clickable custom array list

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

How to show different activities for different ListView items?

I have a ListView with several items and every item has it own Activity class to show details. How can I switch to an appropriate activity when user selects and item in this ListView?
My current code is following:
ListView listView;
ArrayAdapter<String> adapter;
String[] hotel = {"one room", "double room", "suit", "vip"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hotel);
listView.setAdapter(adapter);
}
Add setOnItemClickListener to your listView in onCreate under listView.setAdapter(adapter); :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(position == 0){
Intent intent = new Intent(MainActivity.this, OneRoomActivity.class);
startActivity(intent);
}
else if(position == 1){
Intent intent = new Intent(MainActivity.this, DoubleRoomActivity.class);
startActivity(intent);
}
//Do as above for rest of the list items
}
});
Hope this helps.

How to implement onItemClickListener on each item in the ListView to go to another activity?

How to implement onItemClickListener on each item in the ListView to go to another activity / to a new class?
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Resturants",
"Coffee Shops",
"Hotels",
"Gas Stations",
"Hospitals",
"Airports",
"ATM",
"Cinemmas",
"Phamacies"
};
Integer[] imgid={
R.drawable.restaurantz,
R.drawable.coffeeshop,
R.drawable.hotel,
R.drawable.gaspump,
R.drawable.hospitalblue,
R.drawable.airporticon,
R.drawable.atm,
R.drawable.cinemma,
R.drawable.hospitalblue,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
inside onItemClick:
startActivity(new Intent(MainActivity.this, NewActivity.class));
if you need to differenciate the clicked item, you can use the position parameter, for example:
if (position == 0)
// do something
else
// do something else

open webview when click items of list [android]

in the following when click the first item of list(open1)opened 1.html but when second item of list(open10)again opened 1.html
I want when click second item of list(open10)opened 10.html(And so the next item...)
what can i do?
Main.class:
public class Main extends ListActivity {
private static final String[] items = { "open1","open10"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.label,items));
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String title="file:///android_asset/1.html",des="file:///android_asset/10.html";
Intent intent = new Intent(getApplicationContext() ,WebViewActivity.class);
intent.putExtra("TITLE", title);
intent.putExtra("DES", des);
startActivity(intent);
}
});
}}
WebViewActivity.class:
public class WebViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent = getIntent();
String title = intent.getStringExtra("TITLE");
String des = intent.getStringExtra("DES");
WebView webview = (WebView)findViewById(R.id.webView1);
webview.loadUrl(des);
}}
Create an array like this.
String[] mFilepath = new String[] { "file:///android_asset/1.html",
"file:///android_asset/10.html" };
And then send the appropriate path to the receiver class.
public class Main extends ListActivity {
private static final String[] items = { "open1","open10"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(android.R.id.list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.label,items));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext() ,WebViewActivity.class);
intent.putExtra("TITLE", title);
intent.putExtra("DES", mFilepath[position]);
startActivity(intent);
}
});
}}
And load this URL in WebViewActivity class. Please feel free to ask if there is any concern.
Use This code to Opening a new WebView from List Item: Position is used to start WebView with every Item from the ListView
list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
WebView webView = new WebView(v.getContext());
String[] urls = getResources().getStringArray(R.array.bookmark_urls);
webView.loadUrl(urls[position]);
}
});
}
});
And Create a string-array.xml file inside values and put all web addresses as <item> of <resources> as bellow:
<resources>
<string-array name="bookmark_urls">
<item>http://www.google.com</item>
<item>http://www.android.com/</item>
<item>http://www.facebook.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
<item>http://www.android.com/</item>
</string-array>
</resources>
This works amazingly.

Listview item click issue

I have added leadbolt ad(entry ad). The advertising works correctly but listview.click doesn't work when I close ad from close sign. (Listview.click does not do anything, It works when I remove AdController)
public class SoundList extends ListActivity {
int [] soundfile;
MediaPlayer mediaPlayer;
private AdController myController;
final Activity act = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myController = new AdController(act, "111111111");
myController.loadAd();
soundfile= new int[] {R.raw.sound1,R.raw.sound2.....};
String[] sounds= getResources().getStringArray(R.array.sounds);
// Binding Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, sounds));
ListView lv = getListView();
lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked.
To fix this issue add following code to row view.
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

Categories

Resources