I am a beginner. How will my string can be a clickable one that can redirect to other layout. or different layouts.
EDITED
This is my MainActivity.java:
public class MainActivity extends Activity {
String[] country = new String[] {
"China",
"India",
"Sri Lanka",
"Malaysia",
"Japan",
};
/** 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, R.layout.listview_layout, country);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent in = new Intent(MainActivity.this,PageOne.class);
startActivity(in);
};
});
}
and This is the PageOne.class
public class PageOne extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.china);
getIntent().getStringExtra("China");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
I wanted to do is when India is Click it will go to India.xml
And if China is click it will go to China.xml but what happening is any String I click the result is china.xml.
You need to use base adapter for that, and open another activity an click of the row.
Example
you can also use listview onitem click listener
and open the respective activity with respect to the position.
Assume you want listview row which contains strings to be clickable.
listview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent i= new Intent("com.example.secondactivity");
i.putExtra("value", country[arg2]);
// pass country based on listview position
//value- is the key
// second parameter is the actual value ie country name
startActivity(i);
}
});
country[arg2] . arg2 is the position of the listview. Items in listview are populated based on the position. When listview row is clicked country[arg2] is name of the country in that listview position. So you pass the value to the secondactivity using intent.
To retrieve in Second Activity's onCreate()
Intent intent = getIntent();
Bundle b = intent.getExtras();
String country = b.getString(key);//value or china in youe case.
Make sure second activity has an entry in manifest file
EDIT:
<activity
android:name="com.example.test1.secondactivity"
>
<intent-filter>
<action android:name="com.example.test1.secondactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
MainActivity with listview.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] country = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// R.layout.listview_layout is the custom layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, country);
//this refers to the actiivty context,
//second parameters says its a simple list item
//third is id of text
//country is your array of string
ListView listView = (ListView) findViewById(R.id.lv);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i= new Intent("com.example.test1.secondactivity");
i.putExtra("value", country[arg2]);
// pass country based on listview position
//value- is the key
// second parameter is the actual value ie country name
startActivity(i);
}
});
}
}
Second Actiivty which has a textview.
public class secondactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String country = extras.getString("value");//value or china in youe case
tv.setText(country);
}
}
}
Project folder
Try to make a listener on your list view item and navigate by intent to next activity.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent in = new Intent(MainActivity.this,SecondActivity.class);
startActivity(in);
};
});
Related
I'm trying to make a search function with listview. Each item in listView have it own activity to be start. My problem was I'm using index position for search. If the I search "Mango", the index will automatically change to 0 (which the problem is) and start activity1. "Mango" should start activity2, not 1. So how to keep it to its own respective activity after being search?
package com.try2.androidlistviewwithsearch;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
public static final String products[] = new String [3];
// ArrayList for Listview
static ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Listview Data
products[0]="Apple";
products[1]="Mango";
products[2]="Orange";
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
//ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try1.class);
startActivityForResult(myIntent, 0);
}
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try2.class);
startActivityForResult(myIntent, 0);
}
}
});
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
Try to change your listener
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem vo value je sprava
String prodname = ((TextView) view.findViewById(R.id.product_name)).getText().toString();
if( prodname.equals("Apple") {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try1.class);
startActivityForResult(myIntent, 0);
}
if( prodname.equals("Mango") {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try2.class);
startActivityForResult(myIntent, 0);
}
if( prodname.equals("Orange") {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try3.class);
startActivityForResult(myIntent, 0);
}
}
});
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);
}
});
I'm having trouble using setOnItemClickListener in my listview. Everytime I try, it causes too many errors with fixes that makes the code worse than what I'm intending to do.
This is my code:
public class Search extends Activity{
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
// Listview Data
final String products[] = {"Fill in the Blocks", "The Music Bee", "Little Paragon",
"Subway Surfers", "Cytus", "Temple Run", "Clumsy Ninja", "Smash Hit"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Search.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
I want to make the strings in the listview data clickable for my other activities but it won't work. I tried this code:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String openClass = adapter.getItem[position];
if (openClass.equals("Fill in the Blocks")) {
Intent myIntent = new Intent(view.getContext(), Fill_in_the_Blocks.class);
startActivityForResult(myIntent, 0);
}
else if (openClass.equals("The Music Bee")) {
Intent myIntent1 = new Intent(view.getContext(), The_Music_Bee.class);
startActivityForResult(myIntent1, 0);
}
else if (openClass.equals("Little Paragon")) {
Intent myIntent2 = new Intent(view.getContext(), Little_Paragon.class);
startActivityForResult(myIntent2, 0);
}
}
The errors were as follows:
-In the lv.setOnItemClickListener, "The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){})"
-In the (new OnItemClickListener(), "OnItemClickListener cannot be resolved to a type"
-In the public void onItemClick(AdapterView, "AdapterView cannot be resolved to a type"
-In the String openClass = adapter.getItem[position];, "getItem cannot be resolved or is not a field"
In the lv.setOnItemClickListener, "The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){})"
In the (new OnItemClickListener(), "OnItemClickListener cannot be resolved to a type"
In the public void onItemClick(AdapterView, "AdapterView cannot be resolved to a type"
The first 3 errors should disappear by replacing new OnItemClickListener() with
new AdapterView.OnItemClickListener()
and make sure you import:
import android.widget.AdapterView;
The final error:
In the String openClass = adapter.getItem[position];, "getItem cannot be resolved or is not a field"
You are meant to be calling the getItem method, rather than using array indexing.
replace the line:
String openClass = adapter.getItem[position];
with
String openClass = adapter.getItem(position);
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);
}
I am trying to get a screen transition when i select a value from the spinner. My spinner has just 2 values. The 1st one is selected by default. What I want is that when i click on the 2nd value in my spinner, it should take me to the new screen.
Please Help!
Thanks in advance!
Use onItemSelectedListener of your Spinner. Here is a Demo,
public class AndroidSpinner extends Activity implements OnItemSelectedListener {
TextView selection;
Spinner spin;
String[] items = { "bangladesh", "bangla", "bd", "australia", "japan",
"china", "indiaA", "indiaC" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spin = new Spinner(this);
setContentView(spin);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spin.setAdapter(aa);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// Do your Stuff Here
Intent intent = new Intent(MyActivity.this, NextActivity.class);
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
selection.setText("");
}
}
Assuming that you know how to set up the spinner adapter and everything, simply add the code to go to the next screen when the other item is selected on the spinner.
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if(position ==0)
//do nothing (assuming that you would want to stay in the same screen)
else if (position ==1)
{
//go to the next screen, probably by using an INTENT to the next activity or using setContentView(theLayoutXmlFile)
}
}
Use below code for open new activity or new screen onitemselected event of spinner.
public class SpinnerExample extends Activity implements OnItemSelectedListener {
String[] items = { "Dipak", "Aadi", "Bharat", "Pratik", "Usha", "Jayesh", "Deep", "Imran" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mSpn1 = (Spinner) findViewById(R.id.mSpn1);
mSpn1.setOnItemSelectedListener(this);
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
mSpn1.setAdapter(adpt);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// Do your Stuff Here
// For Open New Activity
Intent mInNewAccount = new Intent(MainActivity.this, SecondActivity.class);
startActivity(mInNewAccount);
finish();
// For Open New Screen
setContentView(R.layout.second_screen);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}