ListView with multiple lines and hyperlink - java

I have a listView with multiple lines and I want to add one more line with a link, but i dont know how to do it.
example
this is my code
activity_main.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="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Custom ListView Example" />
<ListView
android:id="#+id/srListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
custom_row_view
<?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"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF00A7FF"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
SearchResults.java
public class SearchResults {
private String name = "";
private String cityState = "";
private String phone = "";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCityState(String cityState) {
this.cityState = cityState;
}
public String getCityState() {
return cityState;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
}
MyCustomBaseAdapter.java
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCityState = (TextView) convertView.findViewById(R.id.cityState);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
holder.txtPhone.setText(searchArrayList.get(position).getPhone());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtPhone;
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv.getItemAtPosition(position);
SearchResults fullObject = (SearchResults)o;
Toast.makeText(MainActivity.this, "You have chosen: " + " " + fullObject.getName(), Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr = new SearchResults();
sr.setName("Hospital de Lisboa");
sr.setCityState("Avenida 123 nº15");
sr.setPhone("212321234");
results.add(sr);
sr = new SearchResults();
sr.setName("Hospital de Santarém");
sr.setCityState("Rua de Cima nº20");
sr.setPhone("234234234");
results.add(sr);
return results;
}
}

You can't just create a new textview and create new methods to your SearchResults then just sr.setNewLine()?

Related

I'd like to make an internal grid view added by clicking the button on the external recyclerview

I want to make an internal grid view(room) added by clicking the button on the external recycler view(floor). but Null pointer extension occurs in onBindViewHolder. please help me
public class FloorAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnFloorItemClickListener {
OnFloorItemClickListener listener;
static public ArrayList<FloorData> floors;
private Context context;
private LayoutInflater layoutInflater;
private OnItemClickListener mListener = null;
public FloorAdapter(ArrayList<FloorData> floors, Context context) {
this.floors = floors;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
public interface OnItemClickListener{
void onItemClick(View v, int pos);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener ;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.signle_floor, parent, false);
return new GridViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((GridViewHolder)holder).recyclerView.setAdapter(new RoomAdapter(context, floors.get(position).rooms));
((GridViewHolder)holder).recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
((GridViewHolder)holder).recyclerView.setHasFixedSize(true);
((GridViewHolder)holder).tvFloorNum.setText(String.valueOf(floors.get(position).floorNum));
}
#Override
public int getItemCount() {
return floors.size();
}
#Override public void onItemClick(RecyclerView.ViewHolder holder, View view, int position) {
if(listener != null){
listener.onItemClick(holder,view,position);
}
}
#Override
public int getItemViewType(int position) {
return floors.get(position).id;
}
public class GridViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerView;
TextView tvFloorNum;
Button btnPlusRoom;
public GridViewHolder(View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.rvRooms);
tvFloorNum = itemView.findViewById(R.id.tvRoomNumber);
btnPlusRoom = (Button)itemView.findViewById(R.id.btnPlusRoom);
btnPlusRoom.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION)
{
if(mListener != null){
mListener.onItemClick(v, pos);
}
}
}
});
}
}
}
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.CustomViewHolder> {
private Context context;
private ArrayList<RoomData> rooms;
private LayoutInflater inflater;
public RoomAdapter(Context context, ArrayList<RoomData> rooms) {
this.context = context;
this.rooms = rooms;
this.inflater = LayoutInflater.from(context);
}
#Override
public CustomViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
view = inflater.inflate(R.layout.single_room, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CustomViewHolder holder, int position) {
RoomData room = rooms.get(position);
holder.tvRoomNum.setText(String.valueOf(room.roomNum));
}
#Override
public int getItemCount() {
return rooms.size();
}
public static class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView tvRoomNum;
public CustomViewHolder(View itemView) {
super(itemView);
tvRoomNum = (TextView) itemView.findViewById(R.id.tvRoomNumber);
}
}
}
public class RoomActivity extends AppCompatActivity {
private RecyclerView rvFloor;
private FloorAdapter floorAdapter;
public ArrayList<FloorData> globalfloors;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
globalfloors = prepareData();
rvFloor = findViewById(R.id.rvFloors);
floorAdapter = new FloorAdapter(globalfloors, RoomActivity.this);
LinearLayoutManager manager = new LinearLayoutManager(RoomActivity.this);
rvFloor.setLayoutManager(manager);
rvFloor.setAdapter(floorAdapter);
floorAdapter.setOnItemClickListener(new FloorAdapter.OnItemClickListener() {
#Override
public void onItemClick(View v, int position) {
FloorData floor = globalfloors.get(position);
RoomData newRoom = new RoomData();
floor.finalRoomNum++;
newRoom.roomNum = floor.finalRoomNum;
floor.rooms.add(newRoom);
rvFloor.setAdapter(floorAdapter);
}
});
}
private ArrayList<FloorData> prepareData() {
ArrayList<FloorData> floors = new ArrayList<FloorData>();
//첫번째 subject 추가
FloorData floor1 = new FloorData();
floor1.floorNum = 1;
RoomData room101 = new RoomData();
room101.roomNum = 101;
RoomData room102 = new RoomData();
room102.roomNum = 102;
RoomData room103 = new RoomData();
room103.roomNum = 103;
floor1.finalRoomNum = 103;
floor1.rooms.add(room101);
floor1.rooms.add(room102);
floor1.rooms.add(room103);
floors.add(floor1);
FloorData floor2 = new FloorData();
floor2.floorNum = 2;
floor2.finalRoomNum = 200;
floors.add(floor2);
return floors;
}
}
activity_room
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".tools.RewardActivity"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar_room"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="60dp"
android:background="#color/colorPrimaryDark"
app:title="호실 등록"
android:theme="#style/ToolbarTheme" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvFloors"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
single_floor
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tvFloorNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="30dp"
android:textColor="#000000" />
<Button
android:id="#+id/btnPlusRoom"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="+"
android:textSize="30dp"
android:layout_marginTop="10dp"
android:background="#color/colorPrimaryDark"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:columnCount="5"
android:id="#+id/rvRooms"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
single_room
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp">
<TextView
android:id="#+id/tvRoomNumber"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:singleLine="true"
android:background="#color/colorAccent"
android:gravity="center"
android:textSize="30dp"
/>
</LinearLayout>
</LinearLayout>
error message describes "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.eos.parcelnoticemanager.tools.FloorAdapter.onBindViewHolder"
The GridViewHolder inside your FloorAdapter is pointing to the wrong XML Id tag for the TextView that's why it's throwing NullPointerException
Change this in your GridViewHolder
tvFloorNum = itemView.findViewById(R.id.tvRoomNumber);
to this
tvFloorNum = itemView.findViewById(R.id.tvFloorNumber);
Then it should work

Use SearchView with a ListView that have an image and text

I have a problem, I learnt how to use a SearchView with text, but I have an Activity where the listView have an image and text, but I couldnt find the way to display the filter.
So, I debug the code in order to understand why this happens and I get this in the LogCat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{mundo.hola.app.frank.com.universidad2/mundo.hola.app.frank.com.universidad2.RankingActividad}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
the code compiles great, but when I try to have access to this activity trough an Intent the App crashes.
If someone could help me i would be greatful.
activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarLayout_po"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/appbar_rank"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
>
<ImageView
android:id="#+id/home_rank"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:scaleType="center"
tools:ignore="ContentDescription"
android:src="#drawable/ic_first_page_24dp" />
<SearchView
android:id="#+id/search_tecnico"
android:layout_width="match_parent"
android:layout_height="match_parent">
<requestFocus />
</SearchView>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ListView
android:id="#+id/rankinglista"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
item_list
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlanco"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="8dp">
<ImageView
android:id="#+id/imagenR"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/tv_nombreUR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_marginLeft="56dp"
android:layout_marginStart="65dp"
android:textColor="#android:color/black"
tools:text="NombreU" />
<TextView
android:id="#+id/tv_PuntajeR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_nombreUR"
android:layout_alignStart="#+id/tv_nombreUR"
android:layout_below="#+id/tv_nombreUR"
tools:text="Puntaje" />
<TextView
android:id="#+id/tv_NpuntajeR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_PuntajeR"
android:layout_alignParentBottom="false"
android:layout_alignStart="#+id/tv_PuntajeR"
android:layout_alignWithParentIfMissing="false"
android:layout_below="#+id/tv_PuntajeR"
android:textColor="#android:color/holo_red_dark"
tools:text="11223344" />
</RelativeLayout>
Class Ranking
public class Ranking {
private int Id;
private String Titulo;
private String Puntaje;
private String NPuntaje;
private int Imagen;
public Ranking(int id, String titulo, String puntaje, String NPuntaje, int imagen) {
Id = id;
Titulo = titulo;
Puntaje = puntaje;
this.NPuntaje = NPuntaje;
Imagen = imagen;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getTitulo() {
return Titulo;
}
public void setTitulo(String titulo) {
Titulo = titulo;
}
public String getPuntaje() {
return Puntaje;
}
public void setPuntaje(String puntaje) {
Puntaje = puntaje;
}
public String getNPuntaje() {
return NPuntaje;
}
public void setNPuntaje(String NPuntaje) {
this.NPuntaje = NPuntaje;
}
public int getImagen() {
return Imagen;
}
public void setImagen(int imagen) {
Imagen = imagen;
}
}
Activity Class
public class RankingActividad extends AppCompatActivity {
ImageView inicio1;
SearchView sv;
Toolbar toolbar1;
ListView listadatos;
ArrayList<Ranking> Lista;
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ranking_actividad);
inicio1 = (ImageView) findViewById(R.id.home_rank);
sv = (SearchView) findViewById(R.id.search);
toolbar1 = (Toolbar) findViewById(R.id.appbar_rank);
listadatos = (ListView) findViewById(R.id.rankinglista);
Lista = new ArrayList<Ranking>();
Lista.add(new Ranking(1,"Universidad 1","Puntaje","4512",R.drawable.ufps));
Lista.add(new Ranking(2,"universidad 2","Puntaje","4512",R.drawable.unip));
Lista.add(new Ranking(3,"universidad 3","Puntaje","4512",R.drawable.ufps));
Lista.add(new Ranking(4,"universidad 4","Puntaje","4512",R.drawable.ufps));
final RankingAdaptador rankingAdaptador = new RankingAdaptador(getApplicationContext(),Lista);
listadatos.setAdapter(rankingAdaptador);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
if(TextUtils.isEmpty(s)){
rankingAdaptador.filter("");
listadatos.clearTextFilter();
}else{
String texto = s;
rankingAdaptador.filter(s);
}
return true;
}
});
Adapter Class
public class RankingAdaptador extends BaseAdapter {
Context context;
LayoutInflater inflater;
List<Ranking> ListaObjetos;
private ArrayList<Ranking> arraylist;
public RankingAdaptador(Context context, List<Ranking> listaObjetos) {
this.context = context;
ListaObjetos = listaObjetos;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<Ranking>();
this.arraylist.addAll(ListaObjetos);
}
public class ViewHolder{
TextView mtitulo,mpuntaje,npuntaje;
ImageView mimagen;
}
#Override
public int getCount() {
return ListaObjetos.size(); //retorna la cantidad de elementos de la lista
}
#Override
public Object getItem(int i) {
return ListaObjetos.get(i);
}
#Override
public long getItemId(int i) {
return ListaObjetos.get(i).getId();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view==null){
holder = new ViewHolder();
view = inflater.inflate(R.layout.ranking_item_lista,null);
// localizar los items de la lista
holder.mimagen = view.findViewById(R.id.imagenR);
holder.mtitulo = view.findViewById(R.id.tv_nombreUR);
holder.mpuntaje = view.findViewById(R.id.tv_PuntajeR);
holder.npuntaje = view.findViewById(R.id.tv_NpuntajeR);
view.setTag(holder);
}else {
holder = (ViewHolder)view.getTag();
}
// setear los textos de la lista
holder.mtitulo.setText(ListaObjetos.get(position).getTitulo());
holder.mpuntaje.setText(ListaObjetos.get(position).getPuntaje());
holder.npuntaje.setText(ListaObjetos.get(position).getNPuntaje());
// setear las imagenes de la lista
holder.mimagen.setImageResource(ListaObjetos.get(position).getImagen());
return null;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
ListaObjetos.clear();
if (charText.length() == 0) {
ListaObjetos.addAll(arraylist);
} else {
for (Ranking wp : arraylist) {
if (wp.getTitulo().toLowerCase(Locale.getDefault()).contains(charText)) {
ListaObjetos.add(wp);
}
}
}
notifyDataSetChanged();
}}
sv.setOnQueryTextListener(new Sear...
You never initialized sv, so it's null. Add this:
sv = (SearchView) findViewById(R.id.search_tecnico);

Custom List view View Holder pattern with RadioGroup state not maintained

I have implemented CustomListView with RadioGroup. When I selected the radio button of first/beginning rows and scrolling to the end of list, last row elements are automatically selected. And when I'm scrolling back to beginning of List, Radiogroup state is not maintained. I have spent lot of time with this issue.
Kindly help me out where I'm doing a mistake.
MyAdapter.java
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected RadioGroup radioGroup;
protected RadioButton firstRadio;
protected RadioButton secondRadio;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group);
viewHolder.firstRadio = (RadioButton) convertView.findViewById(R.id.first_rad);
viewHolder.secondRadio = (RadioButton) convertView.findViewById(R.id.second_rad);
viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int selectedID) {
int getPosition = (Integer) radioGroup.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(selectedID); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.radio_group, viewHolder.radioGroup);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.radioGroup.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
if(list.get(position).getSelected() == viewHolder.firstRadio.getId()){
viewHolder.firstRadio.setChecked(true);
}else if(list.get(position).getSelected() == viewHolder.secondRadio.getId()){
viewHolder.secondRadio.setChecked(true);
}
return convertView;
}
}
row.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="wrap_content" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30sp" >
</TextView>
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label"
android:orientation="horizontal">
<RadioButton
android:id="#+id/first_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</RadioButton>
<RadioButton
android:id="#+id/second_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</RadioButton>
</RadioGroup>
Model.java
public class Model {
private String name;
private int selected;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
Change the conditions in if statement as below and it will work.
if(list.get(position).getSelected() == viewHolder.firstRadio.getId())
{
viewHolder.firstRadio.setChecked(true);
viewHolder.secondRadio.setChecked(false);
}
else if(list.get(position).getSelected() == viewHolder.secondRadio.getId())
{
viewHolder.secondRadio.setChecked(true);
viewHolder.firstRadio.setChecked(false);
}
else
{
viewHolder.secondRadio.setChecked(false);
viewHolder.firstRadio.setChecked(false);
}
I'm not sure how it is working now. I've spent lot of days in this issue. Overriding getViewTypeCount and getItemViewType resolved my issue.
#Override
public int getViewTypeCount() {
if (objects.size() > 0) {
return objects.size();
}
return 1;
}
#Override
public int getItemViewType(int position) {
return position;
}

Gridview adapter images with title name for images group

I have a gridView witch contains a group of images with two columns, what I want to do is to put a title for a group of images.
Now I can do that but the problem that title is that the title is only in one column and the second column is always empty, how can I put the title in the two columns?
Here is my adapter:
public class MultimediaPhotosAdapter extends BaseAdapter {
private Context mContext;
private List<ImagesDto> imagesDto = new ArrayList<ImagesDto>();
ImagesFlickrDto imagesFlickrDto;
final String formatImage = ImagesNameFormat.FORMAT_IMAGE_DEFAULT.getImagesNameFormat();
ImagesFormatsDto currentImageFormatToDisplay;
GridView gridViewImages;
public MultimediaPhotosAdapter(Context context, GridView gridViewImages) {
this.gridViewImages = gridViewImages;
this.mContext = context;
}
#Override
public int getCount() {
return imagesDto.size();
}
#Override
public Object getItem(int position) {
return imagesDto.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = null;
HolderElementGridView holderElementGridView;
ImagesDto currentImage = imagesDto.get(position);
if(convertView == null) {
holderElementGridView = new HolderElementGridView();
itemView = View.inflate(mContext,R.layout.item_multimedia_photo,null);
holderElementGridView.image = (NetworkImageView) itemView.findViewById(R.id.imageView);
holderElementGridView.title = (TextView) itemView.findViewById(R.id.title);
itemView.setTag(holderElementGridView);
}
else {
itemView = convertView;
holderElementGridView = (HolderElementGridView)itemView.getTag();
}
if(imagesDto.get(position).isFirstElementOfCategorie()){
holderElementGridView.image.setVisibility(View.GONE);
holderElementGridView.title.setVisibility(View.VISIBLE);
holderElementGridView.title.setText(currentImage.getTitle());
}
else if(imagesDto.get(position).getUrl()!=null){
holderElementGridView.image.setVisibility(View.VISIBLE);
holderElementGridView.title.setVisibility(View.GONE);
holderElementGridView.image.setImageUrl(StringFormat.getUrlImage(mContext, currentImage.getUrl(), currentImageFormatToDisplay.getSuffix(), currentImageFormatToDisplay.getExtension()),VolleySingleton.getInstance(mContext).getImageLoader());
holderElementGridView.image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayoutInterface screenInterface=(DrawerLayoutInterface)mContext;
if(screenInterface!=null)
screenInterface.showDiaporama(imagesFlickrDto,position);
}
});
}
// column near title is empty
else{
holderElementGridView.image.setVisibility(View.VISIBLE);
holderElementGridView.title.setVisibility(View.GONE);
}
return itemView;
}
public void update(ImagesFlickrDto imagesFlickrDto){
this.imagesFlickrDto = imagesFlickrDto;
this.imagesDto = imagesFlickrDto.getListImages();
AdministrerImagesSA administrerImages = new AdministrerImagesSAImpl();
currentImageFormatToDisplay = administrerImages.getFormatByProriete(imagesFlickrDto.getImagesFormatsDto(), formatImage);
}
class HolderElementGridView{
NetworkImageView image;
TextView title;
}
}
item_multimedia_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:geekui="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.netcosports.anderlecht.activity.utils.TypefaceTextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/text_size_item_menu"
android:visibility="gone"
android:layout_marginLeft="5dp"
geekui:customTypeface="fonts/DIN-Regular.otf" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/ic_launcher" >
</com.android.volley.toolbox.NetworkImageView>
</LinearLayout>
</LinearLayout>

Cant display listview in AlertDialog android

I want to display ArrayList of object in AlertDialog but my list view dont show. Here is my code.
public class PrzystanekDialog extends DialogFragment {
private ListView trasaPrzejazdu;
private ArrayList<Linia> trasaPrzejazduList;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
LayoutInflater inflater = getActivity().getLayoutInflater();
final View rootView = inflater.inflate(R.layout.dialog_layout, null);
//ustawienie lisview z wygenerowaną linia
trasaPrzejazduList = getArguments().getParcelableArrayList("linia");
trasaPrzejazdu = (ListView) rootView.findViewById(R.id.trasaListView);
final ArrayAdapter<Linia> adapter = new ArrayAdapter<Linia>(getActivity(),R.layout.listbox_dialog_element, trasaPrzejazduList);
trasaPrzejazdu.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final ViewPager mViewPager;
mViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
builder.setView(inflater.inflate(R.layout.dialog_layout, null));
//getActivity().setContentView(inflater.inflate(R.layout.dialog_layout, null));
builder.setMessage(R.string.choose)
.setPositiveButton(R.string.start_trip, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mViewPager.setCurrentItem(1);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getActivity().getApplicationContext(), "Anulowano podróż.", Toast.LENGTH_LONG).show();
}
});
return builder.create();
}
}
Here is my ArrayAdapter
public class DialogListAdapter extends BaseAdapter {
private List<Linia> listData;
private LayoutInflater layoutInflater;
public DialogListAdapter(Context context, List<Linia> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Linia getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listbox_dialog_element, null);
holder = new ViewHolder();
holder.start = (TextView) convertView.findViewById(R.id.przystanekStartowy);
holder.end = (TextView) convertView.findViewById(R.id.przystanekDocelowy);
holder.vehicleImage = (ImageView) convertView.findViewById(R.id.line_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//listData.get(position).getHeadline()
if(listData.get(position).getTypLini().toString() == "bus")
holder.vehicleImage.setImageResource(R.drawable.autobus_ico);
else
holder.vehicleImage.setImageResource(R.drawable.tram_ico);
holder.start.setText(listData.get(position).getStopPrzystanki().get(0).getNazwa_przystanku());
holder.end.setText(listData.get(position).getStopPrzystanki().get(listData.get(position).getStopPrzystanki().size()-1).getNazwa_przystanku());
return convertView;
}
static class ViewHolder {
ImageView vehicleImage;
TextView start;
TextView end;
}
}
And here is my xml file
<?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="vertical" >
<ListView
android:id="#+id/trasaListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I have no compile errors. Thank you very much for help.
EDIT:
<?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="vertical" >
<ImageView
android:id="#+id/line_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/autobus_ico"
android:layout_marginLeft="5dip"/>
<TextView
android:id="#+id/przystanekStartowy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:typeface="sans"
android:layout_marginLeft="60dip"/>
<TextView
android:id="#+id/przystanekDocelowy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text=""
android:textColor="#343434"
android:textSize="12sp"
android:layout_marginLeft="60dip"/>
</LinearLayout>

Categories