I am trying to create a ListView
with customized layout. each item in the listView
should look like as shown in the item.xml
posted below.
in the code, i used
adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);
but it is not working because the constructor of the ArrayAdapter<T>
accepts the second parameter as int
something like
android.R.layout.simple_list_item_1
, and in my case it is customized layout which is
R.layout.listi_tems_layout
which adapter should i use or how to solve this. thanks
Item:
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/tvlist_topic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:id="@+id/ivList_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/delete_icon"
android:contentDescription="icon to delete item from the Listview"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/cbList_hook"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:checked="false"
android:layout_weight="1"/>
mainlayout:
<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"
tools:context="${relativePackage}.${activityClass}" >
....
....
....
<ListView
android:id="@+id/lvEco_topics"
android:layout_width="match_parent"
android:layout_height="470dp"
android:layout_below="@id/tvEco_topic"
android:layout_marginTop="30dp"
android:scrollbars="vertical"
android:divider="@android:drawable/alert_light_frame"></ListView>
<Button
android:id="@+id/btEco_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lvEco_topics"
android:gravity="center"
android:text="Save"/>
code:
public class MainActivity extends Activity {
private ArrayList<String> topicsList;
private ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
topicsList = new ArrayList<String>();
topicsList.add("topic1");
topicsList.add("topic2");
topicsList.add("topic3");
topicsList.add("topic4");
topicsList.add("topic5");
topicsList.add("topic6");
adapter = new ArrayAdapter<T>(getApplicationContext(), R.layout.listi_tems_layout, topicsList);
ArrayAdapter accepts the second parameter as int something like android.R.layout.simple_list_item_1
When not customizing getView
method of ArrayAdapter
then custom layout require one TextView with android:id="@android:id/text1"
id and show value in one TextView.
To run application with current code add android:id="@android:id/text1"
for TextView
in R.layout.listi_tems_layout
layout.
Because R.layout.listi_tems_layout
layout contains other views also with TextView so create custom Adapter by extending ArrayAdapter class to access other views also.
See following example:Custom ArrayAdapter for a ListView (Android)
I'm not sure if it's still relevant, from the source ArrayAdapter seem to have constructor that accept textview resource in 3rd parameter, not sure if it's juts newly added in 2017 :D,
ArrayAdapter adapter = new ArrayAdapter<>(context,YourCustomLayoutID,TextViewIDinYourLayout,ListData);
and from the source, if you didn't supply textview layout id, it will assume that the whole view is a textview, so I think it's possible to use custom view without extending/creating new adapter (I know, I hate to create one myself), but you need to provide textview used for the text
create a class and extends with base adapter like this and then set this adapter
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.test.R;
import com.itoks.model.ClientDetails;
public class ClientListAdapter extends BaseAdapter {
// ArrayList<String> name, company, email, id, status;
ArrayList<ClientDetails> clientArrayList;
Context c;
public ClientListAdapter(Context c, ArrayList<ClientDetails> list) {
clientArrayList = list;
this.c = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return clientArrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return clientArrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
row = inflater.inflate(R.layout.listview_item_clients, parent,
false);
} else {
row = convertView;
}
ClientDetails detail = clientArrayList.get(position);
TextView name = (TextView) row.findViewById(R.id.tvClientFullName);
name.setText(detail.name);
TextView email = (TextView) row.findViewById(R.id.tvClientEmail);
email.setText(detail.email);
TextView id = (TextView) row.findViewById(R.id.tvClientID);
id.setText("ID : " + detail.id);
TextView company = (TextView) row
.findViewById(R.id.tvClientCompanyName);
company.setText(detail.company);
TextView status = (TextView) row.findViewById(R.id.tvClientStatus);
status.setText("Status:" + detail.status);
return row;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With