Android listview - Get text of custom listview - android

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.....

Related

listview item cannot be click in android

i have created Custom listview and i add a onclick listener to it but it was not working.the below is my code
This is my MainActivity Class:
public class MainActivity extends Activity {
public boolean[] status;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
// Attach the adapter to a ListView
listView = (ListView) findViewById(R.id.lvUsers);
populateUsersList();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
Toast.makeText(getBaseContext(),"Hello", Toast.LENGTH_SHORT).show();
}
});
}
private void populateUsersList() {
// Construct the data source
ArrayList<User> arrayOfUsers = User.getUsers();
// Create the adapter to convert the array to views
CustomUsersAdapter adapter = new CustomUsersAdapter(this, arrayOfUsers);
listView.setAdapter(adapter);
}
}
this is my adapter class:
public class CustomUsersAdapter extends ArrayAdapter<User> {
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ToggleButton tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
}
}
i want to get the value from listview when the appropriate toggle button is checked .and want to save the value of toggle button and the appropriate text in arraylist.
this my xml :
<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/lvUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
This is my second 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="match_parent"
android:padding="10dp"
>
<TextView
android:id="#+id/tvName"
android:layout_alignBaseline="#+id/ivUserIcon"
android:layout_toRightOf="#+id/ivUserIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="6dp"
android:text="Sarah"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvHometown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvName"
android:layout_below="#+id/tvName"
android:text="San Marco"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/tvName"
android:layout_marginRight="15dp"
android:text="ToggleButton" />
</RelativeLayout>
Change your RelativeLayout to
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:descendantFocusability="blocksDescendants"
>
This view will block any of its descendants from getting focus, even if they are focusable.
You must set the button on clicklistener.
tgbtn.setOnClickListener(new OnClickListener(position));
Dont know it it works but try anything like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ToggleButton tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
tgbtn.setOnClickListener(new OnButtonClickListener(position));
// Return the completed view to render on screen
return convertView;
}
then a class for the ButtonClick:
private class OnButtonClickListener implements OnClickListener {
int _postion;
// constructor
public OnButtonClickListener(int position) {
this._postion = position;
}
#Override
public void onClick(View v) {
}
}
below of your code that there is a ListView and you wanna able clicked function to the list item, write this code.
public void onListItemClick(ListView parent, View v, int position, long id){
// do something here
// Example below make a toast message
Toast.makeText(getApplicationContext(), "I clicked item of list number " + position,Toast.LENGTH_SHORT).show();
}

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 layout listview advice

I currently have a program that displays a listview when the user clicks a button. This view takes up the whole screen. How can I (instead of having to press a button to get to the listview) divide the main screen into 2 parts, one being a large listview and two being a few buttons?
This is how I was using my listView adapter:
public void showPopUp(){
ListArrayAdapter adapter = new ListArrayAdapter(this, R.layout.list_view_row_item, foodList);
ListView listViewItems = new ListView(this);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new OnListItemClick());
alertDialogStores = new AlertDialog.Builder(MainScreen.this)
.setView(listViewItems)
.setTitle("Food List")
.show();
}
and this is the adapter:
public class ListArrayAdapter extends ArrayAdapter<ListItem> {
Context mContext;
int layoutResourceId;
ListItem data[] = null;
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
ArrayList<ListItem> foodList = new ArrayList<>();
//public ArrayAdapterItem(Context mContext, int layoutResourceId, ListItem[] data) {
public ListArrayAdapter(Context mContext, int layoutResourceId, ArrayList<ListItem> foodList) {
super(mContext, layoutResourceId, foodList);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.foodList = foodList;
//this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* The convertView argument is essentially a "ScrapView" as described is Lucas post
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
// object item based on the position
//ListItem ListItem = data[position];
// get the TextView and then set the text (item name) and tag (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(R.id.itemName);
textViewItem.setText(foodList.get(position).getItemName());
textViewItem.setTag(foodList.get(position).getItemIdInt());
progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar1);
return convertView;
}
}
I also would like to know if it's possible to have a message appear when the user presses and holds an item (and have it linger a little after they let go).
You can use android:layout_weight attribute.
Accept the answer if it helped you. :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button" />
</LinearLayout>
</LinearLayout>
** To get the message on click of item in ListView **
listViewItems.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String str = adapterView.getItemAtPosition(arg2).toString();
Toast.makeText(getApplicationContext(), "You clicked "+ str, 100).show();
}
}
Instead of adapterview, you can use
String str = foodList.get(arg2).getItemName().toString() + " " +foodList.get(arg2).getItemIdInt();

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

How can i add CheckBox in the list view items?

In My application i am using two list view's in one activity, one for only items and one list view i am have add check box in every row.
My requirement is in below list view if item is checked i have to display on text view in the main activity if not have to display another activity .
i was not used ListActivity for this i just created from Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
above list view for only items for below list view i have to add check box. how can i achieve this.
my activity is..
public class Settings extends Activity {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ListView list1 = (ListView) findViewById(R.id.list1);
ListView list2 = (ListView) findViewById(R.id.list2);
list2.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, Display));
list2.setTextFilterEnabled(true);
list2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, Categories));
list1.setTextFilterEnabled(true);
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
}
static final String[] Categories = new String[] {
"Alarm1","Alarm2","Alarm3","Alarm4","Alarm5"
};
static final String[] Display = new String[] {
"24 Hours","Display Seconds"
};
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.provider, null);
TextView location = (TextView) convertView
.findViewById(R.id.description);
checkBoxImage = (ImageView) convertView.findViewById(R.id.check_id);
Provider p = (Provider) getItem(position);
location.setText(p.Description + " - " + p.Location);
return convertView;
}
Well here is the Complete Tutorial how you can include CheckBox in a List. Its the Best Tutorial Regarding the ListView & ListActivity.

Categories

Resources