Fetching List Item Click - android

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

Related

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 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.

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 create clickable listview in Android using Sqlite3?

I have created listview in Android to show my records. And records are shown as well. Now I want to click on particular record from that listview to show/view particular record. And that record must show another layout for that profile. How to achieve it?
Create a onItemClick() event for the listview. Then, when you click on an item, a new intent will be created that will start a child activity that will display the particular record.
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(MyActivity.this, OtherActivity.class);
startActivity(launchActivity);
}
});

Pass custom list item content to new activity when list item is clicked from a listview

First of all I want to know if this is possible. Essentially I have a listview that is being populated programatically with a custom list item view and a custom adapter. This works like a charm but the problem is that I have to sort this list in a specific way once it is populated. The values that are being put into the list are stored in String arrays. The problem is that when I sort the listview it changes the index of where I thought the values would be stored. I want to start a new activity with certain values found in the list view. Is this possible? I could take a string from the list item and can look it up in my arrays storing the values when that item is clicked. I can then start a new intent with extras so that I can pass this new data to the new activity. So, is it possible to extract a string from a list item?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3){
Object o=lv.getItemAtPosition(position);
UtilityClass u=(UtilityClass)o;
String s=u.getMyData();
Intent edit = new Intent(context, AnotherActivity.class);
edit.putExtra("your_identifier", Integer.toString(position));
startActivity(intent);
}});
If u have implemented onItemClickListner / onItemSelectListner then you can get a callback onItemClicked()/ onItemSelected() from there using the Adapter get the item from selected position. and the same can be sent to another activity using a bundle/extra

Categories

Resources