How to make custom ListView - android

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.

Related

how to respond to adapter.notifyDataSetChanged()

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

Android - ListActivity not triggering onListItem clicks

A lot of people had this same issue before, but I haven't been able to solve it using their solutions..
I've got a ListActivity with a custom ArrayAdapter, but I can't seem to trigger the onItemClick.
I've tried it both with the default #Override protected void onListItemClick(...) which didn't trigger, and also with a myListView.setOnItemClickListener(new OnItemClickListener(){ ... });, which also didn't work.
From previous stackoverflow questions regarding this matter I've used the following pieces of code in the xml:
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"`
In the list_item.xml views
Or android:descendantFocusability="afterDescendants" in the ListView itself.
Here below is the main part of code regarding this problem:
ChecklistActivity.java:
public class ChecklistActivity extends ListActivity
{
private List<Product> products;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
// test products:
products = new ArrayList<Product>();
for(int i = 0; i < 5; i++){
Product p = new Product();
p.setName("Product " + i);
products.add(p);
}
MyAdapter adapt = new MyAdapter(this, R.layout.list_inner_view, products);
setListAdapter(adapt);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapt, View v, int position, long id){
onListItemClick(v);
}
});
...
}
private void onListItemClick(View v){
Log.i("CHECKLIST ACTIVITY", "onListItemClick triggered");
...
}
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:contentDescription="#string/checkbox_content_description"
android:src="#drawable/checkbox_unchecked"
android:background="#drawable/transparent_background"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="#+id/tv_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false" />
</LinearLayout>
activity_list.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">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="afterDescendants" />
</RelativeLayout>
Ok, very stupid of me.. I removed
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
from the LinearLayout of the list_item.xml and now it works..

How to display multiple number of TextViews inside each row in ListView?

I am creating a Help page in which I have a set of questions and answers. These questions and answers have different styles. Here is the xml file, which describes the layout of a question & answer set:
<?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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:text="#string/Help_first_question"
android:id="#+id/text1"
android:padding="5dip"
android:background="#e0f3ff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="#+id/panel1"
android:visibility="gone"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_margin="2dip"
android:text="#string/Help_first_answer"
android:padding="5dip"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
I want to display multiple questions and answers within a listView, whereby each row contains a set of the question and the answer. My listview looks like :
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
so it will look like :
first row : Q
A
second row : Q
A
third row : Q
A
What is the best approach for achieving this?
create custom adapter and use the below layout to achieve your goal
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
You need to implement a custom ListAdapter implementing all of the abstract methods.
Let's create a QuestionAndAnswerListAdapter, which you can make your ListView by setting it up in onCreate:
public void onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
setContentView(R.layout.listview);
QuestionsAndAnswersListAdapter adapter = new QuestionsAndAnswersListAdapter(data);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
The adapter itself would look something like this:
public QuestionsAndAnswersListAdapter implements ListAdapter {
private QuestionAndAnswer[] data;
public QuestionsAndAnswersListAdapter(QuestionAndAnswer[] data) {
this.data = data;
}
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
//Only creates new view when recycling isn't possible
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_and_answer_list_item, null);
}
QuestionAndAnswer thisQA = data[position];
TextView questionView = (TextView) view.findViewById(R.id.text1);
questionView.setText(thisQA.question);
TextView answerView = (TextView) view.findViewById(R.id.answer);
answerView.setText(thisQA.answer);
return view;
}
// ...
}
getView is really the central method to get right. The rest of the methods you need to implement to live up to the ListAdapter interface are pretty straight-forward. Check the reference to see exactly what they are.

Android list view setOnItemClickListener not working

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 :)

Make a TextView inside a ListView use the default ListView row style

My app generates 2 different lists, one uses the default ListView style as the ListView rows contain no TextView inside of them. The other list uses a custom CursorAdapter and has a TextView inside each row. All I want to do is make it so that the margins and text size are exactly the same for both lists.
My first list is using a standard ListView with no TextView inside each row, here is what it looks like:
Here is the code to generate it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.records);
DatabaseHandler db = new DatabaseHandler(this);
ArrayList<String> records = db.getRecords(this);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, records));
}
Here is the xml file for it:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
My second list is using a ListView with a TextView inside each row that is generated via a custom `CursorAdapter, here is what it looks like:
Here is the code to generate it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.achievements);
DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.getAchievements(this);
AchievementAdapter cursorAdapter = new AchievementAdapter(this, cursor);
this.setListAdapter(cursorAdapter);
}
private class AchievementAdapter extends CursorAdapter {
public AchievementAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public void bindView(View v, Context context, Cursor cursor) {
TextView tv = (TextView) v.findViewById(R.id.achView1);
if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes")) {
tv.setText(cursor.getString(cursor.getColumnIndex("name"))+" (completed)");
tv.setTextColor(Color.GREEN);
}
else {
tv.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.achievements_item, parent, false);
return v;
}
}
Here is the achievements xml file for it:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""/>
</LinearLayout>
Here is the achievements_item xml for it:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/achView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="" />
</LinearLayout>
Basically I just want these 2 lists to look exactly the same. Is there a way to do it so that the TextView inherits the default ListView row style? Or am I going to have to play with the margins and everything myself?
Just copy the attributes of the TextView from the default ListView layout android.R.layout.simple_list_item_1 and use them on your own row layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/achView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
Also, if all you want for the customized ListView row layout is a TextView, you could remove the parent LinearLayout so you don't have an extra View in the layout
In achievements_item.xml add some padding to the top and bottom of the TextView
<TextView
android:id="#+id/achView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="" />

Categories

Resources