Listview Android onClick defaul event - android

How could I use onClick event from the base ListView in android without using the Adapter. I what to now witch item is selected and in base of that i want to open another layout.
listview.item="Something"
{
setContentView(R.id.layout.Something);
}
Please help me. I tried with the simple onClick but it doesn't work.

You Need To create layout XML file
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/view1" >
</ListView>
</RelativeLayout>
In Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mListView = (ListView)findViewById(R.id.listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}

Related

setOnItemClickListener for activity extending ListActivity without adapter

I want to implement a setOnItemClickListener for the below mentioned listview.
The code works fine for me in displaying items of string-array to listview but I want to perform some action when the user clicks and on longclick.
public class MainActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] plainStrings = getResources().getStringArray(R.array.myarray);
Spanned[] htmlStrings = new Spanned[plainStrings.length];
for(int i = 0 ; i < plainStrings.length; i++) {
htmlStrings[i] = Html.fromHtml(plainStrings[i]);
}
setListAdapter(new ArrayAdapter<CharSequence>(this,R.layout.items, htmlStrings));
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:isScrollContainer="true"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:scrollbarStyle="insideOverlay">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:dividerHeight="3dp"/>
</LinearLayout>
items.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
ListActivity has its own protected method onListItemClick, you can override it to get the row click's event
Normal click:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//Do whatever you want
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
Long click:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
//Do whatever you want
Toast.makeText(getApplicationContext(),"Long Clicked",Toast.LENGTH_SHORT).show();
});
}

Adding search feature to listview

I have created a listview of the users installed applications in a sliding drawer and I want to add a search function to it. I am following the tutorial here.
So far, this is my coding (that includes the so far implemented search function):
Drag_and_Drop_App.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SlidingDrawer
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:layout_marginTop="200dp"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
<!-- Editext for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search applications.."
android:inputType="textVisiblePassword"/>
<ListView
android:id="#+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Drag_and_Drop_App.java:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
}
}
I'm not sure where to put this coding:
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
If you need to see any other of my coding (such as any adapters for the app listview) let me know!

How do I repeat an existing xml spinner dynamically

I have an XML file: my_form.xml (abridged to just show relevant views). The spinner is populated by a string array from strings.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/task_array"
android:prompt="#string/task" />
<TextView android:id="#+id/addTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another task"
android:onClick="onClick"
android:clickable="true"/>
</LinearLayout>
</ScrollView>
So I have an activity that displays the xml spinner and text view.
When the text view is clicked I want to add another instance of the same xml spinner and text view, so that a person can add as many tasks as they like.
I dont want a multi-select listView as I am going to have to associate each chosen item/task with other individual methods
Can anyone help me please?
public class MyFormActivity extends FragmentActivity implements
OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_form);
final Spinner spinnerTask = (Spinner) findViewById(R.id.task);
ArrayAdapter<CharSequence> adapter_task = ArrayAdapter
.createFromResource(this, R.array.task_array,
android.R.layout.simple_spinner_item);
spinnerTask.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long row) {
int taskChoice = spinnerTask.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",
0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("taskChoiceSpinner", taskChoice);
prefEditor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
View tvAddTask = (TextView) findViewById(R.id.addTask);
tvAddTask.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Here I want add another instance of the spinner and text view above
}
}
Try this
1 - Edit my_form.xml to this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
2 - Create new layout xml file text_view.xml and put this in it
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another task"
android:clickable="true"/>
3 - Create new layout xml file spinner_view.xml and put this in it
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/task_array"
android:prompt="#string/task" />
4 - Edit your code like this
public class MyFormActivity extends FragmentActivity implements OnClickListener {
private LinearLayout linear;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_form);
linear = (LinearLayout) findViewById(R.id.LinearLayout01);
addNewView();
}
private void addNewView(){
Spinner spinnerTask = View.inflate(this, R.layout.spinner_view, null);
TextView tvAddTask = View.inflate(this, R.layout.text_view, null);
linear.addView(spinnerTask);
linear.addView(tvAddTask);
spinnerTask.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long row) {
int taskChoice = pos;
SharedPreferences sharedPref = getSharedPreferences("FileName",
0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("taskChoiceSpinner", taskChoice);
prefEditor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
tvAddTask.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Here I want add another instance of the spinner and text view above
addNewView();
}
});
}
}
Hope this helped you.
You could just write a method that would call the spinner and this method would get called in the spinner.setOnItemSelectedListener();

display image when clicked on the item in listview

i am using listview... i try open the image when itemclicked on the list and is diaply in another activity...but its not working... help me if freinds you know... thanks....
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> mainListView, View view, int pos,
long id) {
//Object obj = getFilesDir().getPath();
//String value= obj.toString();
Intent intent= new Intent(SimpleListViewActivity.this,Nextclass.class);
final String root = Environment.getExternalStorageDirectory().getAbsolutePath();
intent.setData(Uri.parse(root + "/mnt/sdcard/dcim/100andro/koala_copy.jpg"));
startActivity(intent);
}
private ArrayAdapter<String> getListAdapter() {
// TODO Auto-generated method stub
return null;
}
my main.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView>
</LinearLayout>
We'd need to see NextClass's code as well, since that's what is receiving that Intent you're launching. It should be a subclass of Activity. Then in its onCreate or onResume you call getIntent(), check if the intent has the extras/data you need, and load the image into an ImageView or something else.

OnClick ListView

I have followed this tutorial
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
and like to add an on click for the listview.
now here is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#drawable/list_divider"
android:dividerHeight="1px"
android:cacheColorHint="#00000000"/>
</LinearLayout>
and here is my code:
setContentView(R.layout.main);
steden = new ArrayList<voorDeLijst>();
this.m_adapter = new StedenAdapter(this, R.layout.list_item, steden);
setListAdapter(this.m_adapter);
ListView lv = (ListView)findViewById(R.id.List);
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder adb = new AlertDialog.Builder(HelloAndroid.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = ");
adb.setPositiveButton("Ok", null);
adb.show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The thing is that I am a beginner and with the code above I get an error because It cannot locate the listview. So I can't attach a OnItemClick Listener to it.
but when I change <ListView android:id="#+id/android:list" to <ListView android:id="#+id/List"
Then I can find the listview. but it gives an exception at the line: setContentView(R.layout.main);
So how do I attach an onClick/onItemClick to a Listview which has a custom adapter to bind objects to the listitems?
Found it, because my class extended the ListActivity I could do this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
...
}
I found it at http://www.anddev.org/viewtopic.php?t=22
Have you tried findViewById(android.R.id.list)? Really though, if your activity is basically one big list view, then you should be using a ListActivity for your activity, and that has an accessor that gives you direct access to the ListView.

Categories

Resources