Simplify my ListView (with Intent, setOnItemClick etc) - android

I write a activity with a ListView. Now I want to know, is it possible to simplify my code (make it shorter).
An example is under the codeblock
package de.bodprod.dkr;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Context;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MediMenue extends ListActivity {
ArrayList<HashMap<String, Object>> medi_menue_items;
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medi_menue);
ListView antidotListView = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
medi_menue_items = new ArrayList<HashMap<String,Object>>();
HashMap<String , Object> item = new HashMap<String, Object>();
item.put("title", "Antidots");
item.put("desc", "Toxine und die Gegenmittel");
item.put("class_open", "MediAntidotList");
medi_menue_items.add(item);
item = new HashMap<String, Object>();
item.put("title", "Notfallmedikamente");
item.put("desc", "Dosierungen, Gegenanzeigen u.v.m.");
item.put("class_open", "MediNotmediList");
medi_menue_items.add(item);
ListView lv;
lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = new Intent();
String class_open = ((TextView) view.findViewById(R.id.class_open)).getText().toString();
if(class_open.equals("MediAntidotList")){
in = new Intent(getApplicationContext(), MediAntidotList.class);
}else if(class_open.equals("MediNotmediList")){
in = new Intent(getApplicationContext(), MediNotmediList.class);
}
startActivity(in);
}
});
final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_menue, medi_menue_items);
antidotListView.setAdapter(adapter);
}
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
super(context, textViewResourceId, Strings);
}
private class ViewHolder{
TextView class_open, title, desc;
}
ViewHolder viewHolder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.medi_menue_item, null);
viewHolder=new ViewHolder();
viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
viewHolder.title=(TextView) convertView.findViewById(R.id.medi_menue_name);
viewHolder.desc=(TextView) convertView.findViewById(R.id.medi_menue_desc);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.class_open.setText(medi_menue_items.get(position).get("class_open").toString());
viewHolder.title.setText(medi_menue_items.get(position).get("title").toString());
viewHolder.desc.setText(medi_menue_items.get(position).get("desc").toString());
return convertView;
}
}
}
For example: In the OnItemClickListener I get the id from the list item, and than I use with if and else. But the id is the same like the name from the activity, is it possible to say the new Intent, that it should open the class with the name wich is in the String class_open? Something like this:
String class_open = ((TextView) view.findViewById(R.id.class_open)).getText().toString();
Intent in = new Intent(getApplicationContext(), <class_open>.class);
startActivity(in);

First of all, since you are using ListActivity, you don't need to call setOnListItemClick handler on the list itself, just override this method from the ListActivity:
protected void onListItemClick(ListView l, View v, int position, long id);
So you can have this:
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent in = new Intent();
String class_open = ((TextView)v.findViewById(R.id.class_open)).getText().toString();
if(class_open.equals("MediAntidotList")){
in = new Intent(getApplicationContext(), MediAntidotList.class);
}else if(class_open.equals("MediNotmediList")){
in = new Intent(getApplicationContext(), MediNotmediList.class);
}
startActivity(in);
}
Then to answer your other question, you can try the Java reflection to get a class:
String class_open = ((TextView)view.findViewById(R.id.class_open)).getText().toString();
Class c = Class.forName("com.yourpackagename." + class_open );
Intent in = new Intent(getApplicationContext(), c);

I'm going for a long shot here since I haven't tried this myself. But I see you got a ViewHolder class that you don't show. If you really want to change ur onItemClick code, you could store an intent in every ViewHolder, which is set in your getView(...). This way you can get the viewholder tag in onItemClick and get the intent from that tag and start it.
Example:
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// Get the intent that u stored in this viewholder
ViewHolder clicked = (ViewHolder)view.getTag();
Intent toStart = clicked.getIntent();
startActivity(toStart);
}
And you dont need to set the onItemClick listener since you are extending from ListActivity, just override it.
To implement all this, you need to do some changes in getView() in your adapter. I'm sure you can figure out what!

Related

Android: How to format getIntent().getIntExtra

I am trying to retrieve the image resource file in another class, the intent putExtra has been placed in the MainActivity but I'm not sure how to retrieve that in the other class? I've tried getIntent().getIntExtra("category", search_name); but I just get errors. The idea is that the user claiks on the photo of a recipe category and then in the other class I can match that resource image file to the category name and retrieve the corresponding recipes.
Also this gridview slows down the app, how would I wrap and call this in an Async?
MainActivity
GridView gv = findViewById(R.id.gridview);
gv.setAdapter(new SetImageAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(MainActivity.this, Category.class);
intent.putExtra("Category", SetImageAdapter.categories[position]); // put image data in Intent
startActivity(intent); // start Intent
}
});
GridView Adapter
package com.stu54259.plan2cook;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class SetImageAdapter extends BaseAdapter {
public static Integer[] categories = {
R.drawable.african, R.drawable.american,
R.drawable.asian, R.drawable.comfort,
R.drawable.desserts, R.drawable.greek,
R.drawable.italian, R.drawable.vegetarian,
R.drawable.mexican,
};
private Context Cont;
public SetImageAdapter(Context c) {
Cont = c;
}
public int getCount() {
return categories.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgview = new ImageView(Cont);
imgview.setLayoutParams(new GridView.LayoutParams(370, 250));
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgview.setPadding(10,10,10, 10);
imgview.setImageResource(categories[position]);
return imgview;
}
}
In your MainActivity :
GridView gv = findViewById(R.id.gridview);
gv.setAdapter(new SetImageAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(MainActivity.this, Category.class);
intent.putExtra("CategoryPosition", position); // put image data in Intent
startActivity(intent); // start Intent
}
});
In your Category activity :
int position = getIntent().getIntExtra("CategoryPosition",0);
int selectedDrawable = SetImageAdapter.categories[position];
//Get name of the selected drawable :
String search_name = getResources().getResourceEntryName(selectedDrawable);
The possibility reason why the performance of your GridView is slow is the drawable resource size, make sure that all image used has a small resolution as well as smaller size.

SQLite: get id from ListView

This is my first app and i'm having some problem whit ListView
How can I get the ID in the database after clicking on a Item?
I can't use position because the Id may not be continuous and even if it is in order sometimes does not return the correct Id any.
this is my code, Thank you for your help:
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class livello1 extends AppCompatActivity {
DatabaseHelper myDb;
Button next;
#Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.livello1);
populateListView();
registerClick();
}
private void registerClick(){
ListView list =(ListView)findViewById(R.id.listViewMain);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Intent i = new Intent(livello1.this, bossi.note.Edit.class);
i.putExtra("id", position);
//i.putExtra("id", id);
startActivity(i);
}
});
}
private void populateListView(){
Cursor res = myDb.getAllData();
String[] myItems = new String[myDb.numRow()];
int cont = 0;
if(res.getCount() == 0){
// show message
return;
}
while( res. moveToNext()){
myItems[cont] = res.getString(1);
cont ++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item, myItems);
ListView list =(ListView)findViewById(R.id.listViewMain);}
There are a lot of solution approaches.
For example, you can store of Id's if ArrayList before ListView initialization.
That is, execute "SELECT id FROM mytable"
Then store in arraylist and use while click method.
Example:
//in class declaration
private ArrayList<Long> ar_ids = new ArrayList<Long>;
//
String sql = "SELECT id FROM table";
Cursor cur = db.rawQuery(sql, null);
String out = "";
ArrayList<Long> ar_ids = new ArrayList<Long>;
if (cur.moveToFirst()) {
do {
ar.add(cur.getString(0));
} while (cur.moveToNext());
}else{
}
}
cur.close();
for click event:
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Intent i = new Intent(livello1.this, bossi.note.Edit.class);
i.putExtra("id", ar_id.get(position));
//this is awsome!
startActivity(i);
}
and initialize this something like:
private void myfunc() {
String sql = "SELECT id FROM table";
Cursor cur = myDb.rawQuery(sql, null);
String out = "";
ArrayList<Long> ar_ids = new ArrayList<Long>;
if (cur.moveToFirst()) {
do {
ar.add(cur.getString(0));
} while (cur.moveToNext());
}else{
throw new NullPointerException();
}
}
cur.close();
}
private void populateListView(){
myfunc();
Cursor res = myDb.getAllData();
String[] myItems = new String[myDb.numRow()];
int cont = 0;
if(res.getCount() == 0){
// show message
return;
}
while( res. moveToNext()){
myItems[cont] = res.getString(1);
cont ++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item, myItems);
ListView list =(ListView)findViewById(R.id.listViewMain);}
well instead of using the ArrayAdapter you can extend a BaseAdapter or ListAdapter or even the ArrayAdapter to make your custom adapter. First of all make a plain java class to map your database data including id to an object. Then make a custom BaseAdapter instead of using the ArrayAdapter. In your BaseAdapter class you have to override various methods including getItem and getItemId methods. Now you can return the whole mapped object from the getItem method and simply obtain the object of selected item of the ListView by using listView.getSelectedItem() method in the Activity or Fragment class. Once you get the object you can access the id easily.
You can find a good example and explanation of a custom adapter here
About mapping the database result to an object, you can get some concept here
Here, you can replace you code class with this one, I have added a cursorAdapter and attached it with your ListView, in adapter I have added a method for getting Id that I call from click listener to retrieve id from cursor, you will have to set column number for _ID in public void getId(int position) from MyCursorAdapter class below.
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class livello1 extends AppCompatActivity {
DatabaseHelper myDb;
Button next;
private MyCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
myDb = new DatabaseHelper(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.livello1);
populateListView();
registerClick();
}
private void registerClick(){
ListView list =(ListView)findViewById(R.id.listViewMain);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Intent i = new Intent(livello1.this, bossi.note.Edit.class);
//retrieve id from cursor
int _id = adapter.getId(position)
i.putExtra("id", _id);
//i.putExtra("id", id);
startActivity(i);
}
});
}
private void populateListView(){
Cursor res = myDb.getAllData();
adapter = new MyCursorAdapter(this, res, 0);
ListView list =(ListView)findViewById(R.id.listViewMain);
list.setAdapter(adapter);
}
//adapter for list view
class MyCursorAdapter extends CursorAdapter {
Cursor cursor;
// Default constructor
public MyCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
this.cursor = cursor;
}
public void bindView(View view, Context context, Cursor cursor) {
String text = cursor.getString(1);
//make sure the TextView id is "#+id/text1" in da_item.xml
TextView tvText = (TextView) view.findViewById(R.id.text1);
tvText.setText(text);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflator inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
return Inflater.inflate(R.layout.da_item, parent, false);
}
public int getId(int position){
cursor.moveToPosition(position);
int colId = //set id column number here
int id = cursor.getLong(colId);
return id;
}
}
Since the code is untested, you might face a build issue in start, but it should give an idea of what's going on in code. feel free to ask any question if you face any problem in compilation

Hello, I have been working on android project where I need to send the selected String from listview to another activity

In my program I have fetched the data's from the server and view it in a listview. Then from those retrieved data in listview I have to select a string and that string will be passed on to another activity. My data's are retrieved and showed in listview but when I select an item(String) and try to send it to the next activity then instead of that string the package name is passed.
The layout code for retrieving and sending selected String is given below:
public class Doctors_layout extends Fragment implements
AdapterView.OnItemSelectedListener{
public final static String Message = "Sohan";
View myView;
Spinner spinner;
String selectedCity;
Context myContext;
String jsonResult;
JSONObject jsonObject;
JSONArray jsonArray;
String JSON_String;
ContactAdapter contactAdapter;
ListView listView;
Button button;
String send;
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.doctors_directory, container, false);
myContext = inflater.getContext();
contactAdapter = new ContactAdapter(myContext, R.layout.row_layout);
spinner = (Spinner)myView.findViewById(R.id.spinner);
listView = (ListView)myView.findViewById(R.id.listView);
listView.setAdapter(contactAdapter);
spinner.setOnItemSelectedListener(this);
List<String> city = new ArrayList<String>();
city.add("Choose a City");
city.add("Chittagong");
city.add("Dhaka");
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(myContext, android.R.layout.simple_spinner_item ,city);
aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// here we have to select medical name and intent viewDoctor page
Intent intent = new Intent(myContext, viewDoctor.class);
send = listView.getItemAtPosition(position).toString(); // doesn't retrieve the selected text, intead chooses package name
intent.putExtra(Message, send);
Toast.makeText(myContext, "Listview item "+send, Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
return myView;
}
And my custom contact adapter class is given below:
package com.example.sohan.patient;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Sohan on 6/9/2016.
*/
public class ContactAdapter extends ArrayAdapter {
List list = new ArrayList();
View row;
ContactHolder contactHolder;
public ContactAdapter(Context context, int resource) {
super(context, resource);
}
public void add(List<Contacts> updatedList) {
list.clear();
list.addAll(updatedList);
notifyDataSetChanged();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public int getCount() {
return list.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
if(row==null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.MedicalName =(TextView) row.findViewById(R.id.textView5);
row.setTag(contactHolder);
}
else{
contactHolder = (ContactHolder)row.getTag();
}
Contacts contacts = (Contacts)this.getItem(position);
contactHolder.MedicalName.setText(contacts.getMedicalName());
return row;
}
static class ContactHolder{
TextView MedicalName;
}
}
Try this, I hope it work...
send = ((TextView) view.findViewById(R.id.textView5)).getText().toString();
or
send = (String) parent.getItemAtPosition(position);
thanks..
send = listView.getItemAtPosition(position).toString();
you are sending a listView object and not actual string.
use:
send = yourListOfStrings.get(position)
UDP:
You are trying to create an adapter with onItemClick listener attacked to it:
contactAdapter = new ContactAdapter(myContext, R.layout.row_layout);
spinner = (Spinner)myView.findViewById(R.id.spinner);
listView = (ListView)myView.findViewById(R.id.listView);
listView.setAdapter(contactAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
right?
So you are suppose to fill(But I haven't found where you are doing this) this adapter with some data:
public void add(List<Contacts> updatedList) {
list.clear();
list.addAll(updatedList);
notifyDataSetChanged();
}
and you want to send a string from from the element of this list to another acitvity, right?
So you need to select an element of this list
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
send = updatedList.get(position).getYourString
inside onItemClickListener

How to set onClick listener on navigation drawer

Its in SlidingMenuFragment.java and I want each item listed clickable. Please help to achieve this.
DRAWER ITEMS
I want this 3 clickable so that I can open a new activity.
data.add("One");
data.add("Two");
data.add("Three");
navigation drawer items
Thank you for helping.
The code below is from http://manishkpr.webheavens.com/android-material-design-navigation-drawer-example/
package com.manishkpr.androidmaterialnavigationdrawer;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.*;
public class SlidingMenuFragment extends Fragment {
List<String>data;
ListView list_view;
SlidingMenuListAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_sliding_menu, null);
setUpView(root);
return root;
}
void setUpView(ViewGroup root){
list_view = (ListView)root.findViewById(R.id.list_view);
initList();
setUpClick();
}
void initList(){
data = new ArrayList<String>();
data.add("One");
data.add("Two");
data.add("Three");
adapter = new SlidingMenuListAdapter(getActivity(),data);
list_view.setAdapter(adapter);
}
void setUpClick(){
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.obj.closeDrawer();
Toast.makeText(getActivity(),"Hi "+position,Toast.LENGTH_SHORT).show();
}
});
}
}
A cleaner implementation with more thorough explanations can be found in the android documentation here:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
That said, it looks like you already have some clickable list items, all you need to do now is start the new activity within your ItemClickListener. You can make each item do a different thing by referencing the 'position' parameter:
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.obj.closeDrawer();
Toast.makeText(getActivity(),"Hi "+position,Toast.LENGTH_SHORT).show();
// Start 'YourActivity' when first item is clicked
if(position == 0){
Intent = new Intent(getActivity(), YourActivity.class);
getActivity().startActivity(intent)
}
}
});
I think it is important that you learn some more basic things, such as how to create new Activities and start them before getting too tied up with specific things like Navigation Drawers.
Try this one
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.obj.closeDrawer();
Toast.makeText(getActivity(),"Hi "+position,Toast.LENGTH_SHORT).show();
//Try to include this to your code
getActivity().startActivity(new Intent(this, YourAnotherActivity.class));
}
});
As an example you can also try something like this in your code :
if(position == 0) {
Intent AlbumsIntent = new Intent(MainActivity.this, avtivity.class);
startActivity(AlbumsIntent);
}else if (position==1){
Intent ArtistsIntent = new Intent(MainActivity.this, avtivity1.class);
startActivity(ArtistsIntent);
}else if (position==2){
Intent SongsIntent = new Intent(MainActivity.this, activity2.class);
startActivity(SongsIntent);
}else if (position==3){
Intent CardIntent = new Intent(MainActivity.this, activity3.class);
startActivity(CardIntent);
}else if (position==4){
Intent LogoutIntent = new Intent(MainActivity.this, avtivity4.class);
startActivity(LogoutIntent);
}

How to populate ListView and List onclick data?

I have a database with Users and their data.
Then make ListView with their names.
How then set click listener to ListView assosiated with my List?
So when I click on List element strats new Activity with all User details, no only name?
package com.test.tabs;
import java.util.ArrayList;
import java.util.List;
import com.test.tabs.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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;
public class SelectFragment extends Fragment {
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_select, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MySQLiteOpenHelper db = new MySQLiteOpenHelper(getActivity());
List<User> users = db.getAllUsers();
List<String> your_array_list2 = new ArrayList<String>();
for (int i = 0; i < users.size(); i++) {
your_array_list2.add(users.get(i).getFirstName());
}
listView = (ListView) getActivity().findViewById(R.id.listView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,your_array_list2);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
});
}
}
You need to implement parcelable in your User class:
public class User implements Parcelable
{
....
}
Now you can attach/send your user to the next activity like so:
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
User user = adapter.getItem(position);
Bundle b = new Bundle();
b.putParcelable("user", user);
Intent i = new Intent(context, OtherActivity.class);
i.putExtra("bundle", b);
startActivity(i);
}
});
In the new activity call:
Bundle b = getIntent().getBundleExtra("bundle");
User user = b.getParcelable("user");
Use ListView#setOnItemClickListener(AdapterView.OnItemClickListener listener).
In my opinion, you should use Cursor Adpater to populate listview from database. On item click event for each row, you can get corresponding cursor object, pass on some details to next activity, so it can fetch more user details and display to user.
You need to understand the basics first. Have a look at this tutorial to have some idea.
Good Luck!

Categories

Resources