Add item from activity(form) to listfragment - android

add expense item
expense history
Input the form in image 1 and click the OK button, pass data from image 1 to image 2.
How to add item from one activity to expense history (another activity)?

You need to do the following:
//in the Activity
eventsList = new ArrayList<HashMap<String, String>>();
// selecting single ListView item
ListView lv = getListView();
// Lauching the Event details screen on selecting a single event
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String ID = ((TextView) view.findViewById(R.id.pid))
.getText().toString();
Intent intent = new Intent(view.getContext(),
EventDetails.class);
intent.putExtra(pid, ID);
view.getContext().startActivity(intent);
}
});
Basically what you are doing is passing the id of the selected item in the listview to then the details in the fragment of that activity. Each time a different item is selected, a different details are shown. For further reference, take a look at this tutorial.

Related

Is there a way to set the correct text of a page for every element in a list?

I am kind of new to android studio, I have an array of strings for my list and when i click on an item from that list it takes me to the correct page. However, there are 100 items on the list and it's not logical to create 100 java files for all of them to set the text on that page.
My question is: is there a way to set the text to the correct item of list just after or before clicking that item?
Assuming you have in your FirstActivity a ListView please use the following code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String item = (String) adapterView.getItemAtPosition(position);
Intent intent = new Intent(YourActivity.this, SecondActivity.class);
intent.putExtra("item", item);
startActivity(intent);
}
});
To get the item in your SecondActivity please use this code:
String item = (String) getIntent().getExtras().get("item");
yourTextView.setText(item);
Hope it helps.

Fetching List Item Click

I have 2 activity in my app.
From First activity I take data to second activity's listview. In second activity I have a listview to show datas. Normally when I create a list and values myself I can create click event but now datas coming from other activity, and the datas uncertain.. How can I create click event for coming datas?(sorry for my very bad grammar)
It's my first activity;
Intent veri = new Intent(elementler.this,sonuclar.class);
veri.putStringArrayListExtra("logoveri", clickeddata);
startActivity(veri);
//I sent data for second activity's list
It's my list class;
//fetch values
Intent veri = getIntent();
veriler = veri.getStringArrayListExtra("logoveri");
//there are a lot of possibility in "veriler" maybe user select banana maybe apple maybe car maybe computer we can not know what he select..
//create a list with fetch data
ListView sonuclistesi=(ListView) findViewById(R.id.sonuclistesi);
ArrayAdapter<String> veriadaptoru=new Listeozellikleri(this,veriler);
sonuclistesi.setAdapter(veriadaptoru);
You can give listView onitem click,
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// do your stuff
}
});

to display items in a listview based on a parameter

i am displaying only three details from my database in 1 row of the listview after the user clicks on this list item all the details should be made visible in another activity in a list view.i tried but m getting a blank activity to open instead of a list..
ListViewDetails.java
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
int appno=cursor.getInt(cursor.getColumnIndexOrThrow("appln_no"));
Intent objintent=new Intent(getApplicationContext(),DisplayDetails.class);
objintent.putExtra("countryCode", countryCode);
startActivity(objintent);
}
});
here m passing an appno parameter to the next intent so that details related to this appno are displayed in DisplayDetails.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listdisplay);
Intent intentobj=getIntent();
int appno=intentobj.getIntExtra("appln_no", 0);
WayDataBase way=new WayDataBase(getApplicationContext());
ArrayList<String> listvalues=way.getListDetails(appno);
if(listvalues.size()!=0)
{
ListView lv=getListView();
ListAdapter adapter=new ArrayAdapter<String>(DisplayDetails.this, R.layout.view_animal_entry, R.id.animalName, listvalues);
lv.setAdapter(adapter);
}
}
but the screen is just balnk..
whats the issue??? please help! thanks!
Shiv,
You have fetched the values in variable "appno" but set values from variable "countryCode" instead of "appno".
In your DisplayDetails.java, you are trying to fetch it from the the variable "appln_no" which is incorrect.
If i look at your code then it seems that you want to pass appno value to another activity
so should keep it like this:
ListViewDetails.java
objintent.putExtra("countryCode", appno);
DisplayDetails.java
int appno=intentobj.getIntExtra("appln_no", "countryCode");

How to get the previous item of clicked listviewitem

I have two activities. In activity1 , I have list view which is populated from the database. On item click it should go to activity2. Activity2 contains two buttons (next and previous) and display the product details. In stead of "next" and "previous" button text, i was trying to get the previous item of clicked listview item and set text in button. and so for the "next" button.
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position-1);
Intent i = new Intent(this, Activity2.class);
i.putExtra("item",item.toString());
startActivity(Activity2);
}
}
and in activity2.class
buttonprevious = (Button) findviewById(R.id.previous);
Bundle b = getIntent().getExtras();
String preitem = b.getString("item");
buttonprevious.setText(preitem);
Is this the correct way or am i doing something wrong??
How can I display product details of next and previous items??
How to use the view flipper in this case?
Thanks ..
You can use getItem(int position) method of the adapter, which return you the list item of the specified position.
(OR)
Get and Store all the database data into one static List.
Set adapter with that list.
In Activity2 access data from that list.

How to view detailed data of a specific list view item

Let's say i have a list of cities from a country.
If the user clicks one of them, i want a new screen to be displayed, with the detailed infos for that list element.
Also, that screen will have others buttons ( eg: Write Review, Post Picture )
How can i achieve this ?
The City class should implement Parcelable. Its toString() method should return the name of the city. (Rather than simply having the city as a String.)
When you populate your ListView, use an ArrayAdapter<City> instead of an ArrayAdapter<String> (this code assumes that the cities are kept in List<City> list):
City[] cities = new City[list.size()];
list.toArray(cities);
mListView = (ListView) findViewById(R.id.citylist);
mListView.setAdapter(new ArrayAdapter<City>(this,
R.layout.listitem, cities));
In your onItemClick handler, get the selected city and add it as an extra on the intent you use to launch your details activity:
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a,
View v, int position, long id) {
City city = (City) a.getItemAtPosition(position);
Intent intent = new Intent(v.getContext(), DetailsActivity.class);
intent.putExtra("com.example.cities.City", city);
startActivity(intent);
}
});
In your details activity, get the city from the extras on the intent:
Bundle bundle = getIntent().getExtras();
City city = bundle.getParcelable("com.example.cities.City");

Categories

Resources