Android listview items show? - android

I'm a beginner in android app development. I write simple app to show listview and items in the list. I want when users click on the item show me row text.
adapter
ArrayAdapter<String> myAdaptor = new ArrayAdapter<String>(MainActivity.this, R.layout.rowlayout,R.id.label, itemsList)
rowlayout.xml
<ImageView
android:id="#+id/icon"
android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="#drawable/maleki" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="50px"
android:layout_gravity="right"
android:paddingLeft="100px"
>
</TextView>
I write this code into the listview click:
ListView myLST=(ListView)findViewById(R.id.listView1);
myLST.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemString=((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), tourdetail.class);
i.putExtra("countrydetail",itemString);
startActivity(i);
}
});
but in the this line I get error:
String itemString=((TextView) view).getText().toString();
How can I solve this problem? Thanks.

Try:
String itemString = ((TextView) view.findViewById(R.id.label)).getText().toString();
or
String itemString = myAdaptor.getItem(position)

Why would you cast the selected view item to TextView, which is a combination of ImageView and TextView. Check this for your solution:
myLST.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView label= (TextView) view.findViewById(R.id.label);
String text = label.getText().toString();
//remaining stuff
}
});

Related

how to setOnItemClickListener on listview and call the second activity

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

Android:How to extract siblings information onItemClick

I have implemented CustomAdapter using ViewHolder. I am listening to the onclick event on item of List View with the following method
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
How can I access the description test field ?
The following is the xml which is instantiated by the adapter
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="25dp"
android:textColor="#cccccc"
android:textSize="20sp" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="25dp"
android:textColor="#cccccc"
android:visibility="invisible"
android:textSize="20sp" />
</RelativeLayout>
One way
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.title);
String value = tv.getText().toString();
TextView desc = (TextView) view.findViewById(R.id.title);
String descp = desc.getText().toString();
}
});
In onItemClick you can use the view object to initialize views. Then get the value from textview.
The second way use
parent.getItemAtPosition(position); // this is better
Here you will use the adapterview (parent) and get the item at that postion
Example :
HashMap<String,String> map = (HashMap<String,String> parent.getItemAtPosition(position);
// assuming you are populating data using hashmap in your case might be different
Then
String title = map.get("key");

Android list view show hidden edit text when an item is clicked

I want to hide the edit text and button field initially in list view and show that edit text and button only for a particular row (clicked row) in list view when that row is clicked.
I used following custom layout to create the list view
<?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="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/emp_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="#drawable/person" />
<TextView
android:id="#+id/emp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/emp_avatar"
android:layout_toRightOf="#+id/emp_avatar"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/empPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/emp_avatar"
android:layout_alignRight="#+id/emp_number"
android:layout_toRightOf="#+id/emp_avatar"
android:ems="10"
android:inputType="number"
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="invisible">
<requestFocus />
</EditText>
In this lay out I have used android:visibility="invisible"
Here is the code of Activity.java
public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();
ListView list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list = (ListView) findViewById(R.id.employeeListView);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
et.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
});
private void populateEmployeeList() {
...
}
private class MylistAdapter extends ArrayAdapter<Employee>{
public MylistAdapter(){
super(MainPortal.this,R.layout.item_view,employees);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
Employee currentEmployee = employees.get(position);
//Avatar
ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
imageView.setImageResource(currentEmployee.getAvatarId());
//Employee number
TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
emp_id.setText(currentEmployee.getId());
et = (EditText) itemView.findViewById(R.id.empPin);
return itemView;
}
}
}
I used following codes withing the on click listener of list view to make edit text visible for item click.
editText.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
But after clicking a row edit text field does not appears. What I'm doing wrong here ? does any body have an idea how to do this ?
Initialization of EditText:
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
et = (EditText) itemView.findViewById(R.id.editText1);
...
Thanks in advance!
You have EdiText in item_view.xml not in activity_main_portal. You are inflating a custom layout in getView
You need to initialize edittext
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View v = (View)view.getParent();
EditText ed1 = (EditText) v.findViewById(R.id.empPin);
ed1.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
});

Clickable imageView on ListView

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

Android listview - Get text of custom listview

I am developing an application in which i created a custom list view as:
The list view xml code as :
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="15dip"
android:divider="#623b42"
android:dividerHeight="0.5dip"
android:cacheColorHint="#00000000"
android:overScrollMode="never" >
</ListView>
And the adapter xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textview_rupee_mlsaa"
android:gravity="center_vertical"
android:padding="15dip"
android:textColor="#color/color_black"
android:textSize="15sp"
tools:ignore="SelectableText" />
<TextView
android:id="#+id/textview2"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:padding="15dip"
android:textColor="#color/color_black"
android:textSize="15sp"
tools:ignore="SelectableText" />
</RelativeLayout>
The .java file of adapter is :
public class ListAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] dataset1;
private int[] dataset2;
/** Constructor Class */
public ListAdapter(Context context,String[] ListArray1, int[] ListArray2) {
super(context,R.layout.list_adapter_layout,ListArray);
this.context = context;
this.dataset1= ListArray1;
this.dataset2= ListArray2;
}
/** Implement getView method for customizing row of list view. */
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
// Creating a view of row.
View rowView = inflater.inflate(R.layout.list_adapter_layout, parent, false);
TextView tv1 = (TextView)rowView.findViewById(R.id.textview1);
TextView tv2 = (TextView)rowView.findViewById(R.id.textview2);
tv1.setText(data1[position]);
tv2.setText(""+data2[position]);
return rowView;
}
}
Then i set on item click listener on list view as :
listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);
ListAdapter adapter = new ListAdapter(this,list1,list2);
listView.setAdapter(adapter);
Then i used onItemClickListener on item click as :
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
name = (String) arg0.getItemAtPosition(position);
}
Here i am getting the value of first text view of custom listview. But i want also the value of second text view. and if i add more then how to get values of that.
Please suggest me the way to do that.
Try like this inside ur OnItemClick
TextView text = (TextView) arg1.findViewById(R.id.urtextID);
^^^^^^
String tEXT = text.getText().toString();
just a simplified version of above answer
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// code in the commented section works in case of normal single textView
// TextView temp=(TextView) view;
// Toast.makeText(this,
// temp.getText()+" is pressed "+position,Toast.LENGTH_SHORT
// ).show();
TextView temp = (TextView) view.findViewById(R.id.textView1);
String str = temp.getText().toString();
Toast.makeText(this,
str + " is pressed " + position,
Toast.LENGTH_SHORT).show();
}
Try this,
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
TextView textView = (TextView) arg1.findViewById(R.id.urtextID);
String text = textView.getText().toString();
Log.d("TEXT", "Text is" + text);
}
above answers are same like my answer.....

Categories

Resources