Trouble with fragments and adapter - android

I'm working on a little app which is supposed to display 3 screens : Talks, Papers and Choices. I've discovered fragments a few days ago and I would like to use this feature for my app, but unfortunately, it doesn't work for a particular reason…
My fragment:
package info.androidhive.tabsswipe;
import java.io.IOException;
import java.util.List;
import be.unamur.confpers.beans.Paper;
import be.unamur.confpers.xml.XMLParser2;
import info.androidhive.tabsswipe.R;
import info.androidhive.tabsswipe.adapter.PaperAdapter;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import be.unamur.confpers.beans.Paper;
import be.unamur.confpers.xml.XMLParser2;
import be.unamur.confpers.*;
public class PapersFragment extends Fragment {
private PaperAdapter listAdapter;
private Context myContext;
List<Paper> papers = null;
private List<Paper> papersParser () {
try {
XMLParser2 parser = new XMLParser2();
papers = parser.parse(getActivity().getApplicationContext().getAssets().open("talks.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return papers;
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_papers, container, false);
ListView lv = (ListView) getActivity().findViewById(R.id.check1);
listAdapter = new PaperAdapter(getActivity(),papers);
//lv.setAdapter(listAdapter);
return v;
}
}
My adapter :
package info.androidhive.tabsswipe.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import be.unamur.confpers.beans.Paper;
import info.androidhive.tabsswipe.R;
import java.util.ArrayList;
import java.util.List;
public class PaperAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Paper> papers;
private LayoutInflater inflater;
public PaperAdapter(Context context, List<Paper> papers) {
this.context = context;
this.papers = papers;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount(){
return papers.size();
}
#Override
public Object getItem(int position) {
return papers.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
// We only create the view if its needed
if (view == null) {
view = inflater.inflate(R.layout.child_row, null);
// Set the click listener for the checkbox
view.findViewById(R.id.check1).setOnClickListener(this);
}
Paper p = (Paper) getItem(position);
// Set the example text and the state of the checkbox
CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
//cb.setChecked(p.isSelected());
// We tag the data object to retrieve it on the click listener.
TextView paper = (TextView)view.findViewById(R.id.papername);
if (paper != null)
paper.setText(p.getTitle());
TextView author = (TextView)view.findViewById(R.id.authorname);
if( author!= null )
author.setText( p.getAuthor() );
return view;
}
/*#Override
/** Will be called when a checkbox has been clicked. */
public void onClick(View view) {
}
}
My XML files :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#+id/buttonsave" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/papername"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<Button
android:id="#+id/buttonsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/btn_save" />
<Button
android:id="#+id/buttonload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/btn_load" />
</RelativeLayout>
Group_row.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="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/papername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
Child_row.xml :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal"
android:shrinkColumns="0">
<!-- 2 columns -->
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="20dp"
>
<CheckBox
android:id="#+id/check1"
android:layout_height="wrap_content"
android:layout_width="30dp"
android:focusable="false"
/>
<TextView
android:id="#+id/papername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:textSize="16sp"
android:textStyle="normal"
android:layout_marginLeft="5dp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingBottom="15dp" >
<TextView
android:id="#+id/authorname"
android:layout_span="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:textSize="16sp"
android:textStyle="italic"
android:layout_marginRight="5dp" />
</TableRow>
</TableLayout>
If I leave
//lv.setAdapter(listAdapter);
commented, it loads, but as soon as I uncomment it, it crashes with the following error:
12-06 11:28:05.240: E/AndroidRuntime(1362): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView
with reference to this precise line.

You make cast checkBox to ListView !
It should be like that.
ListView lv = (ListView) getActivity().findViewById(R.id.list);

I gusse here is the error in your code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_papers, container, false);
ListView lv = (ListView) getActivity().findViewById(R.id.check1);
//Here your casting checkBox to Listview Pleas Check it out
listAdapter = new PaperAdapter(getActivity(),papers);
//lv.setAdapter(listAdapter);
return v;
}
just replace it like
ListView lv = (ListView) v.findViewById(R.id.ListviewName);

Related

Custom ListView doen't show

Can someone tell me what mistake i am making while making a custom listview because it doesnt show any data when using a custom adapter. While it works on a simple arrayadapter i do't get it is there some mistake in my customly made adapter??
This is the my main xml
<?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"
android:orientation="vertical"
tools:context=".SubCategories"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1">
<TextView
android:gravity="center"
android:id="#+id/suitablecategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Services"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.Large" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1">
<Spinner
android:id="#+id/Spin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="8">
<ListView
android:id="#+id/SubListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
This is my row 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:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/CustomListItemName"
android:layout_gravity="center"
android:hint="Name"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:hint="Enter Price"
android:layout_gravity="center"
android:id="#+id/CustomListItemPrice"
android:inputType="number"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
This is my customly made adapter
package application.fyp.com.myfypapplication;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class PriceListAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> items;
public PriceListAdapter(Context context, List<String> list) {
super(context, R.layout.custom_price_listview,R.id.CustomListItemName,list);
this.context = context;
this.items = list;
}
#Override
public int getCount() {
return 0;
}
#Nullable
#Override
public String getItem(int position) {
return super.getItem(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_price_listview, parent,false);
TextView name = convertView.findViewById(R.id.CustomListItemName);
EditText price = convertView.findViewById(R.id.CustomListItemPrice);
String it = items.get(position);
name.setText(it);
return convertView;
}
}
This is my main class where i am intiallizing and setting the listview using the adapter
package application.fyp.com.myfypapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class SubCategories extends AppCompatActivity {
private Spinner spinner;
private ListView lv;
private Button addsc;
private PriceListAdapter priceadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_categories);
spinner=(Spinner)findViewById(R.id.Spin);
lv = findViewById(R.id.SubListView);
List<String> subCategoryList = new ArrayList<>();
subCategoryList.add("A");
subCategoryList.add("B");
subCategoryList.add("C");
subCategoryList.add("D");
priceadapter = new PriceListAdapter(SubCategories.this, subCategoryList);
lv.setAdapter(priceadapter);
}
In you adapter, you must change following:
#Override
public int getCount() {
// return 0; // This is what you have now
return this.items.size(); // This is what it must be
}
getCount method is intended to return the number of items in the list view.

How to fix OnItemClickListener on ListView

This is java class MainActivity.java
package com.example.mhn.intercoapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirectoryActivity extends AppCompatActivity {
ListView listView;
ImageView back_button;
EmployeeDir obj;
ArrayList <EmployeeDir> empDir ;
EmployeeDirAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_directory);
listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
obj = new EmployeeDir();
empDir = new ArrayList<>();
adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
obj.setEmp_name("Hafiz Sadique Umar");
obj.setEmp_email("hsu#gmail.com");
obj.setEmp_contact_num("+923045607057");
empDir.add(obj);
empDir.add(obj);
empDir.add(obj);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
startActivity(intent);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
}
}
This is activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EmployeeDirectoryActivity">
<RelativeLayout
android:id="#+id/relatice_layout_header_emp_directory_activity_xml"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="56dp">
<ImageView
android:id="#+id/back_button_header_emp_directory_activity_xml"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:src="#drawable/back_arrow_header"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Directory"
android:textColor="#fff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:clickable="true"
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
This is Adapter
package com.example.mhn.intercoapp.Adapters;
import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater;
private ArrayList<EmployeeDir> menuData=new ArrayList<>();
public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
{
this.context = context;
this.menuData=EmpDir;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return menuData.size();
}
#Override
public Object getItem(int i) {
return menuData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEmpDir holder;
if (convertView == null) {
holder = new ViewHolderEmpDir();
convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
holder.phone = (TextView) convertView.findViewById(R.id.phone);
holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderEmpDir) convertView.getTag();
}
holder.name.setText(menuData.get(position).getEmp_name());
holder.email.setText(menuData.get(position).getEmp_email());
holder.phone.setText(menuData.get(position).getEmp_contact_num());
//Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
//Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
holder.pic.setImageResource(R.drawable.user_sign_up);
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
}
});
//downloadImage(position,holder);
return convertView;
}
// private void downloadImage(int position,ViewHolderShowAllBooking holder)
// {
// Picasso.with(context)
// .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
// .fit() // will explain later
// .into(holder.profile);
// }
static class ViewHolderEmpDir
{
TextView name;
TextView email;
TextView phone;
ImageView pic;
ImageView viewButton;
}
}
This is list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:background="#color/colorAccent">
<ImageView
android:id="#+id/emp_pic_emp_directory_list_row_layout"
android:layout_marginTop="5dp"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:src="#drawable/user_sign_up"/>
</RelativeLayout>
<RelativeLayout
android:background="#color/colorAccent"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3">
<TextView
android:id="#+id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="18dp"
android:layout_alignParentLeft="true"
android:text="Husnain"/>
<TextView
android:id="#+id/phone"
android:layout_toRightOf="#id/name_textView_emp_directory_list_row_layout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:autoLink="phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***********"
android:textColor="#fff"/>
<TextView
android:id="#+id/email_textView_emp_directory_list_row_layout"
android:layout_alignParentLeft="true"
android:layout_below="#id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mhn786#gmail.com"
android:autoLink="email"
android:layout_marginLeft="10dp"
android:textSize="18dp"
android:textColor="#fff"
/>
<ImageView
android:id="#+id/view_button_emp_dir_list_row_layout"
android:src="#drawable/view"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
When I apply OnItemClickListener it does not do any action like intent inside the func not called. I tried toast too but nothing shown on clicking the listView Items.
I also tried OnItemSlectedListener , it also not worked for me.
What is the problem here with this code. ?
Thanks Every One. Issue was with autolink on email and phone, I make their focusable=false but nothing happened.Then I remove it and guess what? Clicklistener also starts working.
The attribute android:clickable="true" in your ListView could be overriding all of click events of its child views, you should probably remove it:
<ListView
android:clickable="true" <!-- Remove this attribute -->
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

I want to create an Android Page which looks like this

I am beginner Android Developer. I want to create an Android Page which will dynamically display Three things 1. Question No. 2. Your Answer 3. Right Answer. How can I do this? It should look like this and can grow dynamically
What about something like this (Uses a listview):
Mainactivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Question> questionArrayList;
private void fillList(){
questionArrayList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
questionArrayList.add(new Question(i,"myans","yourans"));
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillList();
ListView mlIstView = (ListView)findViewById(R.id.mListview);
QuestionAdapter adapter = new QuestionAdapter(this,R.layout.listviewlayout,questionArrayList);
mlIstView.setAdapter(adapter);
}
}
Question.java
public class Question {
private int numberOfQuestion;
private String yourAnswer;
private String correctAnswer;
public Question(int numberOfQuestion, String yourAnswer, String correctAnswer) {
this.numberOfQuestion = numberOfQuestion;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public int getNumberOfQuestion() {
return numberOfQuestion;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
QuestionAdapter
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by waro on 6/4/17.
*/
public class QuestionAdapter extends ArrayAdapter<Question> {
private ArrayList<Question> arrayList;
public QuestionAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<Question> objects) {
super(context, resource, objects);
arrayList = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout,parent,false);
}
Question question = arrayList.get(position);
TextView questiontv = (TextView)convertView.findViewById(R.id.question);
TextView yourans = (TextView)convertView.findViewById(R.id.yourans);
TextView correctans = (TextView)convertView.findViewById(R.id.correctans);
questiontv.setText(Integer.toString(question.getNumberOfQuestion()));
yourans.setText(question.getYourAnswer());
correctans.setText(question.getCorrectAnswer());
return convertView;
}
}
activity_mail.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.suprentive.waro.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:id="#+id/mLinearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Your answer"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right answer"
android:layout_weight="1"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mListview"
android:layout_below="#id/mLinearLayout">
</ListView>
</RelativeLayout>
listviewlayout.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="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/yourans"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/correctans"
android:layout_weight="1"/>
</LinearLayout>
Result
Hopefully this helps you out.

'android.R.id.list' that is not a ListView class in ListFragment

I am new to android development. I have created a List View for displaying items in custom formats. I have given code below that I've used in eclipse. But I'm receiving an error while running the application, "Content has view with id attribute 'android.R.id.list' that is not a ListView class in ListFragment".
This is Activity
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CalllogActivity extends ListFragment implements AdapterView.OnItemClickListener{
private CLAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.CallLogs, R.layout.layout_calllogs);
ListView lstDetails= (ListView) getView().findViewById(R.id.lstDtls);
adapter=new CLAdapter(this);
lstDetails.setAdapter(adapter);
lstDetails.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l) {
Toast.makeText(getActivity(), "Item " + i, Toast.LENGTH_SHORT).show();
}
class CLAdapter extends BaseAdapter{
private Context context;
String[] CallLogNames;
String[] CallNo;
int[] images;
String[] CallDateTime;
public CLAdapter(Context context){
this.context=context;
CallLogNames = context.getResources().getStringArray(R.array.CallLogs);
}
public CLAdapter(CalllogActivity calllogActivity) {
CallLogNames = context.getResources().getStringArray(R.array.CallLogs);
}
#Override
public int getCount() {
return CallLogNames.length;
}
#Override
public Object getItem(int position) {
return CallLogNames[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=null;
if(convertView==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_calllogs, parent, false);
}
else
{
row=convertView;
}
TextView txtName=(TextView) row.findViewById(R.id.txtName);
ImageView imgPhoto=(ImageView) row.findViewById(R.id.imgContact);
TextView txtNo = (TextView) row.findViewById(R.id.txtNo);
TextView txtDateTime = (TextView)row.findViewById(R.id.txtDateTime);
txtName.setText(CallLogNames[position]);
//txtNo.setText(CallLogNames[position]);
//txtDateTime.setText(CallLogNames[position]);
//imgPhoto.setImageResource(images[position]);
return row;
}
}
}
This is the Fragment XML Code
<?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/lstDtls"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
This is the custom layout I've created for showing in the List
<?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/imgContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:src="#drawable/unknown_contact" />
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imgContact"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgContact"
android:layout_alignRight="#+id/txtName"
android:layout_marginBottom="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtName"
android:layout_alignBottom="#+id/txtName"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
When you want to use ListFragment or ListActivity list in your layout has to use predefined id:
android:id="#android:id/list"
In your code you can retrieve this list by using
ListView listview = (ListView)findViewById(android.R.id.list);
ListFragment and ListActivity have hard coded this value and do not work if they cannot find list to use.
Try to change Id of listView
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
then use R.id.list not android.R.id.list
To add onto this, it's worth noting that in the ListFragment spec it mentions that the listview named 'list' must have either the sibling android.R.id.empty, or not be empty. My issue was that the listview was both empty and didn't have the empty sibling.
The fix was to add a textview named empty,
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list">
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>

Different Column name in grid view in android

Hi i am developing android application. Now at this stage i am developing calendar which is in week view. For doing that i have use grid view.In grid view there is 7 columns per week days. Each column have one textview which display name of the week day.Now i want to display different week days in each column.But I don't know how to achieve this as i am new in this field. i am using custom adapter for this.Here is code Any help is appreciated.
Here on 6 in want to display Name of week days.
package com.gridlayoutdemo;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
public class GridLayoutDemo extends Activity {
/** Called when the activity is first created. */
public String[] weekdays={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyAdapter(this));
}
}
class MyAdapter extends BaseAdapter {
Context context;
public MyAdapter(Context context) {
this.context = context;
}
public int getCount() {
return 7;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater li = LayoutInflater.from(context);
convertView = li.inflate(R.layout.main1, null);
holder = new ViewHolder();
holder. tvWeekDay = (TextView)convertView.findViewById(R.id.tvWeekDayName);
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
holder.tvWeekDay.setText(weekdays[position]);
}
}
// holder.tvWeekDay.setText(Integer.toString(Calendar.MONTH));
TextView tvDate = (TextView)convertView.findViewById(R.id.tvDate);
TextView tvNotes = (TextView)convertView.findViewById(R.id.tvNotes1);
convertView.setTag(holder);
//tvWeekday.setText(c.get(Calendar.MONTH));
tvNotes.setText(" Click here for new Note");
tvNotes.setTextSize(10);
tvNotes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" Click is successful");
}
});
TextView tv2 = (TextView)convertView.findViewById(R.id.tvNotes2);
tv2.setText(" Click here for new Note" );
tv2.setTextSize(10);
tv2.setLayoutParams(new LayoutParams(140, 200));
tv2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("1.Click is successful");
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
return convertView;
}
static class ViewHolder {
TextView tvWeekDay;
}
}
//main1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/LlHeader"
android:background="#drawable/calendar_top_header">
<TextView
android:id="#+id/tvWeekDayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:text="Friday"/>
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:text="25-NOV-2011"/>
</LinearLayout>
<TextView android:id="#+id/tvNotes1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:id="#+id/tvNotes2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
//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:orientation="vertical"
android:background="#f5f5f5"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:background="#drawable/calendar_top_header">
<ImageView
android:id="#+id/imgleftarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "#drawable/cal_left_arrow_off">
</ImageView>
<TextView
android:id="#+id/tvMonthName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_marginLeft="80dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:text="November"/>
<ImageView
android:id="#+id/imgrightarrow"
android:layout_width="wrap_content"
android:layout_marginLeft="95dp"
android:layout_height="wrap_content"
android:src = "#drawable/cal_right_arrow_off">
</ImageView>
<!-- </LinearLayout> -->
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:numColumns="2"
android:layout_height="fill_parent"
android:background="#f5f5f5"
/>
</LinearLayout>

Categories

Resources