Resource Not found exception in android - android

i am trying to create a listview in my android application. But i am getting Resource Not found Exception while running the project.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<include
android:id="#+id/title_bar"
layout="#layout/title_bar" />
<ListView
android:id="#+id/FeedList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_bar"
></ListView>
</RelativeLayout>
list_item.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"
android:background="#FFFFFF">
<ImageButton
android:id="#+id/FeedType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E0E0E0"
android:padding="9dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/FeedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feed Title"
android:textSize="18sp"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/FeedPostedBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FeedTitle"
android:text="by FeedOwner"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:textSize="10sp"
/>
<TextView
android:id="#+id/FeedContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FeedPostedBy"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:paddingTop="8dp"
android:text="The content posted as a feed by the feed owner will appear here"
android:textSize="10sp"/>
</RelativeLayout>
ListAdapter Class :
package com.tcs.internal.prime;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter{
private Activity listAdapterActivity = null;
private ArrayList<HashMap<String,String>> data;
private static LayoutInflater inflater;
public ListAdapter(Activity ListActivity,ArrayList<HashMap<String, String>> listData)
{
listAdapterActivity = ListActivity;
data = listData;
inflater = (LayoutInflater)listAdapterActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItem = convertView;
if(listItem == null)
listItem = inflater.inflate(R.id.FeedList, null);
ImageButton feedIcon = (ImageButton) listItem.findViewById(R.id.FeedType);
TextView feedTitle = (TextView) listItem.findViewById(R.id.FeedTitle);
TextView feedOwner = (TextView) listItem.findViewById(R.id.FeedPostedBy);
TextView feedContent = (TextView) listItem.findViewById(R.id.FeedContent);
HashMap<String, String> feedData = new HashMap<String, String>();
feedData = data.get(position);
feedIcon.setImageResource(R.drawable.ic_launcher);
feedTitle.setText(feedData.get("FeedTitle"));
feedOwner.setText(feedData.get("FeedOwner"));
feedContent.setText(feedData.get("FeedContent"));
return listItem;
}
}
MainActivity class :
package com.tcs.internal.prime;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> data = new HashMap<String, String>();
data.put("FeedTitle","Application crashed when launched in Windows 7");
data.put("FeedOwner", "By Venkatramanan");
data.put("FeedContent","Launch the financial aid adminstration in windows 7 environment");
feedList.add(data);
data.clear();
data.put("FeedTitle","Application crashed when launched in Windows 8 ");
data.put("FeedOwner", "By Siva Guru");
data.put("FeedContent","Launch the financial aid adminstration in windows 8 environment");
feedList.add(data);
ListView feedListView = (ListView)findViewById(R.id.FeedList);
ListAdapter listAdapter = new ListAdapter(this, feedList);
feedListView.setAdapter(listAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I am getting this following exception :
android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is not valid

All you have to inflate the custom layout in your listview's adapter's getview method as
listItem = inflater.inflate(R.layout.list_item, null);
instead of
listItem = inflater.inflate(R.id.FeedList, null);

Related

How to parse a website and display in a listview fragment android

I am trying to create an app that displays the menus of each dining hall at my school. How would I go about parsing this website https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18 or https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18 and getting the menus for breakfast lunch and dinner into a listview that displays on a fragment?
This is the code I have:
ErcFragment.java
import android.view.View;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.app.ProgressDialog;
import android.widget.TextView;
import java.util.ArrayList;
public class ErcFragment extends Fragment {
// URL Address
String url = "http://hdh.ucsd.edu/mobile/dining/locationdetails.aspx?l=18&no_server_init";
ProgressDialog mProgressDialog;
String MENU;
ListView listview;
TextView textview;
#Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_erc, container, false);
View view = inflater.inflate(R.layout.fragment_erc, container, false);
textview = (TextView)view.findViewById(R.id.textView10);
new JSOUP().execute();
return rootView;
}
public class JSOUP extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
#Override
protected void onPreExecute(){
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("loading...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params){
try{
Document document = Jsoup.connect(url).get();
Elements elements = document.select("div[class='message info last']");
for(int i = 0; i<elements.size(); i++){
MENU += "\n" + elements.get(i).text();
}
}
catch (Exception e){
}
return null;
}
#Override
protected void onPostExecute(Void result){
dialog.dismiss();
textview.setText(MENU);
super.onPostExecute(result);
}
}
}
and fragment_erc.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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/textAppearanceLarge"
android:text="Cafe Ventanas"
android:id="#+id/textView9"
android:layout_gravity="center_horizontal"
android:elegantTextHeight="false"
android:textSize="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView10"
android:layout_gravity="center" />
</FrameLayout>
if you are successfuly getting data from server the follow the below code to display it in list fragment-
Here is the example code.
First create a layout for one list row.
example : list_row.xml
<?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="wrap_content"
android:padding="8dp"
>
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="#+id/merk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/harga"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/merk"
android:layout_marginTop="5dp"/>
</RelativeLayout>
After creating this list_row.xml layout, create a adapter class.
create CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private ArrayList<Barang> barangList;
public CustomListAdapter(Activity activity, ArrayList<Barang> barangList) {
this.activity = activity;
this.barangList = barangList;
}
/*
get count of the barangList
*/
#Override
public int getCount() {
return barangList.size();
}
#Override
public Object getItem(int location) {
return barangList.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
/*
inflate the items in the list view
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row, null);
}
/*
creating objects to access the views
*/
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView merk = (TextView) convertView.findViewById(R.id.merk);
TextView harga = (TextView) convertView.findViewById(R.id.harga);
// getting barang data for the row
Barang barang = barangList.get(position);
name.setText(barang.getNama_barang());
merk.setText(barang.getMerk_barang());
harga.setText(barang.getHarga_barang());
return convertView;
}}
Now in your MasterBarang.java, put the following code in your onCreate method.
values = dataSource.getAllBarang();
CustomListAdapter adapter;
adapter = new CustomListAdapter(getActivity(), values);
setListAdapter(adapter);

ListFragment is not display Data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am working on a project where I have to display a list of available Professors for a department. Instead of displaying all professors, I want to allow a student to search professors by Department. As a result, the student will logs in, search for a professor based on name, department, course ID, or all. I am using a custom list adapter. I noticed that Adapter object is not null, but the list is still showing "No records available to display. I think the issue has to do with the way that I am setting the adapter in the ProfessorDetail class. What am I doing wrong here?
See codes below:
Professors Class
package com.mb.professor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.mb.professor.asynctasks.ProfessorAsyncTask;
/**
* Created by Gagouche on 7/25/13.
*/
public class Professors extends FragmentActivity {
private Button bSearch;
private EditText etSearchBy;
private EditText etCourseId;
private Spinner spSearchDecision;
private String searchValue = null;
private Professor[] _professor = null;
public Professors()
{
}
public Professors(Professor[] professor)
{
this._professor = professor;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.professor);
InitialVariables();
}
private void InitialVariables()
{
bSearch = (Button) this.findViewById(R.id.bSearch);
etSearchBy = (EditText) this.findViewById(R.id.etSearchByDepartment);
etCourseId = (EditText) this.findViewById(R.id.etSearchByCouseId);
spSearchDecision = (Spinner) this.findViewById(R.id.spOption);
bSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(searchValue == "Department")
{
ProfessorAsyncTask professor = new ProfessorAsyncTask();
professor.execute("");
FragmentManager mFragment = getSupportFragmentManager();
FragmentTransaction ft = mFragment.beginTransaction();
ProfessorDetail pDetail = new ProfessorDetail();
pDetail.setProfessorAdapter(_professor);
ft.replace(R.id.fprofessorDetail, pDetail);
ft.addToBackStack(null);
ft.commit();
}
}
});
spSearchDecision.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
searchValue = "By All";
break;
case 1:
searchValue = "Department";
break;
case 2:
searchValue = "CourseId";
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
searchValue = "By All";
}
});
}
}
ProfessorDetail Class
package com.mb.professor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.mb.professor.adapter.*;
/**
* Created by Gagouche on 7/25/13.
*/
public class ProfessorDetail extends ListFragment {
private ProfessorAdapter adapter = null;
Professor[] prof = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setProfessorAdapter(Professor[] professor)
{
prof = professor;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String [] emptyAdapter = {};
if(prof == null)
{
ArrayAdapter<String> emptyListAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,emptyAdapter);
setListAdapter(emptyListAdapter);
} else
{
adapter = new ProfessorAdapter(getActivity(), R.layout.professor_row, prof);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.customizelayout, container, false);
}
}
Course Detail Class
package com.mb.professor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.mb.professor.adapter.CourseAdapter;
import java.util.List;
/**
* Created by Gagouche on 7/25/13.
*/
public class CourseDetail extends ListFragment {
private CourseAdapter adapter = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setCourseData(Course[] course)
{
adapter = new CourseAdapter(getActivity(), R.layout.course_row, course);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] course = {};
if(adapter == null)
{
ArrayAdapter<String> emptyAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1 , course );
setListAdapter(adapter);
} else
{
setListAdapter(adapter);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.customizelayout, container, false);
}
}
Login Course
package com.mb.professor;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Login extends Activity implements View.OnClickListener {
Button btnLogin;
EditText etUsername, etPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
initializeVariables();
}
private void initializeVariables()
{
btnLogin = (Button) this.findViewById(R.id.btnLogin);
etUsername = (EditText) this.findViewById(R.id.etUserName);
etPassword = (EditText) this.findViewById(R.id.etPassword);
btnLogin.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
#Override
public void onClick(View v) {
String uName = etUsername.getText().toString();
String pWord = etPassword.getText().toString();
if(uName.equals("college") && pWord.equals("florida"))
{
Intent intent = new Intent(this, Professors.class);
startActivity(intent);
}
}
}
Professor AsyncTask
package com.mb.professor.asynctasks;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import com.mb.professor.Course;
import com.mb.professor.Professor;
import com.mb.professor.ProfessorDetail;
import com.mb.professor.Professors;
import com.mb.professor.R;
/**
* Created by Gagouche on 8/1/13.
*/
public class ProfessorAsyncTask extends AsyncTask<String,Void,Professor[]> {
private FragmentActivity fActivity;
public ProfessorAsyncTask()
{
}
#Override
protected Professor[] doInBackground(String... params) {
Professor[] professor = new Professor[5];
professor[0] = new Professor("Finance", "John", "123", "Accounting");
professor[1] = new Professor("Accounting", "Cain", "124", "finance");
professor[2] = new Professor("Database", "Eugene", "125", "Music");
professor[3] = new Professor("Finance", "Seikei", "126", "Engineer");
professor[4] = new Professor("Finance", "Bojok", "127", "Math");
return professor;
}
#Override
protected void onPostExecute(Professor[] professors) {
super.onPostExecute(professors);
// ProfessorDetail professorDetailFragment = (ProfessorDetail)fActivity.getSupportFragmentManager().findFragmentById(R.id.fprofessorDetail);
// Professors prof = new Professors(professors);
}
}
Course AsyncTask
package com.mb.professor.asynctasks;
import android.app.FragmentTransaction;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import com.mb.professor.Course;
import com.mb.professor.CourseDetail;
import com.mb.professor.R;
import java.util.List;
/**
* Created by Gagouche on 8/1/13.
*/
public class CourseAsyncTask extends AsyncTask<String, Void, Course[]> {
private FragmentActivity mActivity;
private Course[] course = null;
public CourseAsyncTask(FragmentActivity activity)
{
this.mActivity = activity;
}
#Override
protected Course[] doInBackground(String... params) {
Course[] course = new Course[5];
course[0] = new Course("Finance", "John", "123");
course[1] = new Course("Accounting", "Cain", "124");
course[2] = new Course("Database", "Eugene", "125");
course[3] = new Course("Finance", "Seikei", "126");
course[4] = new Course("Finance", "Bojok", "127");
return course;
}
#Override
protected void onPostExecute(Course[] courses) {
super.onPostExecute(courses);
CourseDetail courseDetailFragment = (CourseDetail) mActivity.getSupportFragmentManager().findFragmentById(R.id.fCourseDetail);
courseDetailFragment.setCourseData(courses);
}
}
Professor Adapter
package com.mb.professor.adapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.mb.professor.*;
import java.util.List;
/**
* Created by Gagouche on 8/1/13.
*/
public class ProfessorAdapter extends ArrayAdapter<Professor> {
private Context context;
private int layoutResourceId;
private Professor[] data = null;
public ProfessorAdapter(Context context, int layoutResourceId, Professor[] data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProfessorHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId,parent, false);
holder = new ProfessorHolder();
holder.tvFirstName = (TextView) row.findViewById(R.id.tvFirstName);
holder.tvLastName = (TextView) row.findViewById(R.id.tvLastName);
holder.tvCourse = (TextView) row.findViewById(R.id.tvCourseID);
holder.tvDepartment = (TextView) row.findViewById(R.id.tvDepartment);
row.setTag(holder);
} else
{
holder = (ProfessorHolder) row.getTag();
}
Professor item = data[position];
holder.tvFirstName.setText(item.getFirstName());
holder.tvLastName.setText(item.getLastName());
holder.tvDepartment.setText(item.getDepartmentID());
holder.tvCourse.setText(item.getCourseId());
return row;
}
static class ProfessorHolder
{
TextView tvFirstName;
TextView tvLastName;
TextView tvDepartment;
TextView tvCourse;
}
}
Course Adapter
package com.mb.professor.adapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.mb.professor.Course;
import com.mb.professor.R;
/**
* Created by Gagouche on 8/1/13.
*/
public class CourseAdapter extends ArrayAdapter<Course> {
private Context context;
private int layoutResourceId;
private Course[] data = null;
public CourseAdapter(Context context, int resource, Course[] data) {
super(context, resource, data);
this.context = context;
this.layoutResourceId = resource;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CourseHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CourseHolder();
holder.tvCourseInstructor = (TextView) row.findViewById(R.id.tvCourseInstructor);
holder.tvCourseId = (TextView) row.findViewById(R.id.tvCourse);
holder.tvCourseName = (TextView) row.findViewById(R.id.tvCourseName);
row.setTag(holder);
} else {
holder = (CourseHolder) row.getTag();
}
Course item = data[position];
holder.tvCourseId.setText(item.getCourseId());
holder.tvCourseName.setText(item.getCourseName());
holder.tvCourseInstructor.setText(item.getInstructorName());
return row;
}
static class CourseHolder
{
TextView tvCourseName;
TextView tvCourseInstructor;
TextView tvCourseId;
}
}
Login XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textSize="30sp"
/>
<EditText
android:layout_width="200sp"
android:layout_height="wrap_content"
android:id="#+id/etUserName"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/textView2"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textSize="30sp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/etPassword"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/btnLogin"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>
Professor 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">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search For Professor"
android:id="#+id/textView"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="30sp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="search by Department"
android:id="#+id/etSearchByDepartment"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search by Course ID"
android:id="#+id/textView2"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="30sp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="#+id/etSearchByCouseId"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<Spinner
android:layout_width="321dp"
android:layout_height="wrap_content"
android:id="#+id/spOption"
android:entries="#array/searchOption"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/bSearch"
android:text="Search"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="com.mb.professor.ProfessorDetail"
android:id="#+id/fprofessorDetail"
android:layout_weight="1"
android:layout_gravity="center"/>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:name="com.mb.professor.CourseDetail"
android:id="#+id/fCourseDetail"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
Professor Row Custom 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:text="First Name"
android:layout_marginLeft="10dp"
android:id="#id/tvFirstName"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:layout_marginLeft="10dp"
android:id="#id/tvFirstName"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_marginLeft="10dp"
android:id="#id/tvDepartment"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="New Text"
android:id="#id/tvCourseID"
android:layout_gravity="center_horizontal|top"/>
</LinearLayout>
Customize Layout for the ListViews
<?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">
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"/>
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Record to be displayed."
android:textSize="25sp"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
Here is the exception:
08-05 13:26:25.619 716-731/com.android.exchange D/ExchangeService: Received deviceId from Email app: null
08-05 13:26:25.619 716-731/com.android.exchange D/ExchangeService: !!! deviceId unknown; stopping self and retrying
08-05 13:26:27.242 1055-1055/com.mb.professor D/AndroidRuntime: Shutting down VM
08-05 13:26:27.242 1055-1055/com.mb.professor W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-05 13:26:27.302 1055-1055/com.mb.professor E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:154)
at android.widget.ArrayAdapter.(ArrayAdapter.java:128)
at com.mb.professor.adapter.ProfessorAdapter.(ProfessorAdapter.java:23)
at com.mb.professor.ProfessorDetail.setProfessorAdapter(ProfessorDetail.java:23)
at com.mb.professor.Professors$1.onClick(Professors.java:57)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Am I mistaking ? but you call notifyDataSetChange before the asynctask is finished. You have to call notifyDataSetChange in the onPostExecute of your async task. And therefore your list is empty.
In your code you do in the Professor detail class
adapter = new ProfessorAdapter(getActivity(), R.layout.professor_row, prof);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
you notify the data change and link the adapter to the list. But you never do it again. Your onPost Execute is empty.
What you have to do is move the adapter.notifyDataSetChanged(); to the onPostExecute and your list will be refreshed.
EDIT
Let me show you an example of my code.
public class MyAsyncTaskFragment extends Fragment {
private ListView lv;
private CustomAdapter adapter;
private ArrayList<...your custom type here...> listItems = new ArrayList<...your custom type here...>();
...
#Override
public void onActivityCreated (Bundle savedInstanceState){
//get the listView
lv = (ListView) getView().findViewById(R.id.my_listview_name);
//create new adapter
adapter = new CustomAdapter();
//link adapter to listview
lv.setAdapter(adapter);
//in my case I want the async task to start over when the activity is created
new DoAsyncTask().execute();
}
private class DoAsyncTask extends AsyncTask<Void, Integer, Integer>{
protected void onPreExecute() {
listItems.clear();
}
protected Integer doInBackground(Void...voids) {
//add items to the list
listItems.add(...your custom type here ...);
return null;
}
protected void onPostExecute(Integer result) {
if (listItems.size() > 0){
adapter.notifyDataSetChanged();
}
}
}
}

Listvew is not populated and getView function is also not getting called

I am new to Android programming. I'm trying Shopping cart tutorial trying to populate the listview of catalog view using the productAdapter. But the list is not getting populated and also getview function is not getting called. I'am attaching my files. Please let me know,where I'am going wrong.
ProductAdapter.java
package com.example.helloshoppingcart;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class ProductAdapter extends BaseAdapter {
private class viewItem{
ImageView productImageView;
TextView productTitle;
CheckBox productCheckbox;
};
private List<Product> mproductList;
private LayoutInflater minflater;
private Boolean mShowCheckbox;
ProductAdapter(List<Product> productList, LayoutInflater inflater, Boolean showCheckbox)
{
this.mproductList = productList;
this.minflater = inflater;
this.mShowCheckbox = showCheckbox;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent ){
final viewItem item;
if (convertView == null){
convertView = minflater.inflate(R.layout.item, null);
item = new viewItem();
item.productImageView = (ImageView) convertView.findViewById(R.id.ImageViewItem);
item.productTitle= (TextView) convertView.findViewById(R.id.TextViewItem);
//item.productCheckbox = (CheckBox) convertView.findViewById(R.id.CheckBoxSelected);
convertView.setTag(item);
}else{
item = (viewItem) convertView.getTag();
}
Product prod = mproductList.get(pos);
item.productImageView.setImageDrawable(prod.productImage);
//item.productCheckbox.setChecked(prod.selected);
item.productTitle.setText(prod.title);
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
CatalogActivity.java
package com.example.helloshoppingcart;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class CatalogActivity extends Activity {
private List<Product> mproductList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
//get the catalog and display all the records
mproductList = shoppingCartHelper.getCatalog(getResources());
ListView catalogListView = (ListView) findViewById(R.id.ListViewCatalog);
ProductAdapter catalogListAdapter = new ProductAdapter(mproductList, getLayoutInflater(), false);
catalogListView.setAdapter(catalogListAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_catalog, menu);
return true;
}
}
activity_catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff">
<!--<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#000000"
android:textSize="24dip" android:layout_margin="5dip" android:text="Product Catalog"></TextView>-->
<ListView android:layout_height="wrap_content"
android:layout_weight="1" android:id="#+id/ListViewCatalog"
android:layout_width="fill_parent" android:background="#ffffff"
android:clickable="true" android:cacheColorHint="#ffffff">
</ListView>
<!-- <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="5dip"
android:layout_gravity="right" android:id="#+id/ButtonViewCart"
android:text="View Shopping Cart"></Button>-->
</LinearLayout>
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="vertical" >
<ImageView android:id="#+id/ImageViewItem"
android:layout_margin="5dip"
android:layout_height="wrap_content"
android:layout_width="100dip">
</ImageView>
<TextView android:id="#+id/TextViewItem"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:textSize="26dip"
android:text="Phone Names"
android:textColor="#000000"
android:minLines="2"
android:maxWidth="150dip">
</TextView>
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</TextView>
<!-- <CheckBox android:layout_height="wrap_content"
android:layout_margin="5dip" android:id="#+id/CheckBoxSelected"
android:focusable="false" android:clickable="false"
android:layout_gravity="center" android:layout_width="wrap_content">
</CheckBox>-->
</LinearLayout>
In ProductAdapter, the getCount() method returns the number of items owned by the Adapter associated with this ListView. So, it should return the maximum number of items that the listview will display i.e. the size of the list you are using as a data source : mproductList.size()
The getView() method is not getting called because this function is returning 0 in your code.
Only took a fast glance, but getCount() should return the number of items
in the adapter, and therefore mproductList.size()

Page curl animation to flip between views of a viewflipper

I've come across several examples of the page curl animation as well as viewflippers . Is it possible to navigate between children of a viewflipper via a page curl animation . The animations i have applied to viewflippers till now were very basic such as a slide-in/slide-out and I was wondering if the same could be done/has already be implemented using a page-curl animation.
There is an open source android project here: http://code.google.com/p/android-page-curl/.
I found another one here: https://github.com/harism/android_page_curl/.
But there is no native implementation if that is what you are asking.
You need to import one module library for this, from HERE
After that use following code:-
item_page.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:textColor="#android:color/black" />
</LinearLayout>
</ScrollView>
activity_flipper_view_controller.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flip="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:text="#string/header"
android:textAppearance="#android:style/TextAppearance.Large" />
<com.aphidmobile.flip.FlipViewController
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
flip:orientation="horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:text="#string/footer"
android:textAppearance="#android:style/TextAppearance.Large" />
</LinearLayout>
FlipperAdapter.java
package pratiksha.com.pagecurlviewdemo;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
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 java.util.List;
import pratiksha.com.pagecurlviewdemo.R;
/**
* Created by User(LPT-APR2015-02) on 11/7/2016.
*/
public class FlipperAdapter extends BaseAdapter {
private AppCompatActivity appCompatActivity;
private List<String> strings;
private int[] drawableIds = {R.mipmap.ic_launcher, R.mipmap.page1, R.mipmap.page2, R.mipmap.ic_launcher,
R.mipmap.page2, R.mipmap.page1, R.mipmap.page2, R.mipmap.ic_launcher,
R.mipmap.page1};
public FlipperAdapter(AppCompatActivity appCompatActivity, List<String> strings) {
super();
this.strings = strings;
this.appCompatActivity = appCompatActivity;
}
#Override
public int getCount() {
return strings.size();
}
#Override
public String getItem(int position) {
return strings.get(position);
}
#Override
public long getItemId(int position) {
return strings.indexOf(getItem(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) appCompatActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_page, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(getItem(position));
holder.imageView.setImageResource(drawableIds[position]);
return convertView;
}
private static class ViewHolder {
private TextView textView;
private ImageView imageView;
public ViewHolder(View v) {
imageView = (ImageView)v.findViewById(R.id.image);
textView = (TextView) v.findViewById(R.id.text);
}
}
}
Main Activity.java
package pratiksha.com.pagecurlviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.aphidmobile.flip.FlipViewController;
import java.util.ArrayList;
public class FlipperViewController extends AppCompatActivity {
private FlipViewController flipViewController;
private FlipperAdapter adapter;
private ArrayList<String> stringArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flipper_view_controller);
flipViewController = (FlipViewController)findViewById(R.id.flip_view);
stringArrayList = new ArrayList<>();
readDataFromAssets();
//create and attach adapter to flipper view
adapter = new FlipperAdapter(this, stringArrayList);
flipViewController.setAdapter(adapter);
}
private void readDataFromAssets() {
for(int i=1;i<10;i++)
stringArrayList.add("Page Number "+i);
}
}

Getting ID from ListView item when clicking a checkbox

I use a ListView with a custom adapter to build a list with ckeckboxes. Now I dont know how i can fetch the "ID" from the selected box.
The xml file for the items look like this:
<?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="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView android:id="#+id/class_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="clickHandler" />
<TextView
android:id="#+id/medi_name"
android:text="Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/checkbox" />
</RelativeLayout>
I need the value from: "class_open"
this is my activity:
package de.bodprod.dkr;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MediNotmediUserListNew extends Activity{
LayoutInflater inflater;
ArrayList<HashMap<String, Object>> medi_notmedi_items;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medi_notmedi_user_list_new);
ListView mediListCheck = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
medi_notmedi_items = new ArrayList<HashMap<String,Object>>();
SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext());
medi_notmedi_items = db.getAllNotmedis();
final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_notmedi_user_list_new, medi_notmedi_items);
mediListCheck.setAdapter(adapter);
}
public void clickHandler(View view) {
}
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
super(context, textViewResourceId, Strings);
}
private class ViewHolder{
TextView class_open, title;
}
ViewHolder viewHolder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.medi_notmedi_user_list_new_item, null);
viewHolder=new ViewHolder();
viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
viewHolder.title=(TextView) convertView.findViewById(R.id.medi_name);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.class_open.setText(medi_notmedi_items.get(position).get("sql_id").toString());
viewHolder.title.setText(medi_notmedi_items.get(position).get("wirk").toString());
return convertView;
}
}
}
How I can get the value from the class_open field in the clickHandler?
Is it possible to assign the value directly to the checkbox and request it?
Is it possible to request the value from the class_open field?
Please excuse my bad english
Markus

Categories

Resources