mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
lvUsers.setClickable(true);
lvUsers.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
// TODO Auto-generated method stub
Object o = lvUsers.getItemAtPosition(position);
UserBO obj = (UserBO) o;
Toast.makeText(Select.this, "Record Selected= "+obj.getId()+" "+obj.getName()+" "+obj.getEmail()+" "+obj.getContact(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(Select.this,Update.class);
startActivity(intent);
}
});
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="6px"
android:orientation="vertical"
android:layout_height="67px" android:id="#+id/rlt_main"
android:background="#E0FFFF" android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="16px" android:id="#+id/rlt_main"
android:background="#E0FFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_pid"
android:text="12"
android:layout_centerVertical="true"
android:textColor="#E0FFFF" >
</TextView>
</LinearLayout>
select.xml
<ListView
android:layout_height="wrap_content"
android:id="#+id/lv_user"
android:layout_width="fill_parent"
android:clickable="true"
>
Hi,above code is of onclick listview items selection.but when i click on item from listview nnothing is happening instead i want to call update activity.what is the mistake in my code??if anyone want to see code of XML layout then let me know..
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
Toast.makeText(Context,"Selected item is "+ position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(ListActivity.this, SecondActivity.class);
ListActivity.this.startActivity(i);
}
}
Hi I think you should try like this,
Intent intent = new Intent();
intent.setClassName(Select.this, <packagename>.Update.class.getName());
startActivityForResult(intent,0);
Related
I m raeding csv file and showin it using the listview and textview so i want set setOnItemClickListener on list view of text...and when i click on item then other activity will get open..so please tell how do i code for this..
Here is my code...
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String listItem = (String) listView.getItemAtPosition(position);
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
}
Here is catlog report..
12-08 11:17:28.630 20936-20936/com.technostark.com.myloginactivity W/ResourceType: Skipping entry 0x108035a in package table 0 because it is not complex!
Update
<RelativeLayout
xmlns:android= "schemas.android.com/apk/res/android";
xmlns:tools= "schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape">
<ListView android:id="#+id/editlistview"
android:layout_width="match_parent"
android:layout_height= "wrap_content" />
</RelativeLayout>
Here is textview xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/et_Studname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Student Name"
android:textColorHint="#660000"
android:textSize="20dp"
android:textColor="#660000"
android:clickable="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:ems="20" />
</LinearLayout>
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView c = (TextView) view.findViewById(R.id.rating);
TextView d = (TextView) view.findViewById(R.id.title);
TextView e = (TextView) view.findViewById(R.id.releaseYear);
//Toast.makeText(Video_Main_List.this,Config.YOUTUBE_VIDEO_CODE, Toast.LENGTH_SHORT).show();
Intent launch = new Intent(Video_Main_List.this,Youtube_Player_Activity.class);
launch.putExtra("c", c.getText().toString());
launch.putExtra("d", d.getText().toString());
launch.putExtra("e", e.getText().toString());
startActivity(launch);
}});
// you can also take value from listview items and send into second activity that i mansion above.
ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
Can anyone please tell me how to fix the below code? I am trying to setonclick listner and its causing null exception
ArrayList<Contact> imageArry = new ArrayList<Contact>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
Edit
--
When I click on the image its not sending me to another activity
this my activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Latest" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_vertical"
android:text="Recomanded" />
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#4A9C67" >
</ListView>
</LinearLayout>
</ScrollView>
Your variable listView where you want to add a listener seems not initialized
Your listView is dataList not listView
You should write :
dataList.setOnItemClickListener(...)
Set onClickListener() to your ListView variable datalist. You're currently setting it to listView which exists no where else in your code.
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList.setOnItemClickListener(new OnItemClickListener() {...}
Change your code to this :
ArrayList<Contact> imageArry = new ArrayList<Contact>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList .setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
Your code were fine, the problem what you have faced is this listview is NULL because listview it's not declared, I guess you misstyped the ListView, you only have to change listview to dataList.
I have created list view, what I want to do is that when user clicks on first list view the selected record should show in second list, in my code it show on text view, but I want to show that record in second list view so please give me the code/idea how to do this as I am new in android..
public class MainActivity extends Activity {
String item[]=new String[]{"rk","kk","kk","ll","mm","uu"};
TextView tv,tv2,tv3,tv4,tv5;
List<TextView> arrayTV = new ArrayList<TextView>();
ListView li,li2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=(ListView)findViewById(R.id.listView1);
li2=(ListView)findViewById(R.id.listView2);
tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.textView5);
arrayTV.add(tv);
arrayTV.add(tv2);
arrayTV.add(tv3);
arrayTV.add(tv4);
arrayTV.add(tv5);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, item);
li.setAdapter(adapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
arrayTV.get(position).setText(""+li.getItemAtPosition(position));
}
});
}
Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight=".5" >
</ListView>
set the adapter for second listview in onitemclick of first listview
List<String> a = new ArrayList<String>();
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
String clickedItem =item[postion];
if(!a.contains(clickedItem))
a.add(clickedItem);
String[] newitem = new String[a.size()];
a.toArray(newitem);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1, newitem);
li2.setAdapter(adapter);
}
});
I have a listview which should contain some data (text) and an ImageView in each item.
So this is what I do, but it looks that the ImageView is not clickable :
here's a part the layout code :
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingBottom="5dp" >
<ImageView
android:id="#+id/searchIcone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scaleType="center"
android:background="#c7c7c7"
android:layout_marginTop="60dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="false"
android:src="#drawable/search" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="25dp"
android:onClick="searchVisiter"
android:textColor="#000" />
<TextView
android:id="#+id/CityCountry"
android:layout_below="#id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:textColor="#000" />
and my Java code :
final ListView lv1 = (ListView) findViewById(R.id.ListViewEvents);
lv1.setAdapter(new EventListViewAdapter(EventListActivity.this, records));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
JsonObject response = (JsonObject)o;
ID = response.getString("ID");
// If the image is clicked (doesn't work)
final ImageView img1 = (ImageView) findViewById(R.id.searchIcone);
img1.setClickable(true);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
}
});
// If the list item is clicked (works)
Intent intent = new Intent(EventListActivity.this, SearchActivity.class);
startActivity(intent);
}
});
Please, do you have any idea about this ?
Thank you.
What you do is setting listeners after clicking item on list - it doesn't make sense.
In method onItemClick you actually have View v, which was clicked, so you can handle that.
I think you tried to write something like:
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
JsonObject response = (JsonObject) lv1.getItemAtPosition(position);
ID = response.getString("ID");
if (v.equals(findViewById(R.id.searchIcone)) {
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
} else {
Intent intent = new Intent(EventListActivity.this, SearchActivity.class);
startActivity(intent);
}
}
});
Add onClickListner to getView method of EventListViewAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
....
ImageView img1 = (ImageView) convertView.findViewById(R.id.searchIcone);
img1 .setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
}
});
....
}
This should work
I have experienced the same problem.It is Because when you click on image on listItem ..the onclick() Method of ListView is triggered by Default.
The Mistake is you are registering setOnItemClickListener() for your listView lv1.
Instead just disable setOnItemClickListener() on listView and In your Adapter class EventListViewAdapter.java getView() Method just insert following code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//your layout inflater code here
ImageView iv = (ImageView) convertView.findViewById(R.id.searchIcone);
iv.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent = new Intent(EventListActivity.this, ZBarScannerActivity.class);
startActivity(intent);
}
});
....
}
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.