0

我有一个 listactivity 应用程序,每一行包含 TEXT 和 BUTTON,文本和按钮都必须是可点击的(我试图得到它),当单击文本时它将打开 MyDay 活动,当单击按钮时它将打开 My_videos 活动。

当您打开应用程序并单击任何按钮时会发生什么,它不会在所有行中的任何按钮中单击操作,但是当您单击任何行文本时,它将打开 MyDay 活动,然后 仅单击第一行中的任何按钮,它将打开 My_videos 活动,即 videoview,但同时其他按钮在其余行中不可点击。

任何建议将被认真考虑 ,

谢谢。

我的代码:

MyArrayAdapter 类:

 public class MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] classes;
Button bt1, bt2, bt3, bt4, bt5;   

Typeface tf;

public MyArrayAdapter(Activity context, String[] classes) {
    super(context, R.layout.row, classes);
    this.context = context;
    this.classes = classes;
              }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    tf=Typeface.createFromAsset(context.getAssets(),"BFantezy.ttf"); 
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.row, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.row_label);
    Button bt1=(Button) rowView.findViewById(R.id.button1); 
    Button bt2=(Button) rowView.findViewById(R.id.button2); 
    Button bt3=(Button) rowView.findViewById(R.id.button3); 
    Button bt4=(Button) rowView.findViewById(R.id.button4); 
    Button bt5=(Button) rowView.findViewById(R.id.button5); 

    String s = classes[position];
    textView.setText(s);
    ((TextView)textView).setTypeface(tf); 


if ( s.startsWith("First")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher);
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }

if ( s.startsWith("Second")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }    

if ( s.startsWith("Third")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }        

if ( s.startsWith("Fourth")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }    

if ( s.startsWith("Fifth")) {

    bt1.setBackgroundResource(R.drawable.ic_launcher); 
    bt2.setBackgroundResource(R.drawable.ic_launcher); 
    bt3.setBackgroundResource(R.drawable.ic_launcher); 
    bt4.setBackgroundResource(R.drawable.ic_launcher); 
    bt5.setBackgroundResource(R.drawable.ic_launcher); }        

return rowView;  }}

My_videos 类:

  public class My_videos extends Activity {  
private VideoView vid;  
String night;  
/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.video);    

    Bundle bdl = getIntent().getExtras(); 
    night = bdl.getString("video");   

    vid = (VideoView) findViewById(R.id.videoView1); 

      if (night.equalsIgnoreCase("button1")) {    
         vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()
                        + "/" + R.raw.b));
         vid.setMediaController(new MediaController(My_videos.this));  
             vid.requestFocus();    
             vid.start(); }  

     else if (night.equalsIgnoreCase("button2")) {   
        vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
                       + "/" + R.raw.bb)); 
         vid.setMediaController(new MediaController(My_videos.this));
             vid.requestFocus(); 
             vid.start(); } 

     else if (night.equalsIgnoreCase("button3")) { 
     vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
                    + "/" + R.raw.bbb)); 
         vid.setMediaController(new MediaController(My_videos.this));
             vid.requestFocus();
             vid.start(); } 

     else if (night.equalsIgnoreCase("button4")) { 
     vid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
                    + "/" + R.raw.bbbb)); 
         vid.setMediaController(new MediaController(My_videos.this)); 
             vid.requestFocus();     
             vid.start(); } 

     else if (night.equalsIgnoreCase("button5")) {   
     vid.setVideoURI(Uri.parse("android.resource://" + getPackageName()  
                    + "/" + R.raw.bbbbb)); 
         vid.setMediaController(new MediaController(My_videos.this));   
             vid.requestFocus();      
             vid.start(); }  

                                      } } 
4

2 回答 2

0

您在 onItemCLik 方法中定义单击侦听器时做错了

只需在基本适配器的 getView 方法中为 TextView 和 Button 设置一个 CLick 侦听器......即,从 OnItemClikc 方法中删除所有点击侦听器

public View getView(final int position, View convertView, ViewGroup parent) {
    tf=Typeface.createFromAsset(context.getAssets(),"BFantezy.ttf"); 
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.row, null, true);
    TextView textView = (TextView) rowView.findViewById(R.id.row_label);
    textView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // do what ever do want to ie open activity 
            //must use postion to get that item  
        }
    });
    Button bt1=(Button) rowView.findViewById(R.id.button1); 
    bt1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();    
            intent.setClass(ListButton.this, My_videos.class); 
            //this will send the respective  string to intent class ; 
            intent.putExtra("video", classes[position].toString);

        }
    });
return rowView;  }}
于 2012-08-23T08:51:00.927 回答
0

您设置的侦听器不正确。为了使侦听器工作,您必须单击该ListView行一次以实际设置 and 的侦听ButtonsTextView。正确的方法是在getView布局视图时在方法中设置侦听器。

首先删除onListItemClick回调中的代码,然后修改适配器。我已将您的适配器重写为更高效的版本(尽管可以进行更多改进)。由于代码很大,您可以在这里找到新的适配器https://gist.github.com/3463215。看看这是不是你想要的。

R.layout.video布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="One" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:text="Two" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="three" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Four" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Five" />

</LinearLayout>

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>
于 2012-08-25T10:12:17.970 回答