i have a listview that i want to use with a custom list item .
the list item (list_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="start time"
android:id="#+id/startTime"
android:layout_margin="10dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="finish time"
android:id="#+id/finishTime"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/payment"
android:layout_margin="10dp"/>
</LinearLayout>
and the java class:
private ListView listView;
private ArrayAdapter adapter;
private ArrayList shifts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = (ListView)findViewById(R.id.listview);
listView.setOnItemClickListener(this);
adapter = new ArrayAdapter(this, R.layout.list_item, shifts);
My question is, when the app start running, how can i add a new list item into shifts (ArrayList), override the method notifyDataSetChanged() on the adapter so the listview would "refresh", and then set the edit texts in the list item with the parameters i wish to?
I hope im clear enough..
Thanks a lot!!
void onItemClick (AdapterView<?> parent,
View view,
int position,
long id)
Parameters
View: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
So You can find ExidText(findviewbyid) from View
Related
I want to hide the edit text and button field initially in list view and show that edit text and button for a particular raw in list view when that raw is clicked.So I tried to set the height to 0 in layout xml and then set it to some other value when user clicks on a raw, but it is not working I think my list view click event is not working.
In the Android layout that I have list view there are Image view as a button, edit text field and list view also. Like follows
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainPortal" >
<ListView
android:id="#+id/employeeListView"
android:layout_width="650dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:clickable="true">
</ListView>
<EditText
android:id="#+id/empPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/iv_start_journey_btn"
android:layout_marginBottom="56dp"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/employeeListView"
android:ems="10"
android:inputType="number"
android:hint="#string/add_emp_pin_hint" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/iv_start_journey_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/employeeListView"
android:layout_alignRight="#+id/empPin"
android:layout_marginBottom="84dp"
android:onClick="startJourney"
android:src="#drawable/start" />
</RelativeLayout>
I have used following custom lay out for 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="0dp"
android:layout_alignBottom="#+id/emp_avatar"
android:layout_alignRight="#+id/emp_number"
android:layout_toRightOf="#+id/emp_avatar"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/empAdd"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBaseline="#+id/empPin"
android:layout_alignBottom="#+id/empPin"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/empPin"
android:text="#string/emp_add_btn" />
Here I post 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) {
//I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();
}
});
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;
}
}
}
Above I have posted some codes that I think important.
Can any one please help me to do this task.
Thanks in advance.
Add the following attributes to your button
android:focusable="false"
android:focusableInTouchMode="false"
ListView setOnItemClickListener not working by adding button
Is your edittext taking focus on click of list item? If so remove the focus on edittext also.
Here your edittext inside the listview is having all the focus, so this listview onClick is not working. Remove the focus from the editext box and it will start working
// try this
public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();
list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();
}
});
}
}
In my Case setOnItemClickListener works fine in Android Versions Upto 5.1.1
But Having On Android 6.0.1. At last I found the solution
Try this
Remove these Lines from Your XML Layout
android:orientation="vertical"
tools:context=".ManageRequest"
android:contextClickable="true"
Works for Me in Android Version 6.0.1
Hope Your Problems Solved ..!!! Happy Coding :)
On an Android app targeting APIs 11 to 18, I have dynamically populated a ListView using an array of strings and added a listener to each row. The text usually covers approximately 50% of a row only. The problem is that if I click on the actual text, the onItemClick action is fired, but if I click on the blank portion of the row it is not.
My question is: how can I add the listener to the entire area/view of each row?
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_item, my_array);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
// add listener
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG, "position=" + position);
}
});
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".80"
>
</ListView>
activity_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_textview"
android:textSize="35sp"
/>
Set the layout_width of the TextView in activity_list_item to match_parent.
I have created listview using ListActivity
I want to add another view below that list.So how can I do that?
here are 2 files spacelist.xml and homescreen.java using which I have created list.
spacelist.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="70dp"
android:background="#drawable/background"
android:layout_marginBottom="10dp">
<ImageView
android:id="#+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#+id/label"
android:textSize="20dp" >
</TextView>
</LinearLayout>
homeScreen.java-
public class homeScreen extends ListActivity{
String list[]={"My Ideas","Space1","Space2","All Notes","Create New Space"};
ListView spaces;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//To display as a list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spacelist, R.id.label, list);
setListAdapter(adapter);
}
}
Now suppose i want to add another view below list.How should I do?
It really depends on what you want to do with that view: always visible at the bottom of the view over the list? or is it fine with you to show that view at the bottom of the list?
First option, you could use an Activity (not ListActivity) and do whatever you want in your layout.
Second option, you can set a footer for your listview or you can have a +1 item in your listview and that item would be your view. Of course you have to manage different type of view in your adapter so it's better to set a footer.
I've got a TextView and it's getting a list in it.
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
//Own row layout
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));
//listView.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
Now I want a small grey description of the list item, like here:
I read these tutorials: 1 and 2 but I have to admit, I don't understand what they say.
Can someone explain how to use them? Do I have to remove my TextView/List I have now? Is there another way to make a little description, without using a custom ListView?
Here's my xml:
<?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="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="2dp"/>
I don't really use the listview "list", just for testing purposes.
You need to understand that the following line
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));
means that you want to use layout.activity_home.xml for each item's view of your list.
So I don't see why there is a ListView inside it...
By default the ArrayAdapter uses one single text field per item. If you want a more complex item layout, you need to override the getView() method of your adapter.
Here is the simplest solution I can think of:
In Home.java:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
And layout/list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
First use the setContentView and call the layout.. From the layout call the LISTVIEW. then using the adapter put the LISTE inside the list view use your data.
I'm using a custom ListView which has several TextView objects in each row. in the onListItemClick, I'd like to get the actual TextView that was clicked, but I am simply getting the LinearLayout View that contains the two TextViews.
class CustomList extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.custom_list_item, R.id.label, data);
setListAdapter(aa);
}
public void onListItemClick(ListView lv, View v, int index, long id){
// I would like to access the TextView that was clicked here
// v = LinearLayout, not the TextView that was clicked
}
}
Below is res/custom_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/label"
android:textSize="30sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/delete_text"
android:textSize="30sp"
android:text="X">
</TextView>
</LinearLayout>
Any help would be much appreciated!
You have to define an onClickListener for your TextView items. Right now, your click listener only applies to your CustomList and not your TextView items.