How to get the previous item of clicked listviewitem - android

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.

Related

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

Add item from activity(form) to listfragment

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.

Android ListView Populated from XML won't send data to new activity

I have an array located in my strings.xml file that looks like the following.
<string-array name="array123">
<item>test1</item>
<item>test2</item>
<item>test3</item>
<item>test4</item>
<item>test5</item>
</string-array>
My listview is properly displayed on Activity 1, here is my onItemClick code
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent activityIntent = new Intent(this, Activity2.class);
String selectedItem = adapter.getSelectedItem().toString();
activityIntent.putExtra("my.package.dataToPass", selectedItem);
startActivity(activityIntent);
As soon as I tap an item from the list the screen just goes black and the application crashes. Here is my code for Activity 2
Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");
TextView word = (TextView) findViewById(R.id.word);
word.setText(myVal);
I'm not sure why this isn't working. I'm just trying to pass the string that was selected to show as text on the 2nd activity. I've been searching for a day and a half now.
I did try
activityIntent.putExtra("my.package.dataToPass", id);
but this just seemed to pass an empty screen.
Any help would be much appreciated. Obviously I'm not passing the right data in the intent.
As I can see, you are implementing the Click listener (onItemClick) rather than Select listener(onItemSelected). By default when you click on a ListView item it doesn't change its state to "selected"
Plz use adapter.getItem(position) instead of adapter.getSelectedItem()
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
String selectedItem = adapter.getItem(position).toString();
}

how to get the saved items for a particular listitem in android?

I am developing an application in which when user clicks on a item of a listview, a new page appears and in this page only the selected items are displayed. I mean for a particular list item only previously saved items are displayed.
In this new page i have 3 checkboxes. So when user clicks on a particular list item the new page display the checkbox with previous saved state for that particular item.
Now I dont know how to do that.
thanx in advance!!
This is my list onItemClick :-
OnItemClickListener ocl= new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
String name= list.get(arg2)[2];
Toast.makeText(Profile.this, "session of.." + name, Toast.LENGTH_LONG).show();
Settings.setName(name);
Intent intent= new Intent(Profile.this, ProfileConfig.class);
startActivity(intent);
}
};
lvChildProfile.setOnItemClickListener(ocl);
Now i want that when clicking the saved instance of checkbox appear.
You can keep the state within your objects itself.
Model implements Serializable {
boolean isOptionOneChecked
boolean isOptionTwoChecked
boolean isOptionThreeChecked
int id
...
// Constructor
// Getters & setters
}
This means your Adapater will have a list of objects from the type Model. How you want to display these in your ListView, can be done with a custom Adapter but that's not the real question.
When you click on an Item in your list, you can retrieve the object (like you already did) with
Model lModel = list.get(position);
In order to display the Model in a different Activity, you can send it along with your Intent as such:
Intent intent= new Intent(Profile.this, ProfileConfig.class);
intent.putExtra("MySelectedObject", lModel);
startActivity(intent);
Because our class implements the Serializable interface, we can retrieve the object in our new Activity as followed:
getIntent().getSerializableExtra("MySelectedObject");
You will have all the information about this object so you can create you layout accordingly.

Passing Which ListView Item was Clicked to the Next Screen - Android

I have a ListView in Screen 1 where I have a few items . The User Clicks on an item in the Listview and Screen2 pops up . But What appears in Screen2 depends on what item was clicked in Screen 1
For Ex - User Clicks A in Screen 1 - Words starting from A come up in Screen 2
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, Open the Next Screen
Intent r=new Intent(Dummy.this ,CricksList.class );
r.putExtra("extra", id);
startActivityForResult(r, position);
}
I have passed the item which was clicked on . But I want to Display on Screen 2 what the user clicked on - as an item in the List View . How do I do that ??
Create another activity and put code like this in it.....
TextView textview = (TextView) findViewById(R.id.myEditText);
String Name = getIntent().getStringExtra("extra");
textview.setText(Name);
Use the extra field from the receiving intent, and based on that, lookup the record in your data source(database, contentprovider, array) and inflate a view that suit your needs.
Try to create the other activity which displays the clicked item. Then, call the second activity on click.

Categories

Resources