i have a Listview which displays list of clients, i have added an onClickListner to Listview so that i can get the detailed information of clicked client.
ListView l = (ListView) findViewById(R.id.jl);
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ClientListView j = JList.get(position);
String mess = "you clicked position " + position + " With Name " + j.GetClientName();
Toast.makeText(Home.this,mess,Toast.LENGTH_LONG).show();
}
}
);
}
i want to display information but not in toast, i will prefer some activity,fragment or some popup king of thing.
can anybody help?
Use ths code:
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ClientListView j = JList.get(position);
String mess = "you clicked position " + position + " With Name " + j.GetClientName();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ActivityName.this);//Your activity name
// set title
alertDialogBuilder.setTitle("Hello!");
// set dialog message
alertDialogBuilder.setMessage(mess)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
Hope this will work..
If you want to pass Client Name which you are displaying in list, you can get id from adapter and use it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String clientName= (String) ((TextView) view
.findViewById(R.id.client_name)).getText();
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("client", clientName);
startActivity(intent);
}
}
}
ListView l = (ListView) findViewById(R.id.jl);
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ClientListView j = JList.get(position);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Keyname", j);
startActivity(intent);
}
}
);
}
You can get the intent value in another activity.
If I have understood, you want to send some information to another activity to display this information.
You can have a look to the "Intent" in Android. It's used to start a component like an activity and can carry information to this new activity to start.
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("CUSTOMER_INFO", customer.getName());
startActivity(i);
Note: If you want to pass a custom object between activities, like for instance a List of custom object :
List<'CustomObject'>
Your custom object class have to implement Parcelable.
Related
I have an ListView which I have populate with a dynamic ArrayList and I used an OnItemClickListener to make it does something when i click on them (Code bellow).
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,txtOnlyList);
returnSaveNotesList.setAdapter(arrayAdapter);
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if(position == 0 ) {
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
intent.putExtra("firstNote","1 .txt");
startActivity(intent);
}
}
};
returnSaveNotesList.setOnItemClickListener(noteClickListener);
Now comes the tricky part, I want, somehow to be able to read what is actually written lets say on the position [0], the actual String. Is there any way to achieve that?
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if (position == 0) {
String yourString = txtOnlyList.get(position);// use this
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
intent.putExtra("firstNote", "1 .txt");
startActivity(intent);
}
}
Your ArrayAdapter<String> has a getItem() method that returns the String for a given position.
I have used the getItemAtPosition(position).
Here is the entire code:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,txtOnlyList);
returnSaveNotesList.setAdapter(arrayAdapter);
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
if(position == 0 ){
Intent intent = new Intent(ReadFromFile1.this, SaveAndRstoreNote.class);
String stringFromArray=txtOnlyList.getItemAtPosition(position).toString(); // I added this
intent.putExtra("firstNote","1 .txt");
startActivity(intent);
}
}
};
returnSaveNotesList.setOnItemClickListener(noteClickListener);
The View type parameter being passed in your OnItemClick function is the list item's view at that position.
AdapterView.OnItemClickListener noteClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> txtOnlyList, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.tv);
String text = tv.getText().toString(); //The required text is available in this variable
}
}
I want to get the item on list view to edit Text in another activity.
When clicked on list view item, I want to transfer the item in another activity in edit Text.
You have to make onItemClickListner of listview like that.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
And finally set Value to editText like this
editText.setText(value);
Hope this will help you.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences. And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text").
You can also pass your data through intents from which you are calling your new activity.
create onClick method like this.
ListView list = (ListView) findViewById(R.id.newsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
NewsItem item = (NewsItem) adapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
intent.putExtra(KEY, item.getHeadline());
startActivity(intent);
}
});
In next activity
Intent intent = getIntent();
headline = intent.getStringExtra(KEY);
have a look here
i am trying to get index of the list item, through which i can set different views for different items in another activity. Here is my code..
String description[] = {"inspiron","pavilion","macbook"}; /* i want this list on another activity after clicking listitem of first activity.*/
ArrayList<String> listDesc = new ArrayList<String>();
String ArrayDesc[] = null;
ListView listViewDesc;
ArrayAdapter<String> listAdapterDesc;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
for(int i = 0;i<description.length;i++)
{
listDesc.add(description[i]);
}
ArrayDesc = (String[]) listDesc.toArray();
listAdapterDesc=newrrayAdapter<String>this,android.R.layout.simple_spinner_item,ArrayDesc);
listViewDesc.setAdapter(listAdapterDesc);
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
lstView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a,View v, int i, long l)
{
/*here i want some code to extract list item index from list to set
different lists according to the item click on another view.*/
Intent intent = new Intent(getApplicationContext(), categorySelected.class);
startActivity(intent);
}
});
From your comments You say
please show me the code if any item from the list is clicked its index number will pop-up (in Toast)
So Try the below
public void onItemClick(AdapterView<?> a,View v, int i, long l)
{
Toast.makeText(getApplicationContext(),"Index of the item clicked is"+i,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), categorySelected.class);
startActivity(intent);
}
I have created one listview of some names,what i need is when i will click selected row it will go to that page only,on click on different row it will move to the same class but different content.I think it will move by question id.could anybody help me how to pass the question id Or any other method to do this..
here is my code..
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
};
You can try something like this -
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(Some condition)
{
Intent i= new Intent(YourActivity.this,ActivityOne.class);
// To pass data
i.putExtra("SomeId", someValue);
startActivity(i);
}
else if(Some other condition)
{
Intent i= new Intent(YourActivity.this,SecondActivityTwo.class);
startActivity(i);
}
else
{
// Do something else--
}
}
};
And in the other activity -
String identifier = getIntent().getExtras().getString("SomeId");
Here I've given an example assuming you have user list and clicking on item you want to show user profile...
In List_Act activity...
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.rowitem,parent,false);
convertView.setTag(UserId);
}
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i=new Intent(List_Act.this, Profile_Act.class);
int UserId = ((View)v.getParent()).getTag();
i.putExtra("UserId", UserId); //Setting variable you want to pass to another activity
startActivity(i);
}
};
in Profile_Act activity in onCreate()
String UserId = getIntent().getExtras().getString("UserId"); //retrieving value in another activity
now you'll have UserId variable set and you can use it...
By using the content provider I have inflated SMS in Inbox List View of my application. Now On item Click I want to show SMS text in another activity. I've implemented custom list view. Now I am not getting how to pick each single list item and show in new activity on click. In a Stack flow answer somebody suggested this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri mailUri = ContentUris.withAppendedId(getIntent().getData(), id);
startActivity(new Intent(Intent.ACTION_VIEW, mailUri));
}
If this code is correct then how I will configure next activity to act upon this ACTION_VIEW?
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int pos, long arg3) {
Intent i= new Intent(currentClass.this,secondActivity.class);
i.putExtra("string",Yourlist.get(pos).sms);
startActivity(i);
finish();
}
});
& on Another Activity You can receive through this:-
String msg=getIntent().getExtras().getString("string");
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{ case 0:
Intent firstIntent = new Intent(AndroidListViewActivity.this, SingleListItem.class);
startActivity(firstIntent);
break;
case 1:
Intent secondintent = new Intent(AndroidListViewActivity.this,jokes.class);
startActivity(secondintent);
break;