Custom List in Fragment wont work - android

So im new in Android programming I'm trying to make a custom ListView. I follow a tutorial on YouTube (https://www.youtube.com/watch?v=gAIB4fTm2BA) but i cant get it work on a fragment.
public class DriverFragment extends Fragment implements GeneralFragment {
ListView listView;
public DriverFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.fragment_driver, container, false);
listView = (ListView) view.findViewById(R.id.driverList);
DriverAdapter driverAdapter = new DriverAdapter(getActivity().getApplicationContext(),R.layout.driver_listview);
listView.setAdapter(driverAdapter);
Driver a = new Driver("John Smith","Johnsmith#example.com","123");
driverAdapter.add(a);
return view;
}
Driver Adapter :
public class DriverAdapter extends ArrayAdapter {
ArrayList list = new ArrayList();
public DriverAdapter(Context context, int resource) {
super(context, resource);
}
static class Holder{
TextView NAME;
TextView EMAIL;
TextView PHONE;
}
public void add(Driver driver) {
list.add(driver);
super.add(driver);
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
public int getCount(){
return this.list.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
Holder holder;
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.driver_listview, parent, false);
holder = new Holder();
holder.NAME = (TextView) row.findViewById(R.id.driverName);
holder.EMAIL = (TextView) row.findViewById(R.id.driverMail);
holder.PHONE = (TextView) row.findViewById(R.id.driverPhone);
row.setTag(holder);
}else {
holder = (Holder) row.getTag();
}
Driver driver = (Driver)getItem(position);
holder.NAME.setText(driver.getName());
holder.EMAIL.setText(driver.getMail());
holder.PHONE.setText(driver.getPhone());
return row;
}
The 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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DriverName"
android:id="#+id/driverName"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DriverMail"
android:id="#+id/driverMail"
android:layout_gravity="center_horizontal"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DriverPhone"
android:id="#+id/driverPhone"
android:padding="10dp" />

first of all if you should really check out some good tutorial like
Using lists in Android (ListView) - Tutorial
http://www.vogella.com/tutorials/AndroidListView/article.html
Assuming everything is okay with your Fragment and it´s visible, lets focus on your Adapter. Since your DriverAdapter has it´s own ArrayList of data there is no point in calling super.add() in it´s add() method. You just call notifyDataSetChanged() to let the Adapter know that it should refresh the content on the UI. Try something like this..
public class DriverAdapter extends BaseAdapter {
ArrayList<Driver> data = new ArrayList();
public void add(Driver driver) {
data.add(driver);
notifyDataSetChanged();
}
public void addAll(List<Driver> drivers) {
data.addAll(drivers);
notifyDataSetChanged();
}
#Override
public Driver getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public int getCount() {
return data.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_driver_row, parent, false);
holder = new Holder();
holder.name = (TextView) convertView.findViewById(R.id.driverName);
holder.email = (TextView) convertView.findViewById(R.id.driverMail);
holder.phone = (TextView) convertView.findViewById(R.id.driverPhone);
} else {
holder = (Holder) convertView.getTag();
}
Driver driver = getItem(position);
holder.name.setText(driver.getName());
holder.email.setText(driver.getEmail());
holder.phone.setText(driver.getPhone());
convertView.setTag(holder);
return convertView;
}
static class Holder {
TextView name;
TextView email;
TextView phone;
}
}

My last answer didn't seem to help, I quickly put something together.
I would strongly advise reading up more on this topics since Lists and Adapters are an integral part of Android development and having an understanding of how it works is really helpful.
public class Driver {
private String name;
private String email;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
Driver a = new Driver();
a.setName("Name");
a.setEmail("Email");
a.setPhone("Phone");
ArrayList<Driver> drivers = new ArrayList<>();
drivers.add(a);
DriverAdapter driverAdapter = new DriverAdapter(getActivity().getApplicationContext(),R.layout.driver_listview, drivers);
listView.setAdapter(driverAdapter);
return view;
}
Adapter
public class DriverAdapter extends ArrayAdapter<Driver> {
public DriverAdapter(Context context, int resource, List<Driver> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
Holder holder;
if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.driver_listview, parent, false);
holder = new Holder();
holder.name = (TextView) row.findViewById(R.id.driverName);
holder.email = (TextView) row.findViewById(R.id.driverMail);
holder.phone = (TextView) row.findViewById(R.id.driverPhone);
row.setTag(holder);
}else {
holder = (Holder) row.getTag();
}
Driver driver = getItem(position);
holder.name.setText(driver.getName());
holder.email.setText(driver.getEmail());
holder.phone.setText(driver.getPhone());
return row;
}
public class Holder {
protected TextView name;
protected TextView email;
protected TextView phone;
}
}
Adapter 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">
<TextView
android:id="#+id/driverName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="DriverName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/driverMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="DriverMail"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/driverPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="DriverPhone"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
</LinearLayout>
If this doesn't help you, there is a problem with your fragment showing on the device. As I have tested this myself and it displays correctly on the screen.

Looks like you are setting your adapter to the ListView before you add anything to it, which is fine but you must call adapter.notifyDataSetChanged() in order to update the list with the new data.
Although, I would suggest changing how you create your adapter. Everytime I have used any sort of adapter, I create my list of data before and pass it in through the constructor. Then set it to my listview. So like:
View view;
view = inflater.inflate(R.layout.fragment_driver, container, false);
listView = (ListView) view.findViewById(R.id.driverList);
Driver a = new Driver("John Smith","Johnsmith#example.com","123");
ArrayList<Driver> drivers = new ArrayList();
drivers.add(a);
DriverAdapter driverAdapter = new DriverAdapter(getActivity().getApplicationContext(),R.layout.driver_listview, drivers);
listView.setAdapter(driverAdapter);
This is a only rough example of what I would do, I would advise you to read up on some examples from Google around this. Also read up on RecyclerViews instead of ListViews if possible.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-for-listview-experts/

Related

Creating list inside the Activity after clicking RecyclerView

As the title says, I want to create a new list inside the Activity after clicking the RecyclerView. I am using JSON to get the data and the data inside apparently has a list for name, comments, and date time. I can set them into a TextView but that's not what I wanted. I need to show them as a list. The list should have name, comments, and date time. Thanks!
You need to use Custom Adapter instead of the ArrayAdapter. So that you can customize the view how ever you want.
For example in your case
row_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<!-- Name -->
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Marshmallow"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black" />
<!-- Comments-->
<TextView
android:id="#+id/comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginTop="5dp"
android:text="Android 6.0"
android:textColor="#android:color/black" />
<!-- Date and Time-->
<TextView
android:id="#+id/dateandtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/comments"
android:layout_marginTop="5dp"
android:text="11th August"
android:textColor="#android:color/black" />
</RelativeLayout>
DataModel.java
public class DataModel {
String name;
String comment,
String date;
public DataModel(String name, String type, String comment, String date) {
this.name=name;
this.comment=comment;
this.date=date;
}
public String getName() {
return name;
}
public String getComment() {
return comment;
}
public String getDate() {
return date;
}
}
And finally your Custom Adapter should be like below
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
TextView txtComment;
TextView txtDate;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View v) {
// your on click code
}
private int lastPosition = -1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
viewHolder.txtComment = (TextView) convertView.findViewById(R.id.comments);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateandtime);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(dataModel.getName());
viewHolder.txtComment.setText(dataModel.getComment());
viewHolder.txtDate.setText(dataModel.getDate());
viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
You have to set the adapter like this.
ArrayList<DataModel> dataModels; // then add your data into it
CustomAdapter adapter= new CustomAdapter(dataModels,getApplicationContext());
listView.setAdapter(adapter);
Check this example for better idea.

how to hide/show Imageview on Button Click for Custom Adapter Layout

I have custom adapter for ListView. The ListView is in activity_main.xml and I have a ImageView in CustomAdapter layout which is populated with ListView using CustomAdapter.
My Question is I have to hide/show that image which is in custom layout with a button present in main layout. Like if there is 10 row in my ListView and 10 row have that image. I want to hide only for the particular row.
I hope you understand my question...Thanks in Advance
Here's my ListViewAdapter
public class ListViewAdapter extends ArrayAdapter<FileName> {
Context context;
LayoutInflater inflater;
List<FileName> filenames;
private SparseBooleanArray mSelectedItemsIds;
public ListViewAdapter(Context context,int resource,List<FileName> filenames) {
super(context, resource, filenames);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.filenames = filenames;
inflater = LayoutInflater.from(context);
}
private class ViewHolder {
TextView filename;
TextView shorttext;
ImageView imageView;
}
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.filename = (TextView) view.findViewById(R.id.item_title);
holder.shorttext = (TextView) view.findViewById(R.id.item_desc);
holder.imageView = (ImageView) view.findViewById(R.id.lock);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews<br />
holder.filename.setText(filenames.get(position).getName());
holder.shorttext.setText(filenames.get(position).getShorttext());
return view;
}
}
name = filenames.get(position).getName(); //getting the positin from listview
filenames.set(name,FileName.isVisible = true); //getting error on isVisible
I have writen a Sample code and tested
MainActivity
public class MainActivity extends AppCompatActivity {
List<FileName> filenames;
DBhelper dBhelper;dBhelper
SQLiteDatabase sqLiteDatabase;
ListViewAdapter listViewAdapter;
ListView listView;
ImageView lock;
String name;
//temporary button i have added
Button lockBTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.lv_filename);
lockBTN = (Button) findViewById(R.id.lock);
filenames = getResult();
listViewAdapter = new ListViewAdapter(this, filenames);
listView.setAdapter(listViewAdapter);
lockBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//giving statically position is 2
int position = 2;
// when you press menu button or any other button
FileName fileName = filenames.get(position);
//here you can set true-Show and false-hide imageview
fileName.setVisible(true);
//and update the list
filenames.set(position, fileName);
listViewAdapter.notifyDataSetChanged();
}
});
}
//temporary data to show on listview
private List<FileName> getResult() {
List<FileName> fileNames = new ArrayList<>();
String[] nameArr = {"Name1", "Name2", "Name3"};
String[] shortTextArr = {"ShortText1", "ShortText2", "ShortText3"};
for(int i=0; i<nameArr.length;i ++){
FileName fileName = new FileName();
fileName.setName(nameArr[i]);
fileName.setShorttext(shortTextArr[i]);
//setting default is hide to imageview
fileName.setVisible(false);
fileNames.add(fileName);
}
return fileNames;
}
}
FileName
public class FileName {
private String name;
private String shorttext;
private boolean visible = true;
public String getName() {
return name;
}
public void setName(String _name) {
this.name = _name;
}
public String getShorttext() {
return shorttext;
}
public void setShorttext(String _shorttext) {
this.shorttext = _shorttext;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
ListViewAdapter
public class ListViewAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
List<FileName> filenames;
private SparseBooleanArray mSelectedItemsIds;
public ListViewAdapter(Context context, List<FileName> filenames) {
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.filenames = filenames;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return filenames.size();
}
#Override
public Object getItem(int position) {
return filenames.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.filename = (TextView) view.findViewById(R.id.item_title);
holder.shorttext = (TextView) view.findViewById(R.id.item_desc);
holder.imageView = (ImageView) view.findViewById(R.id.lock);
//holder.filename.setPaintFlags(holder.filename.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews<br />
holder.filename.setText(filenames.get(position).getName());
holder.shorttext.setText(filenames.get(position).getShorttext());
// check weather image is to show/hide
if (filenames.get(position).isVisible()) {
holder.imageView.setVisibility(View.VISIBLE);
} else {
holder.imageView.setVisibility(View.GONE);
}
return view;
}
private class ViewHolder {
TextView filename;
TextView shorttext;
ImageView imageView;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<ListView
android:id="#+id/lv_filename"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/lock"/>
<Button
android:id="#+id/lock"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lock"/>
</RelativeLayout>
custom_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/utilities_notepad_icon" />
<LinearLayout
android:layout_width="232dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.77">
<TextView
android:id="#+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="1dp"
android:paddingTop="5dp"
android:text="Item Title"
android:textSize="16dp" />
<TextView
android:id="#+id/item_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="35"
android:paddingBottom="5dp"
android:paddingTop="1dp"
android:text="Item Description"
android:textColor="#999"
android:textSize="10dp" />
</LinearLayout>
<ImageView
android:id="#+id/lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lo"
android:layout_marginTop="13sp" />
</LinearLayout>
Take a extra variable in your FileName class.
public class FileName {
public String name;
public String shortText;
public boolean visible = true; // Set the default value to true.
}
Now on external button click in your main layout, you might consider updating the list item accordingly. Just set the visible variable to false of that item you want to hide. Then call notifyDataSetChanged() on your adapter to reload the data.
And yes, in your adapter, just check the value of visible attribute and then decide to hide/show the ImageView. So the getView function of your adapter should look like this.
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.filename = (TextView) view.findViewById(R.id.item_title);
holder.shorttext = (TextView) view.findViewById(R.id.item_desc);
holder.imageView = (ImageView) view.findViewById(R.id.lock);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews<br />
holder.filename.setText(filenames.get(position).getName());
holder.shorttext.setText(filenames.get(position).getShorttext());
// Hide/Show the ImageView
if (filenames.get(position).visible) holder.imageView.setVisibility(View.VISIBLE);
else holder.imageView.setVisibility(View.GONE);
return view;
}
I would suggest you to use RecyclerView instead of ListView. RecyclerView got more performance than the ListView. Now a days all are using RecyclerView instead of old ListView, it is quite easy to use as well.
And for handling your case create a function to remove the file name you wanted to remove in the RecyclerViewAdapter class as below
removeImageView(position){
fileNames.remove(position);
this.notifyItemRemoved(position);
}
notifyItemRemoved call will refresh the position in the recycler view and you will also get a nice view removing animation out of the box.

How can I make an android xml layout for Category and CategoryItem

I'am new in using java and android studio and i was wondering how can i make a layout like this in 1 activity. myImage
What you are looking for is a Listview adapter
public class ExampleAdapter extends BaseAdapter {
List<Product> products;
private Activity context;
#Override
public int getCount() {
return products.size();
}
public ExampleAdapter(List<Product> products, Activity context) {
this.products = products;
this.context = context;
}
#Override
public Object getItem(int position) {
return products.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Product product = products.get(position);
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.row_shopping_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.getProductTextView().setText(product.getName());
holder.getProductImageView(). setImageResource(product.getPicture());
return convertView;
}
private class ViewHolder {
private final TextView productTextView;
private final ImageView productImageView;
private ViewHolder(View wrapperView) {
productTextView = (TextView) wrapperView.findViewById(R.id.tvName);
productImageView = (TextView) wrapperView.findViewById(R.id.ivPicture);
}
public TextView getProductTextView() {
return productTextView;
}
public ImageView getProductImageView() {
return productImageView;
}
}
}
This adapter needs a layout to define what the rows will look like. This will be something like this
<?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">
<ImageView
android:layout_gravity="center_vertical"
android:layout_width="50dp"
android:id="#+id/ivPicture"
android:layout_height="50dp" />
<TextView
android:id="#+id/tvName"
android:text="Product 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Lastly in your activity where you want to display the overview you will need to use a listview and set the adapter
final ExampleAdapter adapter = new ExampleAdapter(list, this);
listview.setAdapter(adapter);

Inflated Views inside a ListView are not visible

I'm trying to create a inflated dialog which contains dynamic amount of items.
These Items I inflate aswell.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int anzJobViews = getArguments().getInt("param");
View rootView = inflater.inflate(R.layout.dialog_job_chooser, container, false);
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
ListView listview = (ListView) rootView.findViewById(R.id.dialog_chooser_listview);
for (int i = 0; i < anzJobViews; i++) {
listview.addFooterView( new JobItemView(context));
Log.i("JobChooser","Item added");
}
return rootView;
}
The View (Contains some simple TextViews)
private void init() {
inflate(getContext(), R.layout.job_item_view, this);
}
When showing the dialog I see Nothing.
The xml layout for the dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:background="#drawable/round_corners_selected"
android:backgroundTint="#color/washedOutGreen">
<ListView
android:id="#+id/dialog_chooser_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
The Items a want to add to the List.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue_focused">
<TextView
android:id="#+id/tv_jobname_area"
android:text="defaultName"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_jobvalue_area"
android:text="0 /h"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/tv_jobname_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Why don't use an adapter to populate your listview? An example from here: http://www.pcsalt.com/android/listview-using-baseadapter-android/
public MyBaseAdapter(Context context, ArrayList myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
ListData currentListData = getItem(position);
mViewHolder.tvTitle.setText(currentListData.getTitle());
mViewHolder.tvDesc.setText(currentListData.getDescription());
mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());
return convertView;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
public MyViewHolder(View item) {
tvTitle = (TextView) item.findViewById(R.id.tvTitle);
tvDesc = (TextView) item.findViewById(R.id.tvDesc);
ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
}
}
}
Also, if you are in a pre kitkat enviroment, you can only use addFooterView before setting the adapter.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Reference: http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
use getView function to setup your listview
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null) {
element = View.inflate(context, R.layout.items, null);
}
else {
element = convertView;
}
//setup here something
//e.g.
TextView tv = (TextView) v.findViewById(R.id.tv);
tv.setText("myText");
return element;
}

Custom List View with OnClickListener

I have custom ListView layout with a TextView and CheckBox. Everything works fine.
What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.
I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.
MAIN ACTIVITY
package com.example.smsplanner;
public class SMSPlanner extends ListActivity {
ListView contactsListView;
private String TAG = "SMSPlanner"; CheckBox check;
int count;
List<ContactInfo> list = new ArrayList<ContactInfo>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ph = new String[3];
phType = new String[3];
LoadContactListFromPhone();
ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(contactadAdapter);
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
TextView tx =(TextView)v.findViewById(R.id.firstname);
TextView ph =(TextView)v.findViewById(R.id.phone);
Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
void LoadContactListFromPhone()
{
loadlistandreturns();
}
void call()
{
Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
toast.show();
}
}
CUSTOM ADAPTER
public class ContactsAdapter extends ArrayAdapter<ContactInfo>
{
private final Activity context;
int resourceid;
List<ContactInfo> list = null;
public ContactsAdapter(Activity context, List<ContactInfo> list) {
super(context, R.layout.contactrow, list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
}
LAYOUT
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:orientation="vertical" >
<TextView
android:id="#+id/firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgStyle2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<CheckBox
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RadioGroup>
</LinearLayout>
1. Bydefault all the Rows of the ListView are enabled to listen to click....
You must implement onItemClickListener() for the ListView....
See this example:
http://www.mkyong.com/android/android-listview-example/
you can use a CheckedTextView, or you can create a Checkable Layout, like this one :
http://tokudu.com/2010/android-checkable-linear-layout/
i think you should make adapter as
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}

Categories

Resources