ListView setOnClickLisiner - android

I press a list item in my listView. How I can go to my another activity from list view item?
Please help me to understand how to solve this problem.
Here is my code if:
package com.example.list;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
ListView listView;
Context context;
ArrayList prgmList;
public static int[] prgmImage = { R.drawable.a_2, R.drawable.a_2,
R.drawable.a_2, R.drawable.a_2, R.drawable.a_2, R.drawable.a_2,
R.drawable.a_2, R.drawable.a_2
};
public static String[] prgmNameList = { "Let Us C", "c++", "JAVA", "Jsp",
"Microsoft .Net", "Android", "PHP", "Jquery", "JavaScript" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
setContentView(R.layout.activity_main);
context = this;
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new CustomAdapter(this, prgmNameList, prgmImage));
}
}

You have to implement
OnItemClickListener
to your activity and set it to your listView:
public class MainActivity extends Activity implements OnItemClickListener {
...
...
listview.setOnItemClickListener(this);
Then you will get a new method:
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
Log.e(TAG, "Pos: " + pos);
}
Here you get the position of the licked item

Hi for solving you're problem you need just to call this method onListItemClick.This method show witch row you select and you could set some intents as well.
protected void onListItemClick (ListView l, View v, int position, long id)
{
Toast.makeText(this, "Clicked row " + position, Toast.LENGTH_SHORT).show();
//go to another activity
Intent intent = new Intent();
intent.setAction(this, SecondActivity.class);
startActivity(intent);
}

Just set an OnItemClickListener to the ListView like:
final Context context = this;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.putExtra("position", position);
intent.setAction(context, OtherActivity.class);
startActivity(intent);
}
});

Related

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.

how to move from listview item to other layout in android

how to move from listview item to other layout in android .......
am trying to move from listview to other layout by click on it help me plz
public class ListViewActivity extends Activity {
private ListView lv1;
private String lv_arr[] = {"Android", "iPhone", "BlackBerry", "Nokia"};
#Override
public void onCreate(Bundle x)
{
super.onCreate(x);
setContentView(R.layout.activity_main);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
//lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
}
}
}
Use onItemClickListener for this.
It's simple. if you search around you will get this.
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(),SecondClass.class);
intent.putExtra("passsomething","value_of_variable");
startActivity(intent);
}
});
Try the below
lv1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Intent i = new Intent(ActivityName.this,SecondActivity.class);
// if you just want to navigate to second activity remove the below lines
//String value = (String) arg0.getItemAtPosition(position);
//i.putExtra("key",value);
startActivity(i);
});
In SecondActivity
Have a textview if you want to display item clicked in listview in second activity else ignore the below
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
String value = extras.getString("key");
// textview.setText(value);
}

How I can use onListItem to start a new activity with the ListView Value?

Hi I have a android application and i use a sqlite database and a listview in my Activity.
Now I want to use onListItemClick but I don_t know how I can get the value that I click and open a new activity with this value :(
here my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
mHelper = new DatenbankManager(this);
mDatenbank = mHelper.getReadableDatabase();
ladeDaten();
}
my ladeDaten method:
private void ladeDaten() {
Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null);
startManagingCursor(KlassenCursor);
android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
KlassenCursor,
new String[] {"name"},
new int[] {
android.R.id.text1
});
setListAdapter(KlassenAdapter);
}
Here my onListItemClick that don't work :(
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
I think you using cursor to get data from the database.Instead of using onListItemClick() use onItemClickListener() To get the listView item details use the following code
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String itemid = cursor.getString(cursor.getColumnIndex("ColumnName");//Repeat for other values
//Start the activity here
Intent todayreview = new Intent(ReviewPayment.this,
ReviewandResend.class);
todayreview.putExtra("iteid", itemid);
startActivity(todayreview);
}
});
To use onListItemClick Add extends ListActivity.
public class MainActivity extends ListActivity
But i think it is better to use onItemClick() like Vino said

Layout containing listview, unable to switch to new activity onitemclick

I am having a listview as part of a mainLayout and Onclickevent on listview is not working. Can anybody give me pointer where I am wrong?
Here's the code snippet:
public class HostListActivity extends ListActivity {
public ArrayList<String> deviceList;
static LinearLayout mainLayout;
public ListView hostListView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
designLayout();
setContentView(mainLayout);
}
public void designLayout() {
mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
int lHeight= LinearLayout.LayoutParams.FILL_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
AddHostListView(lHeight,lWidth);
}
private void AddHostListView(int lHeight, int lWidth) {
deviceList = new ArrayList<String>();
deviceList.add("abc");
deviceList.add("efg");
deviceList.add("hij");
hostListView = new ListView(this);
hostListView.setId(android.R.id.list);
hostListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, deviceList));
hostListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent myIntent = new Intent(v.getContext(), DirBrowserActivity.class);
startActivity(myIntent);
}
});
mainLayout.addView(hostListView, new LinearLayout.LayoutParams (lHeight,lWidth));
}
// DirBrowserActivity class
public class DirBrowserActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, DirBrowse View");
setContentView(tv);
}
}
You extend ListActivity but you do not use any feature from this class. I have noticed that this correlates with not working OnItemClickListener. Simply changing:
public class HostListActivity extends ListActivity {
to
public class HostListActivity extends Activity {
will help.
You should put this code
registerForContextMenu(getListView());
after setting an adapter to your listview, if you want to add a context menu to your items. If you just want to make smth on an item's click, you need to ovverride this method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
Hope this helps.

Categories

Resources