Results 1 to 7 of 7
Thread: Touchable listview and buttons
- 07-25-2014, 11:13 PM #1
Member
- Join Date
- Jul 2014
- Posts
- 3
- Rep Power
- 0
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
- 07-25-2014, 11:38 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 11
Re: Touchable listview and buttons
you should put this question in Android sub-forum..
- 07-26-2014, 12:29 AM #3
Señor Member
- Join Date
- Jan 2014
- Posts
- 184
- Rep Power
- 0
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"
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 }
Last edited by AlexGraal; 07-26-2014 at 12:31 AM.
- 07-27-2014, 02:01 PM #4
Member
- Join Date
- Jul 2014
- Posts
- 3
- Rep Power
- 0
Re: Touchable listview and buttons
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
- 07-27-2014, 04:56 PM #5
Señor Member
- Join Date
- Jan 2014
- Posts
- 184
- Rep Power
- 0
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;
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 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"
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); } });
Last edited by AlexGraal; 07-27-2014 at 05:03 PM.
- 07-27-2014, 05:59 PM #6
Member
- Join Date
- Jul 2014
- Posts
- 3
- Rep Power
- 0
Re: Touchable listview and buttons
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
- 07-27-2014, 10:30 PM #7
Señor Member
- Join Date
- Jan 2014
- Posts
- 184
- Rep Power
- 0
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
-
Custom cell in ListView.
By gyijhbk in forum AndroidReplies: 0Last Post: 04-29-2013, 10:27 PM -
multiline listview need help figuring this out
By Darkyere in forum AndroidReplies: 0Last Post: 10-22-2012, 10:00 PM -
Activity's ListView items do not show
By KernelPanic in forum AndroidReplies: 0Last Post: 05-17-2012, 08:21 AM -
ListView Sub Items
By rolledback in forum AndroidReplies: 2Last Post: 09-21-2011, 10:21 PM -
listView and listArray methods
By viviosoft in forum AndroidReplies: 0Last Post: 09-09-2011, 11:08 PM
Bookmarks