2012-08-23 57 views
0

我有一個listactivity應用程序,每行cotain文本和按鈕,文本和按鈕都必須是可點擊的(我試圖得到它),當點擊文本時,它會打開MyDay活動和當點擊按鈕時,它將打開My_videos活動。帶文本和按鈕的Listactivity點擊奇怪的行爲

當你打開應用程序,點擊任何按鈕,什麼確切地發生,它做的沒有點擊的動作在所有行任何按鈕,但是當你點擊任何一行文本,以便它會打開MyDay活動,然後單擊任何按鈕第一行僅爲,它將打開視頻視圖的My_videos活動,但同時其他按鈕在其餘行中不可點擊。

任何意見將不勝感激,

謝謝。

我的代碼:

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(); } 

             } } 

回答

0

您正在設置不正確的聽衆。爲了讓收聽者工作,您必須單擊ListView行一次才能實際設置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> 
+0

非常感謝,它的工作很好,還有一件事你在你的適配器中提到(if/else塊應該改進,但我不知道你的確切代碼),你還指的是我已經發布的完整代碼在這裏,你能解釋pleaseeeeeee –

+0

@AndroidStack檢查我發佈在我的答案的鏈接。我添加了一段額外的代碼。看看這是你想做的。 – Luksprog

+0

你是驚人真的,再次感謝,我是這麼希望每個按鈕於各行的不同的圖像,我只是分配 所有按鈕創建我的應用程序的開始ic_launcher只是爲了方便uploade它 到我的手機來檢查它,但現在我面臨另一個問題,這也是我想每個按鈕的每行不同的視頻,我修改 My_videos類,但它不工作,當你點擊按鈕顯示空的videoview,我會用它更新我的文章,請檢查它,thankssssssssssssssss –

0

您在onItemCLik方法做錯了定義點擊收聽

只設置在底座適配器的getView方法的TextView一個點擊監聽器和按鈕....... I,E從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; }} 
+0

我申請這方面,我有紅色標記錯誤Y可以解釋更多,寫詳細的代碼,請,謝謝 –

+0

你得到了什麼錯誤......... ... –