0

i have a listactivity app , each row cotain TEXT and BUTTON , both text and button must be clickable (what im try to get it ) , when click the text it will open MyDay activity and when click button it will open My_videos activity .

whats happen exactly when you open the app and click on any button , it do no click action in any button in all rows , but when you click any row text so it will open MyDay activity then click any button in the first row ONLY , it will open My_videos activity which is videoview , but in the same time the other buttons is not clickable in the rest of rows .

any advice will be appreciated ,

THANKS.

MY CODE :

MyArrayAdapter Class:

 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 Class:

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

                                      } } 
Android Stack
  • 4,314
  • 6
  • 31
  • 49

2 Answers2

0

you are doing wrong in defining click listener in onItemCLik method

just set a CLick Listener for TextView and Button in getView method of base adapter ....... i,e Remove all the click listener from OnItemClikc method

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;  }}
Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
0

You are setting the listeners incorrect. In order for the listeners to work you would have to click the ListView row once to actually set the listeners for the Buttons and TextView. The correct way would be to set the listeners in the getView method when you layout the views.

First delete the code you have in the onListItemClick callback and then modify the adapter. I've rewritten your adapter to a more efficient version(although more improvements could be made). As the code is big you can find the new adapter here https://gist.github.com/3463215 . See if this is what you want.

The R.layout.video layout file:

<?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>
user
  • 86,916
  • 18
  • 197
  • 190
  • thanks indeed , its work fine , one more thing you mentioned in your adapter (if/else block should be improved but I don't know your exact code), what do you mean also the full code i already post it here , can you explain pleaseeeeeee – Android Stack Aug 25 '12 at 13:52
  • @AndroidStack Check the link I posted in my answer. I added an extra piece of code. See if that is what you want to do. – user Aug 25 '12 at 14:12
  • you are amazing really, thanks again ,thats what i want a different images for each button for each row, i just assign all button to ic_launcher in the beginning of creating my app just to facilitate uploade it to my mobile to check it , but now i face another issue which is also i want a different videos for each button for each row, i modify My_videos Class but its not work when you click the button it show empty videoview , i will update my post with it, please check it , thankssssssssssssssss – Android Stack Aug 25 '12 at 15:16
  • @AndroidStack I've added an improved `My_videos` class, see the link from my answer. Also there are some modifications in the `MyArrayAdapter` class, don't forget to add them too. – user Aug 25 '12 at 16:05
  • my dear sir i get force close , updated logcat , please check it thanks – Android Stack Aug 25 '12 at 16:58
  • @AndroidStack Which one is line 47 in the activity `My_videos`? – user Aug 25 '12 at 17:05
  • this is the line 47 : bt1.setTag(0); – Android Stack Aug 25 '12 at 17:15
  • @AndroidStack Check your layout and make sure you have the five buttons with the id `button1`, `button2` etc. Also I wrote the layout `R.layout.misc_video` for tests, in your activity you used `R.layout.video` so make sure you have the correct layout set. – user Aug 25 '12 at 17:18
  • @AndroidStack The buttons are `null` because you either don't have them in the layout(with the correct ids) or the layout file is wrong, so again double check the layout file. You could also try to clean the project(go to the menu Project -> Clean) and then re run your app. – user Aug 25 '12 at 18:22
  • im driving to home now , when i reach i will check it and reply you , thanks – Android Stack Aug 25 '12 at 18:29
  • @AndroidStack In `My_videos` class you set the content view to `R.layout.video`(`setContentView(R.layout.video);`) but you showed my the row layout used in the adapter. In the `R.layout.video` you should have a `VideoView` with the id `@+id/videoView1` and besides this 5 `Buttons` with the ids `@+id/button1`, `@+id/button2` etc. – user Aug 26 '12 at 04:50
  • @AndroidStack In the layout file `R.layout.video` that you used for the `My_videos` activity you **DON'T HAVE the 5 buttons** that you look in the `onCreate` method. Add those 5 buttons to the `R.layout.video` and all should be ok. See my edited answer. – user Aug 26 '12 at 12:48
  • i applied that this will open videoview with 5 button inside , when you press in any button inside videoview it will display video but i want to display video from the list it self not from inside the videoview , videoview just to stream video which intiated by press any button in the list and sure each button will display different video than other finally i will have 15 button with 15 video , you get me my friend – Android Stack Aug 26 '12 at 13:42
  • @AndroidStack I didn't quite understand what you want, I'm not very experienced with the `VideoView`. Anyway you should ask a new question with the new problem as we are out of the original question topic and you should always have one question per problem. – user Aug 26 '12 at 17:04
  • thanks for endless support and help , i will write another quetion and need also your help in it please ,thanks alot – Android Stack Aug 27 '12 at 05:06
  • Luksprog i wrote another post would you please chek it my dear friend : http://stackoverflow.com/questions/12138393/listactivity-with-multiple-button-and-multiple-videos – Android Stack Aug 27 '12 at 08:16
  • i wrote another post related to this post would you please chek it my dear friend please : http://stackoverflow.com/questions/12138393/listactivity-with-multiple-button-and-multiple-videos – Android Stack Aug 27 '12 at 15:54
  • my friend in both my post which you answered , the app run nicly but when change mobile to landscape mode and scroll the list it gave NullPointerException at this line in MyArrayAdapter ( holder.textView.setText(s); ) , any help my dear sir please ,thanks . my posts :this post . and the other one is : http://stackoverflow.com/questions/12138393/listactivity-with-multiple-button-and-multiple-videos – Android Stack Aug 31 '12 at 07:09
  • Luksprog i post it as question would you please check it my dear , thanks http://stackoverflow.com/questions/12211078/listactivity-with-multiple-buttons-nullpointerexception-with-scrolling-list-in-l – Android Stack Aug 31 '12 at 07:56