如何从列表视图中获取YouTube视频

时间:2018-06-05 12:03:22

标签: android listview android-intent onitemclicklistener

我有一个包含两个文本视图的适配器的列表视图。 当我按下列表视图中的特定项目时,我想达到一个特定的意图。例如;如果项目有textview1 =“a”和textview2 =“b”,那么

if(textview2=="b") { do something. }

我的问题是,我如何处理textview2。

感谢

2 个答案:

答案 0 :(得分:0)

尝试在listview适配器中实现此功能。

  1. 在listview函数中初始化listview适配器中的textview1和textview2。
  2. 现在根据要求对受尊重的textview进行单独的点击操作。

    public View getView(final int position, View convertView, ViewGroup parent) {
    
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View itemView = inflater.inflate(R.layout.sample_layout_item, parent, false);
    
    
    
       TextView textview1 = (TextView) itemView.findViewById(R.id.textview1);
       TextView textview2 = (TextView) itemView.findViewById(R.id.textview2);
    
    
       textview1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
    
               //Write the code that is required for textview1 onclick.
           }
       });
    
        textview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                //Write the code that is required for textview2 onclick.
            }
        });
    
    
    
    
    
        return itemView;
    
    }
    

答案 1 :(得分:0)

适配器代码:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class injuryadapter extends ArrayAdapter<injury>  {
    Context context;
    List<injury> objects;
    public injuryadapter(Context context, int resource, int textViewResourceId, List<injury> objects)
    {
        super(context, resource, textViewResourceId, objects);
        this.context=context;
        this.objects=objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater layoutInflater = ((Activity)context).getLayoutInflater();
        final View view = layoutInflater.inflate(R.layout.activity_injuryadapter,parent,false);

        TextView injuryzone = (TextView)view.findViewById(R.id.injury_zone);
        final TextView injurytype = (TextView)view.findViewById(R.id.injury_type);

        injury temp = objects.get(position);

        injuryzone.setText(String.valueOf(temp.getInjuryZone()));
        injurytype.setText(temp.getInjuryType());

        return view;
    }

}

列表视图显示的活动,我想从列表视图中的某个项目上按下该活动,然后转到youtube上的特定视频:

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    public ListView lv;
    public static ArrayList<injury> injuryList;
    Button bt1;
    injuryadapter injuryadapter;
    FirebaseAuth firebaseAuth;
    FirebaseUser firebaseUser;
    String vidid;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = (Button) findViewById(R.id.add_injury);
        bt1.setOnClickListener(this);
        firebaseAuth = FirebaseAuth.getInstance();
        firebaseUser = firebaseAuth.getCurrentUser();
        lv = (ListView) findViewById(R.id.lv);
        injuryList = new ArrayList<injury>();
        injuryadapter= new injuryadapter(this,0,0,injuryList);
        lv.setAdapter(injuryadapter);
        lv.setOnItemClickListener(this);
        showall();
    }

    public void showall()\\ method to read the extract the users data from the firebase and put it in the list view
    {
        FirebaseDatabase.getInstance().getReference().child("injury").addListenerForSingleValueEvent(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                for (DataSnapshot child : dataSnapshot.getChildren())
                {
                    injury k = child.getValue(injury.class);
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    if(k.getUid().equals(user.getUid()))
                    {
                        injuryList.add(k);
                    }
                }
                injuryadapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
            }
        });
    }


    public  void signout()
    {

        if (firebaseUser!=null)
        {
            firebaseAuth.signOut();
        }
    }
    @Override
    public boolean onCreateOptionsMenu (Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.bhp:
                Intent intent = new Intent(this, Main2Activity.class);
                startActivity(intent);
                return true;

            case R.id.logout:
                signout();
                Intent intent1 = new Intent(this, Main2Activity.class);
                startActivity(intent1);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view)
    {
        if (bt1==view)
        {
        Intent intent= new Intent(this, addinjury.class);
        startActivity(intent);
        }

    }

我首先想到用setItemOnClickListener()方法收听列表视图,但我不知道如何处理每个项目的内容。

受伤等级:

 public class injury
{

        private String InjuryZone;
        private String InjuryType;
        private String Uid;


        public injury()
        {

        }
        public injury(String injuryZone, String injuryType, String uid)
        {
            InjuryZone = injuryZone;
            InjuryType = injuryType;
            Uid = uid;
        }

        public String getInjuryZone()
        {
            return InjuryZone;
        }

        public void setInjuryZone(String injuryZone)
        {
            InjuryZone = injuryZone;
        }

        public String getInjuryType()
        {
            return InjuryType;
        }

        public void setInjuryType(String injuryType)
        {
            InjuryType = injuryType;
        }

        public String getUid()
        {
        return Uid;
        }

        public void setUid(String uid)
        {
        Uid = uid;
        }

}