0

I have a listview. When one of items clicked, it is showing now only textview. But under textview, i will put a map. It must take also location.

So, i will create a new class, a new activity when clicked. But i need to send this location and textview text. How can i send?

Now i am putting only the class of listview. Right now, i have 2 views, when clicked other view is coming. No fragment, no activity:

package cursedchico.showmeevets;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

 public class ListEvents extends AppCompatActivity {

    private String type;

    ListView lv;
    private TextView desc;
    private View listView, showView;
    private Context mContext ;
    DBHandler db;
    private List<Event> events;
    ProgressDialog dialog;

   // @Override
   // public void onBackPressed(){

      //  setContentView(listView);
   // }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "ListEvents activity oncreate");

        mContext = getBaseContext();
        db = new DBHandler(mContext);
      if(db==null)  Log.v(TAG, "null ");
        if(mContext==null)  Log.v(TAG, "mnull ");

        Intent myIntent = getIntent(); // gets the previously created intent
        type = myIntent.getStringExtra("Type");
        Log.v(TAG, "Type: " + type);

        listView = getLayoutInflater().inflate(R.layout.activity_list, null);
        showView = getLayoutInflater().inflate(R.layout.show_description, null);

        setContentView(listView);
        lv = (ListView) findViewById(R.id.list);
        Log.v(TAG,"setadapter oncesı");
        int[] colors = {0, 0xFFFF0000, 0}; // red for the example
        lv.setDivider(new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, colors));
        lv.setDividerHeight(1);
        events  = new ArrayList();
        events = db.getSomeEvents(type);

//burdan baska bir ekrana gitmesi iyi olur cunku duruyor bos bos oyle
        if(events==null)  { Toast.makeText(getApplication(), "ETKINLIK BULUNAMADI ...",
                Toast.LENGTH_LONG).show();}
else {
            lv.setAdapter(new CustomAdapter(this, events));
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    Log.v(TAG, "tıklandi, şurdaki: " + position);
                    //  Object o = lv.getItemAtPosition(position);
                    //Log.v(TAG, "tıklandi1 desc: " + events.get(position).getDescription());
                    //  Wrapper wra = (Wrapper)o;
                   // Log.v(TAG, "tıklandi2");
                    desc = (TextView) showView.findViewById(R.id.desc);
                    //Log.v(TAG, "tıklandi3");
                    desc.setText(events.get(position).getDescription());
                    Log.v(TAG, "desc view geçiş");
                    setContentView(showView);
                }//geri tusuna basınca maine gidiyor
            });
        }//else sonu
    }//oncreate sonu
}

Maybe i can put map into this layout and let it stay inside this class but it will be very complicated. I dont know how to use fragments, a new activity seems good but i dont know how to send when creating intend. Can i send via creating construct of that map class but this time how can that map class will take the view and make layout?

TerNovi
  • 390
  • 5
  • 16
  • its best if you create an array that holds the items (text,lat,lng) and populate the list using the array. when you click on the item (onItemClick) you have the position id. use that id to grab the (text,lat,lng) from the array. if you are going to use another activity then check here on how to pass data from one activity to the next -- http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data – Tasos Dec 29 '15 at 19:51
  • I already have an wrapper. but icant pass object of it. –  Dec 29 '15 at 19:52
  • i can pass only string? –  Dec 29 '15 at 19:53

3 Answers3

0

You can use serialization.

 //To pass:
myIntent.putExtra("MyList", lv);

// To retrieve object in second Activity
getIntent().getSerializableExtra("MyList");
Viktor
  • 53
  • 1
  • 4
0

You can save some data with SharedPreferencesLINK TUTORIAL) or put data in Intent with methods:

Intent i = new Intent(CurrentActivity.this,NextActivity.class) ; String[] tab = {"1","2","3"}; i.putExtra("arrayName",tab); startActivity(i);

You have also method for Intent like: i.putStringArrayListExtra(...)

And in next activity you can get back this values with:

String[] tabFromPreviousActivity = getIntent().getExtras().getStringArray("arrayName");
taulus
  • 69
  • 8
  • i have event class. i want to pass Event event = new event. but it says it is deprecated –  Dec 29 '15 at 19:50
  • If you wan't to send whole object with intent you must use serialization(like Viktor said). What method is deprecated? – taulus Dec 30 '15 at 15:49
0

If I understand your question correctly.

You have a listview filled with "events". You want to set a listener that opens another activity when an event is clicked on.

You have your items in here.

private List<Event> events;

On your setItemClickListener you need to get the event from the list according to the position that was clicked on the list.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    Intent intent = new Intent(getApplicationContext(), YourOtherActivity.class ); //Here YourOtherActivity.class is the Activity that you will start up to show your Event.
                    intent.putSerializable("EVENT_KEY", events.getPosition(position); //Here you put your "EVENT" on the intent.
                    getApplicationContext().startActivity(intent); // This will start your new activity with the Intent you used to start it.
                }
            });

Once you have started your activity you need to get the get the intent and retrieve the EVENT that you passed.

    public class YourOtherActivity extends Activity{
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.your_other_activity_layout); //Set layout for current activity
           Event eventFromFirstActivity = (Event) getIntent().getExtras().getSerializable("EVENT_KEY"); 
/* This part is the part where you get the event from previous intent. You have to read it as a serialiazable since the example I am 
    doing for you stores it in a Serializable. Notice that you also do a type cast to Event.*/

       }

    }

In order for this to work you need to make sure your Event class implements Serializable.

import java.io.Serializable;
    public class Event implements Serializable{

}

Finally make sure you create your other activity properly first. Make sure you configure the Activity inside your AndroidManifest.xml

TerNovi
  • 390
  • 5
  • 16
  • You can note that put as Serializable might give you a warning saying it is deprecated. The new style is Parcelable. But it requires more work and for a beginner it might be confusing to make your Object implement a Parcelable first. – TerNovi Dec 29 '15 at 21:40