views:

64

answers:

2

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

}

A: 

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
Pentium10
to be honest ive only been programming for about a week now.. everything ive accomplished has been through researching tutorials on google and using this site.. i know how to do that third bullet of yours so ive been trying to research the first two to no avail.. any way you could write up some sample code or show me a tutorial of how to read values at a certain position in an adapter?
dootcher
ArrayAdapter has a `getItem` method you can use to get the item at pos. That is probably a String, so you are done also with step 2. It will be something like : `String myItem = HomePageActivity.this.arrayAdapter.getItem(item)`
Pentium10
k if im understanding that code right then that would work for a listview that was populated by list items with only 1 corresponding string each.. however i neglected to mention that each list item has multiple strings corresponding to it.. check my code above that i just added.. its my Alerts class and my AlertsAdapter class that extends ArrayAdapter<Alerts> so i imagine this makes it a bit more complex.. sorry :-/
dootcher
You are already doing the code you need in getView(). Alerts al = getItem(position);textPromo.setText(al.promocontent); textPromoter.setText(al.promoterid); textLocation.setText(al.locationid);
Pentium10
but isnt that getItem used to pull certain strings from the alerts class and send them to my homepageactivity class (the first block of text up there) for every item in the list? im needing to pull the corresponding strings that were received from the alertsadapater class for a specific item in the list.. correct me if im wrong cuz you gotta remember ive only been at this for a week :)
dootcher
A: 

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 :)

dootcher