open webview when click items of list [android] - 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.

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.

Intent in ListView

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
}
});
}

ListView setOnClickLisiner

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);
}
});

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);
}

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