Results 1 to 7 of 7
  1. #1
    terry is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Default Touchable listview and buttons

    Hi all,

    I am trying to do something which I thought would be easy but it is turning into a nightmare. I would like to have a touchable listview and two image buttons underneath.

    Eclipse allows me to do this but I suspect the touchable listview won't work.

    public class MainActivity extends ListActivity implements OnClickListener {
    .....


    but I can't use

    insClick.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    .......
    }
    });

    because the IDE does not like setOnClickListener, OnClickListener and onClick.

    I have searched around the internet and tried every combination of the code, but without luck. Can anyone please point me in the right direction to allow a combination of listview and image buttons..

    Thanks in advance,

    Terry

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Touchable listview and buttons

    you should put this question in Android sub-forum..

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Touchable listview and buttons

    Can you tell me what Eclipse is angry about? If you hover over the errors, what does it say? Also, can you show where you defined/initialized insClick? That may be where the problem lies.

    EDIT:
    Here's a quick fix if what you need to do is something easy. Inside of your XML file, add:
    Java Code:
    android:onClick="doThis"
    to your button.

    Then inside of your java class, create a method called doThis that takes a View as a parameter:
    Java Code:
    public void doThis(View view) {
    //whatever you want to happen when you click the button goes here
    }
    Obviously doThis can be changed to any other name if you want it to be different.
    Last edited by AlexGraal; 07-26-2014 at 12:31 AM.

  4. #4
    terry is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Default Re: Touchable listview and buttons

    Quote Originally Posted by AlexGraal View Post
    Can you tell me what Eclipse is angry about? If you hover over the errors, what does it say? Also, can you show where you defined/initialized insClick? That may be where the problem lies.

    EDIT:
    Here's a quick fix if what you need to do is something easy. Inside of your XML file, add:
    Java Code:
    android:onClick="doThis"
    to your button.

    Then inside of your java class, create a method called doThis that takes a View as a parameter:
    Java Code:
    public void doThis(View view) {
    //whatever you want to happen when you click the button goes here
    }
    Obviously doThis can be changed to any other name if you want it to be different.
    Hi AlexGraal,

    thanks for your response. Before I will try your quick fixes, here are the answers to yozur questions.

    insClick is defined in Java as:

    insClick.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address",
    "com.package.address.MainActivity"));
    startActivity(intent);
    }
    });

    and in XML as:

    <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/blood" />


    Eclipse is angry about the following:

    over setOnClickListener Eclipse says:

    The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})

    Is this because the class is initiated with: public class MainActivity extends ListActivity implements OnClickListener

    over onClick Eclipse asked me to remove the @Override, which I have done.

    After trying your quickfixes, the error still remains at setOnClickListener.

    Terry

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Touchable listview and buttons

    Well the issue is that you have the wrong OnClickListener imported.


    You probably have
    Java Code:
    import android.content.DialogInterface.OnClickListener;
    at the top of your file in the imports.

    Looks like your error says it wants a View.OnClickListener there. Just remove that import statement and add
    Java Code:
    import android.view.View.OnClickListener;
    and your problem should be gone!




    And Sorry, I should have been more clear with the quickfix.
    It should have been used on its own - not in conjunction with the OnClickListener. It replaces it. They are two ways to do the same thing.

    So, if you add
    Java Code:
    android:onClick = "insButtonPress"
    to your XML file for the button,

    and create a method with the contents of the onClick that you just showed me that takes a View as a parameter and is called "insButtonPress", you're done.

    Just get rid of this:
    Java Code:
    insClick.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address",
    "com.package.address.MainActivity"));
    startActivity(intent); 
    }
    });
    Instead, scroll down to the bottom and create the method I explained.
    Last edited by AlexGraal; 07-27-2014 at 05:03 PM.

  6. #6
    terry is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Default Re: Touchable listview and buttons

    Quote Originally Posted by AlexGraal View Post
    Well the issue is that you have the wrong OnClickListener imported.


    You probably have
    Java Code:
    import android.content.DialogInterface.OnClickListener;
    at the top of your file in the imports.

    Looks like your error says it wants a View.OnClickListener there. Just remove that import statement and add
    Java Code:
    import android.view.View.OnClickListener;
    and your problem should be gone!




    And Sorry, I should have been more clear with the quickfix.
    It should have been used on its own - not in conjunction with the OnClickListener. It replaces it. They are two ways to do the same thing.

    So, if you add
    Java Code:
    android:onClick = "insButtonPress"
    to your XML file for the button,

    and create a method with the contents of the onClick that you just showed me that takes a View as a parameter and is called "insButtonPress", you're done.

    Just get rid of this:
    Java Code:
    insClick.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address",
    "com.package.address.MainActivity"));
    startActivity(intent); 
    }
    });
    Instead, scroll down to the bottom and create the method I explained.
    Hi AlexGraal,

    I have modified the .xml file as you suggested and followed your idea for the MainActivity file. Now I have

    package com.testapp;

    import java.util.ArrayList;
    import java.util.List;
    import android.app.ListActivity;
    import android.content.ComponentName;
    import android.content.DialogInterface;
    import android.view.View.OnClickListener;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;

    public class MainActivity extends ListActivity implements OnClickListener {

    private testOperations testDBoperation;
    public static ArrayList<String> infoList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testDBoperation = new testOperations(this);
    testDBoperation.open();
    testOperations database = new testOperations(this);

    List values = testDBoperation.getAlltests();

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

    View insClick = findViewById(R.id.imageButton1);
    @Override
    public void insButtonPress(View view) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address",
    "com.package.address.MainActivity"));
    startActivity(intent);
    }

    }
    }

    sorry if I am not being very clever but the setOnClickListener has disappeared and I have the following errors.

    Where blue is shown The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)

    and where green is

    void is an invalid type for the variable insButtonPress

    Thanks for your patience,

    Terry

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Touchable listview and buttons

    Well first off, you need to take a look at where you put the insButtonPress method. It isn't something that should be inside of onCreate. It is its own separate method. So put it outside of onCreate. Second of all, you have, "public class MainActivity extends ListActivity implements OnClickListener", so of course it is looking for a OnClickListener method. Take that out, and move insButtonPress method out of the onCreate method, and it should work!

Similar Threads

  1. Custom cell in ListView.
    By gyijhbk in forum Android
    Replies: 0
    Last Post: 04-29-2013, 10:27 PM
  2. multiline listview need help figuring this out
    By Darkyere in forum Android
    Replies: 0
    Last Post: 10-22-2012, 10:00 PM
  3. Activity's ListView items do not show
    By KernelPanic in forum Android
    Replies: 0
    Last Post: 05-17-2012, 08:21 AM
  4. ListView Sub Items
    By rolledback in forum Android
    Replies: 2
    Last Post: 09-21-2011, 10:21 PM
  5. listView and listArray methods
    By viviosoft in forum Android
    Replies: 0
    Last Post: 09-09-2011, 11:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •