Question

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

}

Was it helpful?

Solution 2

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top