Custom ListView doen't show - android

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.

Related

Having issue with designing comment section

I am trying to make Popup Dialog Comment Section.
In this Comment section i done almost all things but facing new problem.
Here i am using Dialog for Show and add new comment but when user click on Enter a Comment (Edittext). It gets hide under Keyboard. So it will be problematic for user to post new comment.
Here My Xml File :
Popup.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="wrap_content"
android:background="#drawable/dialog_bg"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/popup_rcv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<EditText
android:id="#+id/new_comment_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Please enter Comment" />
<Button
android:id="#+id/comment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thank you in Advance.
Try this
dialog_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/popup_rcv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="#+id/new_comment_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Please enter Comment" />
<Button
android:id="#+id/comment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
Activity
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class HomeActivity extends AppCompatActivity {
RecyclerView additionalDataList;
ArrayList<String> arrayList = new ArrayList<>();
DataAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_layout);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
EditText edtComment = dialog.findViewById(R.id.new_comment_et);
Button btnComment = dialog.findViewById(R.id.comment_btn);
additionalDataList = dialog.findViewById(R.id.popup_rcv);
additionalDataList.setLayoutManager(new LinearLayoutManager(this));
additionalDataList.setHasFixedSize(true);
addDataToList();
btnComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!TextUtils.isEmpty(edtComment.getText().toString().trim())) {
arrayList.add(0, edtComment.getText().toString().trim());
edtComment.setText("");
adapter.notifyDataSetChanged();
}
}
});
adapter = new DataAdapter(this, arrayList);
additionalDataList.setAdapter(adapter);
dialog.show();
}
private void addDataToList() {
for (int i = 0; i < 5; i++) {
arrayList.add("Comment :" + i);
}
}
}
DataAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
Context context;
ArrayList<String> arrayList = new ArrayList<>();
public DataAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(arrayList.get(position));
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.additionalInfoTitle);
}
}
}
OUTPUT

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.

increasing the height of imageView in gridView android

I am using GridView first time. Everything is fine but the image height is less. I searched a lot but couldn't get the method of increasing the height of image through XML. Here is my code :
activity_select_images.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fillViewport="false">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectImagesActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:text="Please Select the pictures you like"
android:id="#+id/selectTextview"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_below="#+id/selectTextview"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<Button
android:id="#+id/submit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="18dp"
android:textAllCaps="false"
android:textColor="#ffffff"
android:layout_below="#+id/frameLayout"
android:background="#drawable/my_button"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</RelativeLayout>
</ScrollView>
gridview_layout.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="fill_parent">
<ImageView android:id="#+id/thumbImage"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbImage"
android:layout_alignLeft="#+id/thumbImage"
android:layout_alignStart="#+id/thumbImage"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp"
android:layout_marginBottom="61dp" />
</RelativeLayout>
SelectImagesActivity.java
package com.houssup.userapp;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class SelectImagesActivity extends AppCompatActivity {
private int count = 0;
Button btn;
private Drawable marker, marker1;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_images);
GridView imagegrid = (GridView) findViewById(R.id.gridView);
btn = (Button) findViewById(R.id.submit);
marker = getResources().getDrawable(R.drawable.pic_five);
marker1 = getResources().getDrawable(R.drawable.pic_two);
imagegrid.setAdapter(new ImageAdapter(this, marker, marker1));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (count < 5)
Toast.makeText(SelectImagesActivity.this, "Please " +
"select total 5 images", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private Drawable drawable1, drawable2;
private LayoutInflater mInflater;
int positionID[];
public ImageAdapter(Context context, Drawable drawable1,
Drawable drawable2) {
this.context = context;
this.drawable1 = drawable1;
this.drawable2 = drawable2;
}
public int getCount() {
return 6;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.gridview_layout, null);
ImageView imageView = (ImageView) gridView.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) gridView.findViewById(R.id.itemCheckBox);
if (position < 5) {
imageView.setImageDrawable(marker);
} else {
imageView.setImageDrawable(marker1);
}
// set image based on selected text
} else {
gridView = (View) convertView;
}
return gridView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}

Trouble with fragments and adapter

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);

listview item is not added on button click

I have an issue with adapter adding data to listview on button click.
I have a activity called createteam that has name and email edittext and a createplayer button. On click createplayer button, the name and email should be added to list view.
I want to use an adapter for doing that(so that I can learn).
Update:
Onclicking the button the the first entry gets added but when I fill in the
edittexts again, the listview does get updated. I am notifying the adapter when I update
the data but only first entry gets and remains added to list view
create_team.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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/teamname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Team Name"
android:gravity="center"
android:textStyle="bold">
</EditText>
<Button
android:id="#+id/addplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:text="Add Player"
android:layout_gravity="center"
android:textStyle="bold">
</Button>
<LinearLayout
android:id="#+id/view_addplayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/createplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/red"
android:text="Create Player"
android:layout_gravity="center"
android:textStyle="bold">
</Button>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Name"
android:gravity="center"
android:textStyle="bold">
</EditText>
<EditText
android:id="#+id/email"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Email "
android:textStyle="bold"
android:gravity="center">
</EditText>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</ScrollView>
add_player_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView android:id="#+id/name"
android:textSize="20sp"
android:textStyle="bold"
android:color="#color/blue"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
<TextView android:id="#+id/email"
android:textSize="15sp"
android:textStyle="bold"
android:color="#color/mustard"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
CreateTeamActivity.java
package com.recscores.android;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.Toast;
import com.recscores.android.PlayerInfo;
public class CreateTeamActivity extends ListActivity {
Button createPlayer;
EditText Email;
EditText Name;
private ArrayList<PlayerInfo> newList;
private PlayerAddAdapter newAdpt;
private int i = 0;
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_team);
newList = new ArrayList<PlayerInfo>();
// CREATE TEAM
getWindow().setBackgroundDrawableResource(R.drawable.app_background2);
createPlayer = (Button)findViewById(R.id.createplayer);
Email = (EditText)findViewById(R.id.email);
Name = (EditText)findViewById(R.id.name);
lv = (ListView)findViewById(android.R.id.list);
newAdpt = new PlayerAddAdapter(CreateTeamActivity.this,android.R.id.list,newList);
lv.setAdapter(newAdpt);
createPlayer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// newList = new ArrayList<PlayerInfo>();
PlayerInfo info = new PlayerInfo();
info.SetName(Name.getText().toString());
info.SetEmail(Email.getText().toString());
newList.add(info);
newAdpt.notifyDataSetChanged();
//Thread.sleep(2000);
Name.setText("");
Email.setText("");
}
});
}
}
PlayerAddAdapter.java
package com.recscores.android;
import java.util.ArrayList;
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;
public class PlayerAddAdapter extends ArrayAdapter<PlayerInfo> {
private Activity activity;
//private final Context mContext;
private ArrayList<PlayerInfo> items;
public PlayerAddAdapter(Activity a, int textViewResourceId, ArrayList<PlayerInfo> items){
super(a,textViewResourceId,items);
this.items=items;
this.activity = a;
}
public static class ViewHolder{
public TextView name;
public TextView email;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi.inflate(R.layout.add_player_listview, null);
holder = new ViewHolder();
holder.name = (TextView)v.findViewById(R.id.name);
holder.email = (TextView)v.findViewById(R.id.email);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final PlayerInfo custom = items.get(position);
if (custom != null) {
**holder.name.setText(custom.GetName());
holder.email.setText(custom.GetEmail());
}
return v;
}
}
PlayerInfo.java
package com.recscores.android;
public class PlayerInfo {
private String name="";
private String email="";
public void SetName(String name){
this.name = name;
}
public String GetName(){
return this.name;
}
public void SetEmail(String email){
this.email = email;
}
public String GetEmail(){
return this.email;
}
}
LayoutInflater vi =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.add_player_listview, null);
Change your code like this. You are not setting values to v variable.
onClick of your button call list.notifyDataSetChanged(). It will refresh your list.
Incase if doesn't work just check getCount() of your adapter what value it is returning. If it is returning 0 your list will not show any data even you call list.notifyDataSetChanged().
Make an arrayList in your adapter and add new items into that. and from getCount() return size of arrayList.

Categories

Resources