getting to another screen from listview row - android

In the following code, I want to set Onclick Listeners on some "elements" of my row. where should I place the setOnclick listener code? In the activity code or in the adapter? I suppose the first, but in that case how will I identify which row has been selected
(each row is supposed to get me to different views)?
Java code:
List<String[]> info=new ArrayList<String[]>();
String[] names_info=new String[5];
String[] phones_info=new String[5];
String[] map_info=new String[5];
String[] web_info=new String[5];
String[] photos_info=new String[5];
for(int i=0;i<names_info.length;i++)
{
info.add(new String[]{names_info[i],phones_info[i],map_info[i],web_info[i],photos_info[i]});
}
for (int i = 0; i < info.size(); i++) {
Log.d("TAG", "item " + i +
" name:" + info.get(i)[0] +
" info:" + info.get(i)[1]);
}
FacilitiesAdapter adapter = new FacilitiesAdapter(this,info,want_info_display,want_phone_display,want_photos_display);
//System.out.println(info[0]);
setListAdapter(adapter);
EDIT
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// When clicked, show a toast with the TextView text
//Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),
//Toast.LENGTH_SHORT).show();
Intent i33 = new Intent(HotelsScreen.this,AbousUsScreen.class);
i33.putExtra("position", arg2);
System.out.println(arg2);
System.out.println(info.get(arg2)[0]);
startActivity(i33);
}
});
I see results in Log, so that means that my info is getting its result normally.
This is my adapter.
public class FacilitiesAdapter extends ArrayAdapter<String[]> {
private final Context context;
List <String[]> dataList;
public FacilitiesAdapter(Context context, List<String[]> dataList, int need_phone, int need_info, int need_photos) {
super(context, R.layout.expand_row);
this.context = context;
this.dataList = dataList;
//this.tracks_condition=tracks_condition;
//this.count=count;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.expand_row, parent, false);
System.out.println("I am here");
String[] data = dataList.get(position);
String name=data[0];
TextView textView = (TextView) rowView.findViewById(R.id.name);
System.out.println("I am in the adapter "+name);
textView.setText(name);
return rowView;
}
}
EDIT 1:
am I supposed now to see in every item I click its position and its name? Cause I see nothing?

can be as simple as something alone these lines:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
Intent i33 = new Intent(MyScreen.this,OtherScreen.class);
i33.putExtra("position", position);
startActivity(i33);
}
});

I think your biggest problem will be figuring out how to map a row to the appropriate screen. If you can do this, you can set up a Map object with all the mappings.
In your adapter, you can then use setTag() on the view and pass it what you get back from the Map, then creating a new Intent from it.
//In your adapter's getView() method...
Class activityClass = rowToActivityMap.get(someRowAttribute);
view.setTag(activityClass);
//In your click listener...
Class activityClass = (Class)view.getTag();
Intent intent = new Intent(this, activityClass);
startActivity(intent);

Related

Previous items piling up in the Listview

I've created a listview which takes the input from the alertDialog editText and populate it. Everything is working fine but after deleting the item from the row and adding something again it adds up the previous deleted item also which is not a good thing. I know the clear picture of my problem but it is difficult to find out whether how to do that.
This is my code where I'm adding the list inside the listView :
Adding Items
{ items[items.length - 1] = value;
List<String> newList = new LinkedList<String>(Arrays.asList(items));
//Log.e("STRIING", newArray.toString());
adapter = new MyListAdapter(RecyclerActivity.this, newList);
mListView.setAdapter(adapter);
String[] temp = new String[items.length + 1];
for (int i = 0; i < items.length; i++)
temp[i] = items[i];
items = temp;
alertDialog.dismiss();
adapter.notifyDataSetChanged();
input.getText().clear();
}
In here I have to notify the changes of the deleted item in order to get the desired result but i don't know how.
Here is the place where I'm deleting the item from the listview:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> adapterView, View view, int i, long l) {
final String pos = mListView.getItemAtPosition(i).toString();
Log.e("POS", pos);
deleteDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
adapter.remove(pos);
adapter.notifyDataSetChanged();
}
});
deleteDialog.show();
}
});
and here is the MyAdapter class which binds the data :
public class MyListAdapter extends ArrayAdapter<String>{
private final Context context;
private final List<String> values;
class ViewHolder {
public TextView text;
}
public MyListAdapter(Context context, List<String> newList) {
super(context, R.layout.row_item, newList);
this.context = context;
this.values = newList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.tvItem);
textView.setText(values.get(position));
return rowView;
}
}
My adapter is the Global Variable
I've tried using this in order to see whether the problem is getting fixed or not by following this link :
Deleting the items for the listview
The code looked like this now . :
newList.remove(pos);
adapter.notifyDataSetChanged();
newList is nothing but a List<> of type String
Please help me with this because I have wasted a long hour in this only. Thanks in advance.
EDITS
I've tried adding this in my Adapter Class, but this is giving me the same result :
public void removeItem(String position) {
values.remove(position);
notifyDataSetChanged();
}
Calling this method like this
adapter.removeItem(pos);
But same result
SOLVED
I've solved this problem by defining the single ArrayList in Global and using it. Made my work easy.
In my add section I've changed it like this
{
newArrayList.add(value);
adapter = new MyListAdapter(RecyclerActivity.this, newList);
mListView.setAdapter(adapter);
String[] temp = new String[items.length + 1];
for (int i = 0; i < items.length; i++)
temp[i] = items[i];
items = temp;
alertDialog.dismiss();
adapter.notifyDataSetChanged();
input.getText().clear();
}
Just creation of single variable as the array works smoothly.

Filtered list item opens the original list items' activity

After a tremendous amount of time searching in here, and everywhere else I am hopeless to find a solution.
So here is my problem.
I have created a list-view and on top of that I added a search-bar.
When I use the search-bar, to filter the results... when I click on item 7, instead of opening the specific clicked activity i.e. 7, it always starts from the first one.
I am looking forward to your help guys; because I need it!
public class Group extends ListActivity {
// ArrayList thats going to hold the search results
ArrayList<HashMap<String, Object>> searchResults;
// ArrayList that will hold the original Data
ArrayList<HashMap<String, Object>> originalValues;
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grouplist);
final EditText searchBox = (EditText) findViewById(R.id.searchBox);
ListView playersListView = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final EditText searchBox = (EditText) findViewById(R.id.searchBox);
ListView playersListView = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// these arrays are just the data that
// I'll be using to populate the ArrayList
String names[] = {/*list of names*/ };
String teams[] = {/*list of teams*/};
Integer[] photos = {R.drawable.... /*list of drawables*/};
Integer[] id ={/*Position*/};
originalValues = new ArrayList<HashMap<String, Object>>();
// temporary HashMap for populating the Items in the ListView
HashMap<String, Object> temp;
// total number of rows in the ListView
int noOfPlayers = names.length;
// now populate the ArrayList players
for (int i = 0; i < noOfPlayers; i++) {
temp = new HashMap<String, Object>();
temp.put("name", names[i]);
temp.put("team", teams[i]);
temp.put("photo", photos[i]);
temp.put("id", id[i]);
// add the row to the ArrayList
originalValues.add(temp);
}
// searchResults=OriginalValues initially
searchResults = new ArrayList<HashMap<String, Object>>(originalValues);
final CustomAdapter adapter = new CustomAdapter(this, R.layout.players, searchResults);
// finally,set the adapter to the default ListView
playersListView.setAdapter(adapter);
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// get the text in the EditText
String searchString = searchBox.getText().toString();
int textLength = searchString.length();
// clear the initial data set
searchResults.clear();
for (int i = 0; i < originalValues.size(); i++) {
String playerName = originalValues.get(i).get("name").toString();
if (textLength <= playerName.length()) {
// compare the String in EditText with Names in the
// ArrayList
if (searchString.equalsIgnoreCase(playerName.substring(0, textLength)))
searchResults.add(originalValues.get(i));
}
}
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});
// listening to single list item on click
playersListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos=Integer.ParseInt(searchResults.get(position).get("id").toString());
switch (pos) {
case 0:
Intent newActivity = new Intent(TeamsList.this, Barca.class);
startActivity(newActivity);
break;
case 1:
etc...
}
}
}
});
}
Custom adapter Class:
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
// let android do the initializing :)
super(context, textViewResourceId, Strings);
}
// class for caching the views in a row
private class ViewHolder {
ImageView photo;
TextView name, team;
}
ViewHolder viewHolder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.players, null);
viewHolder = new ViewHolder();
// cache the views
viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.team = (TextView) convertView.findViewById(R.id.team);
//Take one textview in listview design named id
viewHolder.id = (TextView) convertView.findViewById(R.id.id);
// link the cached views to the convert view
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
int photoId = (Integer) searchResults.get(position).get("photo");
// set the data to be displayed
viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId));
viewHolder.name.setText(searchResults.get(position).get("name").toString());
viewHolder.team.setText(searchResults.get(position).get("team").toString());
viewHolder.id.setText(searchResults.get(position).get("id").toString());
// return the view to be displayed
return convertView;
}
}
}
I think you cant find correct position on listview item click. so u can use one textview with visibility="Gone" and insert the position in that textview in every row. now u can easily access position while clicking on item with the value of textview which shows perfect position. Hope it works. Thanx
The problem is that Adapter is populated once but with search results it gets overViewed by the searched items so on clicking it refers to the original items of list instead of the filtered list once , so we have to use the filtered lists' positions instead of the original one, i also faced this problem, try this:
In your listView.setOnItemClickListener
playersListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//Object objFilteredItem = parent.getItemAtPosition(position);
//String a = searchResults.get(position);
switch (Integer.parseInt((String) adapter.getItem(position))) {
case 0:..
Intent newActivity = new Intent(TeamsList.this,Barca.class);
startActivity(newActivity);
break;
case 1:
etc...
break;
}
}
});
As from my previously answered question here, you have to override the getItem(position) method in your CustomAdapter. You are setting the onItemClick somewhat correctly but the list adapter doesn't know what exactly it's getting from getItem(position).
EDIT (details): You need to add something like this in your custom adapter -
#Override
public Object getItem(int position) {
return list.get(position);
}
You should already have the list in your custom adapter. If not, you can add a list reference to your CustomAdapter:
private ArrayList<HashMap<String, Object>> list;
Then setting it using a setter in your Group activity:
customAdapter.setList(searchResults);

Deleting last element from listview

Ok, so I'm trying to delete elements from a ListView, and everything goes alright until I try to delete the last element, but only if I delete the second last element and the try to delete the last one. Here's my code:
public class TestActivity extends ListActivity {
ListView list;
PAdapter adapter;
static SQLiteDatabase db;
static ArrayList<Profesor> datos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
db = openOrCreateDatabase("DBLogin", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Profesor (Id integer primary key AUTOINCREMENT"
+ ", Nombre" + " varchar(30), Imagen varchar(15), Fecha" + " varchar(15)" +
", Direccion varchar(35), sexo varchar(10), Telefono varchar(15), creado int);");
list = (ListView) findViewById(android.R.id.list);
datos = new ArrayList<Profesor>();
datos = datos();
adapter = new PAdapter(this, R.layout.row, datos);
setListAdapter(adapter);
list.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (datos.get(arg2).getCreado() == 1) {
PopupMenu popup = new PopupMenu(TestActivity.this, list);
popup.getMenuInflater().inflate(R.menu.popup3, popup.getMenu());
final int id = arg2;
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
delete(datos.get((int)adapter.getItemId(id)));
datos = datos();
adapter = new PAdapter(TestActivity.this, R.layout.row, datos);
setListAdapter(adapter);
list.invalidateViews();
return true;
}
});
popup.show();
}
return false;
}
});
}
private ArrayList<Profesor> datos(){
ArrayList<Profesor> ap = new ArrayList<Profesor>();
Cursor cursor = db.rawQuery("SELECT * FROM Profesor", null);
if(cursor.moveToFirst()){
do {
Profesor p = new Profesor(null, null);
p.setId(cursor.getInt(0));
p.setNombre(cursor.getString(1));
p.setFecha(cursor.getString(3));
p.setDireccion(cursor.getString(4));
p.setSexo(cursor.getString(5));
p.setImagen(cursor.getString(2));
p.setCreado(cursor.getInt(7));
p.setTelefono(cursor.getString(6));
ap.add(p);
} while(cursor.moveToNext());
}
return ap;
}
public static void delete(Profesor p){
db.execSQL("DELETE FROM Profesor WHERE Id = " + p.getId() + ";");
}
Here is the class PAdapter:
public class PAdapter extends ArrayAdapter<Profesor> {
private Context context;
private int layout;
private ArrayList<Profesor> datos;
public PAdapter(Context context, int layout, ArrayList<Profesor> datos) {
super(context, layout, datos);
this.context = context;
this.layout = layout;
this.datos = datos;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(layout, parent, false);
ImageView imagen = (ImageView) item.findViewById(R.id.imageView1);
datos.get(position).imagen(imagen, context, false);
TextView nombre = (TextView) item.findViewById(R.id.tNombre);
nombre.setText(datos.get(position).getNombre());
return item;
}
}
Funny thing is, it executes the code following the delete but doesn't delete the last element, neither does it throw me an exception. And again, it only happens with the last element, and only after I delete the one before it, if I close and reopen the app afterwards I can delete it normally.
I assumed you passed pos = 3 as parameter value. Because the size of list is 3, last element's position should be 2.
*EDIT:
Remember, start index of listview and adapter is different. The ListView item pos starts from "1" as first position, adapter (such as array) starts from index "0" as first position.
adapter.remove(adapter.getItem(pos-1));
You have some mistakes in your code. My following suggestion dont promise will solve your current problem, but lets try it :
Pay attention when you setting the adapter setListAdapter(adapter);, the list will set its adapter each time user did onClick - which is a waste.Use adapter.add()/adapter.remove() instead of reset all data by doing setListAdapter again and again.
You called list.invalidateViews(), for what? In your case, adapter.notifyDataSetChanged() is enough.
Try this:It's working fine for me.
ListView list;
adapter = new MyListAdapter(this);
list = (ListView) findViewById(android.R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
I solved it by Adding this in the activity of the single item so if i get back to the ListView Activity it is refreshed because of finish(); function.[ 8 feb 2020]
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(SingleItem.this, ListView.class));
finish();
}

How to append an id to a ListViewItem using SimpleCursorAdapter and SQLiteDatabase

I've been searching on this for a while now, but I can't find anything that could help me solve my problem.
I have a list of categories in a listview. I fetch these from a SQLiteDatabase and use a SimpleCursorAdapter to put them in the list.
This works fine...
Now, if I click a category, I want to launch a new activity displaying all items with that specific category. Passing parameters to the new activity isn't a problem - found a nice tutorial on how to do this here: Passing data or parameter to another activity
My problem is that I can't get the id out of the selected view... I want the id from the selected category so I can get the items with that categoryId.
This is where I try and get the id out of the view - I've used a lot of different methods (including some fiddling with listview, item and position, ... this is my most recent attempt) and don't know what to try next...
#Override
public void onItemClick(AdapterView<?> listview, View item, int position,
long itemId) {
Intent intent = new Intent();
Bundle bun = new Bundle();
bun.putInt("categoryId", (int) itemId);
intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
intent.putExtras(bun);
startActivity(intent);
}
Has anybody encountered the same problem and if you have, do you have some advice for me on how to do this?
This is how the problem got solved:
Cursor c = (Cursor)listview.getAdapter().getItem(position);
int id = c.getInt(c.getColumnIndex(_ID)); //0 = index id
//Log.d("Category id", id + "");
Intent intent = new Intent();
Bundle bun = new Bundle();
bun.putInt("categoryId", id);
intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
intent.putExtras(bun);
startActivity(intent);
Try following
#Override
public void onItemClick(AdapterView<?> listview, View item, int position,
long itemId) {
Cursor c = (Cursor)adatper.getItem(position); //adapter = cursor adapter object
int id = c.getInt(0)// 0 is index of id.
Intent intent = new Intent();
Bundle bun = new Bundle();
bun.putInt("categoryId", id);
intent.setClass(MainActivity.this, ItemsPerCategoryActivity.class);
intent.putExtras(bun);
startActivity(intent);
}
For this,you have can use custom adapter and there,you can set ids in a HashMap.Here is an example:
public class DemoAdapter extends SimpleCursorAdapter
{
Cursor dataCursor;
LayoutInflater mInflater;
Context context;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
public DemoAdapter(Context context, int layout, Cursor dataCursor, String[] from,int[] to)
{
super(context, layout, dataCursor, from, to);
this.context=context;
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(context);
this.fromWhere=fromWhere;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.my_list_item, null);
holder = new ViewHolder();
holder.cName=(TextView)convertView.findViewById(R.id.contactName);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
dataCursor.moveToPosition(position);
String id=Integer.toString(dataCursor.getInt(dataCursor.getColumnIndexOrThrow("_id")));
myList.put(position, id);
holder.cName.setText(dataCursor.getString("contact_name"));
return convertView;
}
static class ViewHolder
{
TextView cName;
}
}
Now,use it like-
listView.setOnItemClickListener(new OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
int id=Integer.parseInteger(DemoAdapter.myList.get(position));
}
});

how to pull strings from an array adapted listview for a clicked list item

ok so i have an array adapted listview (the array adapting is done in another class).. i just got the click listener working for the list but now i want set it up so that when i click an item it pulls the strings from the clicked item and piggybacks them on the intent to a new activity.. i figure im supposed to use intent.putextra however im not sure how to pull the correct strings corresponding to the item that i click on.. my code is below.. im simply lost to be honest
//Initialize the ListView
lstTest = (ListView)findViewById(R.id.lstText);
//Initialize the ArrayList
alrts = new ArrayList<Alerts>();
//Initialize the array adapter notice with the listitems.xml layout
arrayAdapter = new AlertsAdapter(this, R.layout.listitems,alrts);
//Set the above adapter as the adapter for the list
lstTest.setAdapter(arrayAdapter);
//Set the click listener for the list
lstTest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int item, long arg3) {
Intent intent = new Intent(
HomePageActivity.this,
PromotionActivity.class
);
finish();
startActivity(intent);
}
});
my alerts class..
public class Alerts {
public String cityid;
public String promoterid;
public String promoshortcontent;
public String promocontent;
public String promotitle;
public String locationid;
public String cover;
#Override
public String toString() {
return "City: " +cityid+ " Promoter: " +promoterid+ "Short Promotion: " +promoshortcontent+ "Promotion: " +promocontent+ "Title: " +promotitle+ "Location: " +locationid+ "Cover: " +cover+ "$";
}
}
anddddd my alertsadapter class..
public class AlertsAdapter extends ArrayAdapter<Alerts> {
int resource;
String response;
Context context;
//Initialize adapter
public AlertsAdapter(Context context, int resource, List<Alerts> items) {
super(context, resource, items);
this.resource=resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout alertView;
//Get the current alert object
Alerts al = getItem(position);
//Inflate the view
if(convertView==null)
{
alertView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, alertView, true);
}
else
{
alertView = (LinearLayout) convertView;
}
//Get the text boxes from the listitem.xml file
TextView textPromo =(TextView)alertView.findViewById(R.id.txtPromo);
TextView textPromoter =(TextView)alertView.findViewById(R.id.txtPromoter);
TextView textLocation =(TextView)alertView.findViewById(R.id.txtLocation);
//Assign the appropriate data from our alert object above
textPromo.setText(al.promocontent);
textPromoter.setText(al.promoterid);
textLocation.setText(al.locationid);
return alertView;
}
}
You need to use the onItemClick event's parameters
a full more readable param enum with param name is
(AdapterView<?> parent, View view, int pos, long id)
that means you have the pos param that indicated the position in the adapter.
What you have to do is:
jump to pos in the adapter
read out the values from the adapter
use putExtra to signup for the intent
had an epiphany over the weekend about how to fix this problem and i finally found a good work around for my app.. i know it isnt optimal because i hard coded the number 100 into it but for my uses as of now i know i wont ever have that many list items..
i added these 2 bits of code to my alertsadapter class
int startzero = 0;
public static String[][] promomatrix = new String[6][100];
and
promomatrix[0][startzero] = al.cityid;
promomatrix[1][startzero] = al.promoterid;
promomatrix[2][startzero] = al.promocontent;
promomatrix[3][startzero] = al.promotitle;
promomatrix[4][startzero] = al.locationid;
promomatrix[5][startzero] = al.cover;
startzero++;
then went to my homepageactivity class and added this to the click listener
Intent intent = new Intent(
HomePageActivity.this,PromotionActivity.class);
intent.putExtra("listitemcity", AlertsAdapter.promomatrix[0][pos]);
intent.putExtra("listitempromoter", AlertsAdapter.promomatrix[1][pos]);
intent.putExtra("listitemcontent", AlertsAdapter.promomatrix[2][pos]);
intent.putExtra("listitemtitle", AlertsAdapter.promomatrix[3][pos]);
intent.putExtra("listitemlocation", AlertsAdapter.promomatrix[4][pos]);
intent.putExtra("listitemcover", AlertsAdapter.promomatrix[5][pos]);
finish();
startActivity(intent);
and finally went to my promotionactivity (where i was trying to send the strings) and added this
Bundle extras = getIntent().getExtras();
if (extras == null){
return;
}
String listitemcity = extras.getString("listitemcity");
String listitempromoter = extras.getString("listitempromoter");
String listitemcontent = extras.getString("listitemcontent");
String listitemtitle = extras.getString("listitemtitle");
String listitemlocation = extras.getString("listitemlocation");
String listitemcover = extras.getString("listitemcover");
worked like a charm.. i hope this helps someone :)

Categories

Resources