I am trying to add a custom adapter to a listview within a fragment. It's running fine but it is not displaying anything in fragment. I've tried many methods but I am not able to get it to work. Any help would be great. Thanks in advance. Here is my code:
Fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/UniFeeList"
android:layout_centerHorizontal="true"
android:clickable="true"
>
</ListView>
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_input_add"
android:id="#+id/addQuestion"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dip"
/>
</RelativeLayout>
Fragment.Java
public class UniFeedFragment extends Fragment {
ListView uniFeedListview;
FloatingActionButton askQuestion;
String stdEmail, UniEmail;
TextView tv;
Post post;
String date,time;
ListView UniFeedList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.unifeedfragment, container, false);
//Getting Time
DateFormat df = new SimpleDateFormat("HH:mm");
time = df.format(Calendar.getInstance().getTime());
//Getting Date
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
date = dateFormatter.format(today);
//Getting data from news feed search bar
UniFeed uniFeed = (UniFeed) getActivity();
stdEmail = uniFeed.stdEmail;
UniEmail = uniFeed.uni.getEmail();
//Setting Listview
UniFeedList = (ListView) view.findViewById(R.id.UniFeeList);
final ArrayList<Post> postArray = new ArrayList<Post>();
PostListAdapter PostList = new PostListAdapter(this.getActivity(), R.layout.activity_uni_feed ,postArray);
UniFeedList.setAdapter(PostList);
return view;
}
PostListAdapter.java
public class PostListAdapter extends BaseAdapter {
Context context = null;
ArrayList<Post> postData = new ArrayList<>();
LayoutInflater inflater;
public PostListAdapter(Context context, int activity_uni_feed, ArrayList<Post> postData){
this.context = context;
this.postData = postData;
}
#Override
public int getCount() {
return postData.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.unifeedpost, parent, false);
return convertView;
} }
unifeedPost.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stdProfileImage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/stdName"
android:layout_alignTop="#id/stdProfileImage"
android:layout_toRightOf="#+id/stdProfileImage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date and Time"
android:id="#+id/time"
android:layout_below="#+id/stdName"
android:layout_toRightOf="#+id/stdProfileImage"
android:layout_toEndOf="#+id/stdProfileImage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/postContent"
android:layout_below="#id/time"
android:text="Test Post"
android:layout_marginTop="10dip"
android:layout_toRightOf="#id/stdProfileImage"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/postContent"
android:layout_toRightOf="#id/stdProfileImage"
android:layout_marginTop="10dip"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like"
android:layout_weight="0.5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment"
android:layout_weight="0.5"
/>
</LinearLayout>
Related
This is my fragment class:
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
return view;
}
This is adapter:
public class DDayMonthlyListAdapter extends BaseAdapter implements View.OnClickListener {
private Context mContext;
private ArrayList<MonthlyModel> monthlyModels;
ListView listView;
public DDayMonthlyListAdapter(Context context,ArrayList<MonthlyModel> monthlyModels, ListView listView) {
this.mContext = context;
this.monthlyModels = monthlyModels;
this.listView = listView;
}
public int getCount() {
return monthlyModels.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.item_monthly_list, parent, false);
return convertView;
}
Layout fragment_dday_monthly just have a Listview. And here is layout of Listview's item (R.layout.item_monthly_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">
<RelativeLayout
android:id="#+id/frequency_choose_container_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dday_activity_divider"
android:layout_alignBottom="#+id/frequency_choose_container_1">
<Spinner
android:id="#+id/spin_chosen_month"
android:layout_width="#dimen/spinner_frequency_width"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/img_chosen_month_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/spin_chosen_month"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frequency_choose_container_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_month"
android:layout_width="#dimen/spinner_frequency_width"
android:layout_height="wrap_content"
android:text="Monthly"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_month_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/txt_chosen_month"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frequency_monthly_picker_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/frequency_choose_container_2"
android:layout_marginTop="10dp"
>
<RadioButton
android:id="#+id/rb_frequency_date_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radiobutton_selector" />
<RelativeLayout
android:id="#+id/date_choose_container_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_date_5"
android:background="#drawable/dday_activity_divider">
<Spinner
android:id="#+id/spin_chosen_date_2"
android:layout_width="#dimen/spinner_chosen_date_width"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/sp14_text_size"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/img_chosen_date_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/spin_chosen_date_2"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/date_choose_container_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_date_5"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_date_1"
android:layout_width="#dimen/spinner_chosen_date_width"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_date_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_date_1"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RadioButton
android:id="#+id/rb_frequency_week_n_month_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/date_choose_container_2"
android:button="#drawable/radiobutton_selector" />
<RelativeLayout
android:id="#+id/daily_choose_container_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_week_n_month_5"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_day_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weekly"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_day_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_day_child_6"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/daily_choose_container_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/daily_choose_container_child_6"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_day_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_day_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_day_child_7"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<ImageView
android:id="#+id/img_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_right_10dp"
android:src="#drawable/add_ic_delete" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img_delete"
android:layout_alignLeft="#+id/img_delete"
android:layout_toLeftOf="#+id/img_add"
android:layout_marginTop="10dp"
android:src="#drawable/add_ic_add" />
</RelativeLayout>
</RelativeLayout>
As you can see I just inflate view to fragment. I dont do any heavy task, but the fragment load so long, it take 1-2 second to be shown. Please help me to fix it!
Instead of inflating , setting adapter in onCreateView do those things in onViewCreated() method
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
return view;
}
}
Like this
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
}
}
There is no need of passing Arraylist To DDayMonthlyListAdapter as this is not necessary in your case
also you should modify the code as below:
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
This must work as the solution!>..
I am having an issue with scrolling of ListView in a Popup. I have an activity that has a dialog theme so that it can open up as a popup. The Popup should contain EditText at the bottom and the remaining area above it should be the ListView. When the activity is started it should start with keypad open & focus on EditText.
Issue: ListView doesn't scroll when keypad is open.
Below is my activity code:
public class CommentsActivity extends Activity{
private InputMethodManager imm;
private String commentCount;
private ShopPirateDatabase db;
private ArrayList<CommentsBean> commentsList = new ArrayList<CommentsBean>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comments_layout);
imm = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
db=new ShopPirateDatabase(this);
if(getIntent().hasExtra("count")){
commentCount=getIntent().getExtras().getString("count");
}
ListView lvMyComments=(ListView)findViewById(R.id.lvMyComments);
lvMyComments.setVisibility(View.GONE);
final Button btnPost=(Button)findViewById(R.id.btnPost);
btnPost.setVisibility(View.GONE);
etAddComment=(EditText)findViewById(R.id.etAddComment);
RelativeLayout progressLayout=(RelativeLayout)findViewById(R.id.progressLayout);
progressLayout.setVisibility(View.VISIBLE);
TextView tvNoComments=(TextView)findViewById(R.id.tvNoComments);
tvNoComments.setVisibility(View.GONE);
imm.showSoftInput(etAddComment, InputMethodManager.SHOW_IMPLICIT);
if(Integer.parseInt(commentCount) > 0){
getCommentsFromDb();
if(commentsList!=null && commentsList.size() > 0){
progressLayout.setVisibility(View.GONE);
tvNoComments.setVisibility(View.GONE);
lvMyComments.setVisibility(View.VISIBLE);
CommentsAdapter commentsAdapter = new CommentsAdapter(
CommentsActivity.this, commentsList);
lvMyComments.setAdapter(commentsAdapter);
}
} else{
progressLayout.setVisibility(View.GONE);
tvNoComments.setVisibility(View.VISIBLE);
lvMyComments.setVisibility(View.GONE);
}
}
private void getCommentsFromDb() {
db.openDatabase();
Cursor c = db.getComments();
if (c != null) {
commentsList = null;
commentsList = new ArrayList<CommentsBean>();
if (c.moveToFirst()) {
do {
CommentsBean mBeanClass = new CommentsBean();
mBeanClass
.setCommentText(c.getString(c
.getColumnIndex(db.KEY_COMMENT_TEXT)));
mBeanClass
.setCommentedDate(c.getString(c
.getColumnIndex(db.KEY_COMMENT_DATE)));
mBeanClass
.setCommentedBy(c.getString(c
.getColumnIndex(db.KEY_COMMENT_USER)));
commentsList.add(mBeanClass);
} while (c.moveToNext());
}
}
c.close();
db.closeDatabase();
}
}
comments_layout.xml
<RelativeLayout
android:id="#+id/commentsBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="#dimen/margin_5"
android:layout_marginLeft="#dimen/margin_2"
android:layout_marginRight="#dimen/margin_5" >
<Button
android:id="#+id/btnPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="#dimen/margin_35"
android:background="#color/coupon_color"
android:paddingBottom="#dimen/margin_7"
android:paddingLeft="#dimen/margin_15"
android:paddingRight="#dimen/margin_15"
android:paddingTop="#dimen/margin_7"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_2"
android:text="Post"
android:visibility="gone"
android:textColor="#color/white"
android:textSize="#dimen/normal_textsize" />
<EditText
android:id="#+id/etAddComment"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/margin_2"
android:layout_toLeftOf="#id/btnPost"
android:layout_marginRight="#dimen/margin_2"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_5"
android:hint="#string/comment_hint" />
</RelativeLayout>
<ListView
android:id="#+id/lvMyComments"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="#dimen/margin_5"
android:divider="#color/home_bg_color"
android:dividerHeight="#dimen/divider_height"
android:scrollbars="none"
android:visibility="gone" >
</ListView>
<RelativeLayout
android:id="#+id/progressLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/commentsBox"
android:layout_marginLeft="#dimen/margin_10"
android:layout_marginRight="#dimen/margin_10" >
<ProgressBar
android:id="#+id/pBarComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/circular_progress_bar" />
<TextView
android:id="#+id/tvWaitComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/pBarComments"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/margin_5"
android:layout_marginRight="#dimen/margin_5"
android:text="Loading comments..."
android:textColor="#color/black"
android:textSize="#dimen/small_textsize" />
</RelativeLayout>
<TextView
android:id="#+id/tvNoComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/etAddComment"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_25"
android:gravity="center_vertical|center_horizontal"
android:text="No comments have been posted for this offer"
android:textColor="#color/black"
android:textSize="#dimen/normal_textsize"
android:visibility="gone" />
CommentsAdapter.java
public class CommentsAdapter extends BaseAdapter {
private Context context;
private ArrayList<CommentsBean> mArrayList = new ArrayList<CommentsBean>();
public CommentsAdapter(Context ctx, ArrayList<CommentsBean> arrayList) {
this.context = ctx;
this.mArrayList = arrayList;
}
#Override
public int getCount() {
return mArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
view = null;
convertView = null;
if (convertView == null) {
view = new View(context);
view = inflater.inflate(R.layout.list_comments, null);
TextView commentText = (TextView) view
.findViewById(R.id.commentText);
TextView postedDate = (TextView) view.findViewById(R.id.postedDate);
TextView userName = (TextView) view.findViewById(R.id.userName);
commentText.setText(mArrayList.get(position).getCommentText());
userName.setText(" - " + mArrayList.get(position).getCommentedBy());
String dt = mArrayList.get(position).getCommentedDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
String convertedDate = null;
convertedDate = sdf1.format(d);
postedDate.setText(convertedDate);
}
return view;
}
}
list_comments.xml
<ImageView android:id="#+id/cmtIcon"
android:layout_width="#dimen/margin_25"
android:layout_height="#dimen/margin_25"
android:src="#drawable/comment_list_icon"
android:layout_alignParentTop="true"
android:layout_marginTop="#dimen/margin_5"
android:layout_marginLeft="#dimen/margin_5"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/commentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_10"
android:layout_toRightOf="#id/cmtIcon"
android:layout_alignParentTop="true"
android:layout_marginTop="#dimen/margin_5"
android:textColor="#color/black"
android:textSize="#dimen/normal_textsize" />
<TextView
android:id="#+id/postedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/commentText"
android:layout_toRightOf="#id/cmtIcon"
android:layout_marginLeft="#dimen/margin_10"
android:layout_marginTop="#dimen/margin_5"
android:text="Posted on"
android:textColor="#color/black"
android:textSize="#dimen/small_textsize" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/postedDate"
android:layout_marginTop="#dimen/margin_3"
android:text="User Name"
android:layout_below="#id/commentText"
android:textColor="#color/black"
android:textSize="#dimen/normal_textsize" />
listView.setAdapter() is crashing my program. Anyone have any idea why the crash might be happening?
Logcat error:
AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
Activity:
public class SavedVideosFragment extends Fragment {
View rootview;
MySQLiteHelper mydb;
private SavedVideoAdapter mSavedVideoAdapter;
ListView listView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.saved_video_fragment_layout,container,false);
getSavedVideo();
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
return rootview;
}
public void getSavedVideo() {
mydb = new MySQLiteHelper(getActivity());
ArrayList savedVideoIDs = mydb.getAllSavedVideo();
mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);
listView.setAdapter(mSavedVideoAdapter);
}
}
Adapter:
public class SavedVideoAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Object> mData = new ArrayList<Object>();
MySQLiteHelper mydb;
private LayoutInflater mInflater;
public SavedVideoAdapter(Context context, ArrayList<Object> data, MySQLiteHelper database) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mydb = database;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
Object obj = mData.get(position);
return "";
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Cursor res = res = mydb.getData((Integer)mData.get(position));
convertView = mInflater.inflate(R.layout.saved_video_adapter, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
TextView activity = (TextView) convertView.findViewById(R.id.activity);
TextView activityDate = (TextView) convertView.findViewById(R.id.activityDate);
ImageView cancel = (ImageView) convertView.findViewById(R.id.x);
activity.setText(res.getColumnIndex("title"));
return convertView;
}
}
ListView 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="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/savedVideoListView"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Adapter Layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/thumbnail">
<TextView
android:id="#+id/activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
<TextView
android:id="#+id/activityDate"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="#+id/activity"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="fill_parent"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/x"
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="#drawable/x" />
</RelativeLayout>
</RelativeLayout>
AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException
Because listView is not initialized.
Call getSavedVideo() method after initializing listView :
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
getSavedVideo();
I tried to use a ListView with a BaseAdapter. I had a java.lang.NullPointerException on this line:
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
Where I'm doing wrong?
This is my main activity:
public class ListaUtenti extends Activity {
private ListView listView;
private ListAdapter adapter;
private List<Persona> persona = new LinkedList<Persona>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_utenti);
listView = (ListView)findViewById(R.id.listaUtenti);
adapter = new BaseAdapter() {
#Override
public int getCount() {
return persona.size();
}
#Override
public Persona getItem(int position) {
return persona.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.list_item_persona, null);
}
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
final Persona p = getItem(position);
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
descrizione.setText(getResources().getString(R.string.descrizione, p.cognome));
return view;
}
};
listView.setAdapter(adapter);
}
#Override
protected void onStart(){
super.onStart();
persona.clear();
persona.add(new Persona("nome1","cognome1"));
persona.add(new Persona("nome2","cognome2"));
persona.add(new Persona("nome3","cognome3"));
listView.setAdapter(adapter);
}
}
This is the ListView in the xml file activity_lista_utenti:
<ListView
android:id="#+id/listaUtenti"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:listitem="#android:layout/simple_list_item_1" />
This is the xml file list_item_persona:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="5dp"
android:src="#drawable/ic_contact_picture"
android:contentDescription="#string/immagine_profilo" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:background="#color/yellow2"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/icon"
android:layout_toEndOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="20sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:background="#color/yellow"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toRightOf="#id/icon"
android:layout_toEndOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="15sp" />
</RelativeLayout>
Get textview from view's findViewById,i.e.
Change
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
to
final TextView titolo = (TextView)view.findViewById(R.id.firstLine);
final TextView descrizione = (TextView)view.findViewById(R.id.secondLine);
Since they are part of list_item_persona view.
I'm trying to create an android list using a custom adapter and the following XML row layout. I have implemented the following classes however when I run it onto my emulator the "android:id="#+id/firstLine" TextView does not appear to be setting the text! The "id/secondLine" Text View does receive the correct text!
List Row Item XML Layout:
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="#string/TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="#string/purchase_date"
android:textSize="12sp" />
/// THIS IS THE TextView that's not working
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="#string/home_depot"
android:textSize="16sp" />
</RelativeLayout>
Adapter Class:
public class SimpleAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
//public ImageLoader imageLoader;
public SimpleAdapter(Activity a, ArrayList<HashMap<String, String>> d){
activity = a;
data = d;
inflater = (LayoutInflater)activity.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 vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
TextView purchaseDate = (TextView)vi.findViewById(R.id.secondLine);
TextView seller = (TextView)vi.findViewById(R.id.firstLine);
HashMap<String, String> employee = new HashMap<String, String>();
employee = data.get(position);
// Should this not be setting the text for the "seller" TextView?
purchaseDate.setText(employee.get(HistoryListView.KEY_PURCHASE_DATE));
seller.setText(employee.get(HistoryListView.KEY_SELLER));
return vi;
}
}
List Activity:
public class HistoryListView extends Activity {
static final String KEY_SELLER="seller";
static final String KEY_PURCHASE_DATE="purchase_date";
static final String KEY_SALE_AMMOUNT="sale_ammount";
static final String PURCHASE_DATE="Purchase Date: ";
static final String USD="$ ";
ListView list;
SimpleAdapter adapter;
public String[] sellerList = {"Home Depot","7 Eleven","Costco","Outback Steak House","Philz Coffee"};
public String[] purchaseDateList={"1/12/13", "1/23/13", "2/16/13", "2/17/13", "2/18/13"};
public String[] saleAmmountList={"1023.10","243.98","107.54","45.88","21.99"};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.history_list);
ArrayList<HashMap<String,String>> expenseList=new ArrayList<HashMap<String, String>>();
Intent intent = getIntent();
for(int i=0; i<5; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_SELLER, sellerList[i]);
map.put(KEY_PURCHASE_DATE, PURCHASE_DATE+purchaseDateList[i]);
//map.put(KEY_SALE_AMMOUNT, saleAmmountList[i]);
expenseList.add(map);
}
ListView list=(ListView)findViewById(R.id.list);
adapter=new SimpleAdapter(this, expenseList);
list.setAdapter(adapter);
}//END onCreate
}//END HistoryListView
You have problem in List Row Item try this one
tested and it show icon and two lines
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/firstLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/icon"
android:text="TextView" />
<TextView
android:id="#+id/secondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/firstLine"
android:layout_below="#+id/firstLine"
android:text="TextView" />
</RelativeLayout>
try this one..
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:maxLines="5"// to specify how many lines its occupied or else reduce your size of textsize
android:singleLine="false"
android:ellipsize="end"
android:text="#string/home_depot"
android:textSize="16sp" />
thank you..