How to make fragment transaction when clicked on a listitem? - android

I have an activity where it contains an ActionBar (with four tabs) a fragment respectively assigned to each of these tabs. In these fragments I've assigned some ListAdapters filled with string values, clickable that furthermore I want to operate. When clicking on an item I want that app to send from that fragment to another. I know that I have to use FragmentManager() and FragmentTransaction() but since I'm new to Android dev I demand of any kind of help, help that is appreciated.
Here's the snippet code of one of the tabs(UserFragment.java):
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/** This is a listfragment class */
public class UserFragment extends ListFragment
{
/** An array of items to display in ArrayList */
String user_items[] = new String[]
{
"Account",
"Addresses",
"Payment Providers",
"Profile",
"Transactions",
"Wallet"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, user_items);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onStart()
{
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
The Profile.java activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Profile extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
//Button test = (Button) findViewById(R.id.btnTest);
}
}

You need to define OnItemClickListener for your ListFragment to handle item click events. For example:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// start your new activity here
}
});

I found what was wrong. After a long search I learnt that if the onCreateView() method is static than it's all good to set listener/s but in this case while we fill an array-adapter of string than it's a no-go since first of first it has to created its View therefore doesn't let the app to make any further listeners. In order to make that available, the onActivityCreated(Bundle) should be initiated/created between onCreateView() and onStart() methods and insert the rest of the code.
Here's the solution to link a ListFragment to another FragmentActivity class:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
}

Related

multiple intents on different items of CustomListView

Whenever the user clicks the "phone icon" and sms icon an intend Dials a number or Sends an SMS. I don't know how to code my OnClickListener so that the two icons in the list view respond to the click event
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<Student> students = new ArrayList<Student>();
ListView studentListView = (ListView) findViewById(R.id.list);
StudentAdapter adapter = new StudentAdapter(this, R.layout.item_list, students);
studentListView.setAdapter(adapter);
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Student student = students.get(position);
String url = student.getStudentNumber();
Student student = students.get(position);
String url = student.getStudentNumber();
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", url);
smsIntent.putExtra("sms_body", "your desired message");
startActivity(smsIntent);
}
}
});
}
}
this Sms intent works fine when I click anywhere of the item of my ListView but
let's say there is a with "sms icon" and R.id.smsButton in each item and I only want that to respond to the click event and start the intent, not any other Views in the Item of my CustomListView.
`
write click listeners in getView() method of adapter class itself
public View getView(int pos, View convertView, ViewGroup parent){
...
Button button = (Button) row.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//do something
}
});
...
return row;
}

How to see text in toast

I'm trying to see text from GridView using a toast when I selected or clicked on GridView's data. I can see position, but don't know what to put to see a text.
Here is a code.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
String [] data = {"a","b", "c","d","e","f", "g","h"};
GridView gdView;
ArrayAdapter<String> adapter;
String result;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, data);
gdView = (GridView) findViewById(R.id.gdView);
gdView.setAdapter(adapter);
gdView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getBaseContext(), position, Toast.LENGTH_SHORT).show();
}
});
}
}
Could someone suggest me, what I need to use or where to look for it?
Thank you
The parameter int position of the method onItemClick() on AdapterView.OnItemClickListener gives you:
The position of the view in the adapter.
See here: https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView, android.view.View, int, long)
You can use this to retrieve the content of this view from your ArrayAdapter with:
adapter.getItem(position)
See here: https://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)
This will return the content of your ArrayAdapter at the given position, in your case as a String, which you can store in a variable if you like or pass directly into Toast.makeText().
Turn last string to
Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();

add two number using spinner in android

i want to add two numbers using spinner view. here in my code two spinners .After i run the emulator it displays straight result only. it does not display spinner control and i'm not able to select the two numbers. Pls give one solution. Thanks in advance. Here code
package com.kk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}
This might be because the spinner's "onItemSelected" method gets called initially as soon as your code enters the onCreate method. Maybe you have to maintain flag values to do this.
These links might help you get started with it,
Spinner onItemSelected called erroneously (without user action)
Spinner onItemSelected() executes when it is not suppose to
Try to exchange
android.R.layout.simple_dropdown_item_1line
to
android.R.layout.simple_spinner_item

array that starts a new activity

OK, I realize this is prolly pretty basic, but im so new to this its unreal.
What I have is an array. What I want is for when a user clicks an item in the array, it opens a new activity that is specific to that item. Its a list of festivals, and when you click on one of the festivals, and when you click on it, it opens an activity that provides information about that festival.
I have no idea what I'm doing here. Im pretty sure I need to use an OnClickListener, but thats it.
Activity
package com.MADONK.LAFESTS;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class Home extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, Festivals));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] Festivals = new String[] {
"Lake Arthur Regatta", "Contraband Days", "Iowa Rabbit Festival",
};
}
Since you're extending ListActivity, you can override onListItemClick(). You could do something like this, which gets the appropriate Festival object, and passes a member of it into an Intent, the Intent is then used to start another Activity:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Festival item = (Festival) l.getItemAtPosition(position);
Intent i = new Intent(v.getContext(),YourFestivalDetailActivity.class);
i.putExtra("some_attribute", item.getSomeAttribute());
startActivity(i);
}
And then in the Activity you start that is meant to show the Festival detail, which in the above example is called YourFestivalDetailActivity, you should extract the Festival information from the Intent used to start it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String someAttribute = getIntent().getStringExtra("some_attribute");
}
Note that in this example I only pass a single String via the Intent, but know that you can pass more than that via an Intent. See the docs.

I want to store the edited spinner value in the database

my coding is working well .But I want to store the edited spinner value in the database how it be done.here is my code
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class tooo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner localSpinner = (Spinner)findViewById(R.id.color_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.color_array, R.layout.my_normal_spinner_item_style);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
localSpinner.setAdapter(adapter);
}
}
my simple_spinner_dropdown_item.xml
Add follwoing after setting the adapter in your code
localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
String text = localSpinner.getSelectedItem().toString();
//The above text variable has the selected value of spinner
}
public void onNothingSelected(AdapterView parentView)
{
}
});
Why didnt you edited question in your last post rather than creating a new post

Categories

Resources