1

I have two activities. A,B. A loads 1st and then i open B activity from a button within A. When B opens i load values from sqlite to a listview. I want to get the selected values from one item on listview and after i click one row ill get transfered back to activity A and use those values. The thing is that for each row in listview it holds multiple value. Something like (Not actual code)list((String,String,String,String),(String,String,String,String)). So for each row i have 4 values or so. How can i chose the selected row and the values within?

Here is my code: Activity B

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_locations);
        // Show the Up button in the action bar.
        db = new DBAdapter(this);

        db.open();
        final Cursor c = db.getSavedLocations();
        locationsList = new ArrayList<String>();
        list = (ListView) findViewById(R.id.list);
        items = new ArrayList<SearchResults>();
        Log.d("","c count: "+c.getCount());
        if (c.moveToFirst()) {
            do {
                items.add(new SearchResults(c.getString(c
                        .getColumnIndex(DBAdapter.MY_ID)), c.getString(c
                        .getColumnIndex(DBAdapter.MY_COORD_LAT)), c.getString(c
                        .getColumnIndex(DBAdapter.MY_COORD_LONG)), c
                        .getString(c.getColumnIndex(DBAdapter.MY_COORD_NOTE)),
                        c.getString(c.getColumnIndex(DBAdapter.MY_COORD_DATE))) {
                });
            } while (c.moveToNext());
        }
        listAdapter = new ListAdapter(LoadLocationsActivity.this, R.id.list,
                items);
        list.setAdapter(listAdapter);
        db.close();

        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

            }
        });

This is the ListAdapter class i have. dont think its necessary to mention but here it is anw.

public class ListAdapter extends ArrayAdapter<SearchResults> {
    private ArrayList<SearchResults> entries;
    private Activity activity;

    public ListAdapter(LoadLocationsActivity a, int list,
            ArrayList<SearchResults> items) {
        super(a, list, items);
        this.entries = items;
        this.activity = a;
    }

    public static class ViewHolder {
        public TextView id;
        public TextView lati;
        public TextView longi;
        public TextView notes;
        public TextView date;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_row, null);
            holder = new ViewHolder();
            holder.lati = (TextView) v.findViewById(R.id.latLoad);
            holder.longi = (TextView) v.findViewById(R.id.longLoad);
            holder.notes = (TextView) v.findViewById(R.id.loadNote);
            holder.date = (TextView) v.findViewById(R.id.loadDate);
            holder.id = (TextView) v.findViewById(R.id.idLoad);
            v.setTag(holder);

        } else
            holder = (ViewHolder) v.getTag();
        final SearchResults sr = entries.get(position);
        if (sr != null) {
            holder.lati.setText(sr.getLatitude());
            holder.longi.setText(sr.getLongitude());
            holder.notes.setText(sr.getNotes());
            holder.date.setText(sr.getDate());
            holder.id.setText(sr.getLocationId());
            // holder.item2.setText(custom.getSecond());
            // Log.d("", sr.getLatitude().toString() + " lat");
            // Log.d("", sr.getLongitude().toString() + " long");
            // Log.d("", sr.getLocationId().toString() + " id");
            // Log.d("", sr.getNotes().toString() + " note");
            // Log.d("", sr.getDate().toString() + " date");
        }
        return v;
    }

}

Edit: I guided you wrong people sorry about that. I didnt mean on how can i get the values from that row. But how to get the listview row it self. I changed now my description.

lantonis
  • 143
  • 14

3 Answers3

0

First, Use startActivityForResult instead of startActivity when you navigate to B from Activity A. And the Second thing is, Put all of those values that you want to transfer into one Object and Send the object only in the Bundle. Or you can also insert those 4 different values into bundle separately and you will need to use Key, Value pairs for it. So you will know which values is for when you get them back in Activity A.

osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • But how will i get the values that i want to in order to get them into object? From onItemClick its the arg0, or arg1? position i know for sure its the selected listview row number:P – lantonis Dec 06 '13 at 09:44
  • You already have your items in the List, ArrayList entries. When you click on the List Item, you get your related Object based on the position. `SearchResults sr = entries.get(position);` – osayilgan Dec 06 '13 at 09:58
  • This is the Object you are supposed to send. – osayilgan Dec 06 '13 at 09:59
  • No problem. Btw, as far as I see, you are trying to use ViewHolder patter in your adapter. And you are setting your UI values inside the ELSE statement. You need to move them out of the else statement. In the if/else statement you need to initialise the ViewHolder, if it is not initialised (in the if statement) you init them and set the tag. If it is initialised before then you get the ViewHolder instance from TAG. Rest is supposed to be out of the If / Else. – osayilgan Dec 06 '13 at 10:27
  • Well the code works, i didnt get any errors. I used to have some log.d messages to see what happens and inside the if it only gets the 1st time. After that it goes to else. Inside if it initializes the textviews and after that in else it sets the string values. – lantonis Dec 06 '13 at 10:53
  • It depends on how many items you have in your list. If you don't have that many items, your device's memory is enough to keep them in the memory. But when you have too many items, you will have problem. – osayilgan Dec 06 '13 at 11:32
  • ill change that then. Thank you for the heads up m8 – lantonis Dec 06 '13 at 11:51
0

you could set extras when you start the intent to Activity A from B. Like intent.putExtras or something. And since you have multiple data's there you could use an array type in the extras. Also if you want to be more manipulative of the data ... for example you want to control the (String, String, String) output ... say just want to get the String according to your need, you could resolve to using ArrayList<HashMap<String, String>> and use the serializable type in the putExtras. And because you are loading the activity A first, I recommend you try and catch the Bundle(getExtras) with a NullPointerException.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
0

You have to Override getItem() in your adapter class. See this.

to get result from activity See this and this.

Community
  • 1
  • 1
R9J
  • 6,605
  • 4
  • 19
  • 25