打开活动并根据ListView选择填充

时间:2017-06-02 19:23:12

标签: java android listview

我尝试将数据从自定义列表视图传递到新活动并填充其TextView。它确实有效,但总是需要添加的第一个位置并将其传递出去。如何让它传递点击的数据?

MainActivity

package uk.co.jaunt_app.jaunt;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

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

import static uk.co.jaunt_app.jaunt.R.id.Posts;

public class MainActivity extends AppCompatActivity {

    private FloatingActionButton addmap;
    private Button settingbtn;
    private TextView mNameTextView;
    private TextView mEmailTextView;
    ListView mPosts;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    ArrayAdapter<String> adapter;
    private FirebaseAuth auth;
    private ArrayList<feed> feedPop = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.drawable.jauntlogodark);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        setContentView(R.layout.activity_main);

        Firebase.setAndroidContext(this);

        Firebase ref = new Firebase(Config.FIREBASE_URL);
        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        String uid = currentFirebaseUser.getUid();
        Firebase usersRef = ref.child("Users").child(uid).child("name");
        Firebase emailRef = ref.child("Users").child(uid).child("email");
        Firebase postRef = ref.child("Users").child(uid).child("Maps");

        postRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (final DataSnapshot postSnapshot : snapshot.getChildren()) {
                    //Getting the data from snapshot

                    Person person = postSnapshot.getValue(Person.class);
                    final String mapName = person.getMapName();
                    final String mapid = person.getMapID();
                    final String mapStartLat = person.getStartLat();
                    final String mapStartLong = person.getStartLong();
                    final String mapEndLat = person.getEndLat();
                    final String mapEndLong = person.getEndLong();

                    adapter.notifyDataSetChanged();

                    feedPop.add(
                            new feed(mapName, mapStartLat, mapStartLong, mapEndLat, mapEndLong));

                    final ArrayAdapter<feed> adapter = new feedArrayAdapter(MainActivity.this, 0, feedPop);

                    ListView listView = (ListView) findViewById(R.id.customListView);
                    listView.setAdapter(adapter);

                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {

                            Intent intent = new Intent(MainActivity.this, ViewUserMapActivity.class);
                            intent.putExtra("mapName", mapName.toString());
                            intent.putExtra("mapStartLat", mapStartLat.toString());
                            intent.putExtra("mapStartLong", mapStartLong.toString());
                            intent.putExtra("mapEndLat", mapEndLat.toString());
                            intent.putExtra("mapEndLong", mapEndLong.toString());
                            startActivity(intent);
                        }
                    });


                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Toast.makeText(getApplicationContext(),
                        "Cancelled", Toast.LENGTH_LONG)
                        .show();
            }
        });

        //create property elements


        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        mPosts = (ListView) findViewById(Posts);
        mPosts.setAdapter(adapter);

        List<Nav> navList= new ArrayList<Nav>();
        navList.add(new Nav("Profile"));
        navList.add(new Nav("Feed"));
        navList.add(new Nav("Maps"));
        navList.add(new Nav("Most Popular"));
        navList.add(new Nav("Settings"));
        navList.add(new Nav("Report a Bug"));

        ArrayAdapter<Nav> navadapter = new ArrayAdapter<Nav>(this,android.R.layout.simple_list_item_1, navList);
        final ListView lv= (ListView) findViewById(R.id.left_drawer);
        lv.setAdapter(navadapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int itemPosition = position;

                switch(itemPosition) {

                    case 0:
                        Intent menuItem = new Intent(MainActivity.this, UserMapActivity.class);
                        startActivity(menuItem);
                        break;
                    case 1:
                        menuItem = new Intent(MainActivity.this, UserMapActivity.class);
                        startActivity(menuItem);
                        break;
                    case 2:
                        menuItem = new Intent(MainActivity.this, UserMapActivity.class);
                        startActivity(menuItem);
                        break;

                    case 3:
                        menuItem = new Intent(MainActivity.this, UserMapActivity.class);
                        startActivity(menuItem);
                        break;

                    case 4:
                        menuItem = new Intent(MainActivity.this, SettingsActivity.class);
                        startActivity(menuItem);
                        break;

                    case 5:
                        menuItem = new Intent(MainActivity.this, UserMapActivity.class);
                        startActivity(menuItem);
                        break;
                }
            }
        });

        mNameTextView = (TextView) findViewById(R.id.NameTextView);

        usersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String name = dataSnapshot.getValue(String.class);
                mNameTextView.setText(name);

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        mEmailTextView = (TextView) findViewById(R.id.EmailTextView);

        emailRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String email = dataSnapshot.getValue(String.class);
                mEmailTextView.setText(email);

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });



        addmap = (FloatingActionButton) findViewById(R.id.add_map);
        addmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Answers.getInstance().logCustom(new CustomEvent("Added Map"));
                Intent intent = new Intent(MainActivity.this, MapStartActivity.class);
                startActivity(intent);
            }
        });

        settingbtn = (Button) findViewById(R.id.settingbtn);
        settingbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_menu_drawer, menu);
        return true;
    }

}

//custom ArrayAdapter
class feedArrayAdapter extends ArrayAdapter<feed>{

    private Context context;
    private List<feed> feedPop;

    //constructor, call on creation
    public feedArrayAdapter(Context context, int resource, ArrayList<feed> objects) {
        super(context, resource, objects);

        this.context = context;
        this.feedPop = objects;
    }

    //called when rendering the list
    public View getView(int position, View convertView, ViewGroup parent) {

        //get the property we are displaying
        feed feed = feedPop.get(position);

        //get the inflater and inflate the XML layout for each item
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.feed_listview, null);

        TextView others = (TextView) view.findViewById(R.id.others);
        TextView mapname = (TextView) view.findViewById(R.id.address);

        //set address and description
        String completeOthers = feed.getFeedStartLat() + ", " + feed.getFeedStartLong() + ", " + feed.getFeedEndLat() + ", " + feed.getFeedEndLong();
        others.setText(completeOthers);

        //set address and description
        String completeAddress = feed.getFeedName();
        mapname.setText(completeAddress);

        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

根据位置从ArrayList获取值,然后传递给activity

intent.putExtra("mapName", feedPop.get(position).getmapName().toString());