**Adapter class**
public class checklist extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> name;
public checklist(Activity context,ArrayList<String> name) {
super(context, R.layout.activity_custm_list, name);
this.context = context;
this.name = name;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.activity_custm_list, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.check);
txtTitle.setText(name.get(position));
return rowView;
}
}
adapter_xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<CheckBox
android:id="#+id/check"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
listview_actitvty
public class Delete extends ActionBarActivity {
ListView lv ;
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> ids = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
lv = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
name = intent.getStringArrayListExtra("note name");
ids = intent.getIntegerArrayListExtra("id");
System.out.println("-------*********----------------"+name);
// checklist dapter = new checklist(Delete.this,name);
checklist adapter = new checklist(Delete.this, name);
lv.setAdapter(adapter);
}
activity_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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.mohamedreda.note.Delete">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</RelativeLayout>
this output of system.out.print
05-11 05:19:03.072 5464-5464/com.example.mohamedreda.note I/System.out﹕ -------*********----------------[alissaas, mes, hhhhhhhhh, meshoo, zeko, mostafaaaa, ahmed, hi, ahmedddd, kattttb, xxxxxxxxxxx, sssssssssssssss, -----------------------------------, a44444, n, no, mes2, zeko5, ahmed7]
error
*
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setTag(java.lang.Object)' on a null object reference
at com.example.mohamedreda.note.checklist.getView(checklist.java:32)*
you can : ""+name.get(position);
became : txtTitle.setText(""+name.get(position));
Related
I try to make a list view with content from realm, but problem is that nothing shows when I open activity in emulator, just white screen.
Code is
public class ListActivity extends AppCompatActivity {
ListView list;
Realm realm;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_items);
realm = Realm.getDefaultInstance();
RealmResults<ArticleList> results = realm.where(ArticleList.class).equalTo("category", "Танковые сражения").findAll();
ListActivityListAdapter adapter = new ListActivityListAdapter(this, results);
list = (ListView) findViewById(R.id.listactivity_listview);
list.setAdapter(adapter);
button = (Button) findViewById(R.id.button);
}
}
public class ListActivityListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final RealmResults<ArticleList> articleList;
public ListActivityListAdapter(Activity context, RealmResults<ArticleList> articleList) {
super(context, R.layout.listview_listactivity_item);
this.context = context;
this.articleList = articleList;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.listview_listactivity_item, null, true);
TextView title = (TextView) rowView.findViewById(R.id.listview_item_title);
TextView subtitle = (TextView) rowView.findViewById(R.id.listview_item_subtitle);
ImageView image = (ImageView) rowView.findViewById(R.id.listview_item_imageView);
ArticleList item = articleList.get(position);
title.setText(item.getTitle());
subtitle.setText(item.getSubtitle());
String imageName = item.getImage();
File imageFile = ImageStorage.getImage(imageName, getContext());
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeFile(context.getFilesDir() + "/tanks/" + imageName + ".jpg");
} catch (Exception e) {
e.printStackTrace();
}
image.setImageBitmap(bitmap);
return rowView;
}
}
activity_list_of_items.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.me.demo2.ListActivity"
tools:showIn="#layout/listview_main_list">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listactivity_listview"
android:layout_gravity="center_horizontal|top"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="164dp" />
listview_listactivity_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="213dp"
android:id="#+id/listview_item_imageView"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listview_item_title"
android:paddingTop="100dp"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listview_item_subtitle"
android:paddingTop="150dp"
android:layout_gravity="center_horizontal|top" />
What did I do wrong ?
I think you're missing the getCount() method in your ArrayAdapter. Also, dont forget about #Override annotation for all the method you're overriding.
Try adapter.notifyDataChanged() after list.setAdapter(adapter);
I have the following code:
public class MainActivity extends Activity {
private ListView list;
private static final String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "BOE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
String books[] = {"Mercedes", "Skoda", "Volkswagen", "Audi", "Citroen", "plik"};
BookArrayAdapter bma = new BookArrayAdapter(this, books);
bma.setNotifyOnChange(true);
list.setAdapter(bma);
if (!new File(path).exists()) {
new File(path).mkdirs();
}
for (int i=0; i<books.length; i++) {
View v = list.getAdapter().getView(i, null, null);
String name = ((TextView)v.findViewById(R.id.row_text)).getText() + "";
for (File f:new File(path).listFiles()) {
if (f.isFile()) {
Button b = (Button) v.findViewById(R.id.row_button);
if (name.equals(f.getName().substring(0, f.getName().length()-4))) {
b.setText("Open");
} else {
b.setText("Download");
}
bma.notifyDataSetInvalidated();
bma.notifyDataSetChanged();
Log.d("AC", b.getText().toString());
}
}
}
}
public class BookArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public BookArrayAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(values[position]);
rowView.findViewById(R.id.row_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((Button)v).getText().toString().equals("Open")) {
//open
} else {
//Download
}
}
});
return rowView;
}
}
}
and I have problems in for loop in onCreate. this Log shows correct Download/Open texts depenging on Files in file system. But the graphical representation of this list view does not changing. Here's my layout.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="${relativePackage}.${activityClass}" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
<RelativeLayout
android:id="#+id/main_linear_blackout"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"
android:background="#CC000000" >
<ProgressBar
android:id="#+id/main_progress_startup"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
and my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/row_button"
android:orientation="vertical" >
<TextView
android:id="#+id/row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="#+id/row_progress"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="invisible" />
</LinearLayout>
<Button
android:id="#+id/row_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/row_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row_button"
android:layout_alignBottom="#+id/row_button"
android:layout_alignLeft="#+id/row_button"
android:layout_alignRight="#+id/row_button"
android:layout_alignTop="#+id/row_button"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible" />
</RelativeLayout>
how to update it? What is wrong?
EDIT:
I moved editing single row to getView(...) overrided method on my adapter class. I don't really know why my first idea was wrong, but now it works.
I want to know why it tells me: "Unfortunately your app has stopped"?
I followed all the steps in this video:
https://www.youtube.com/watch?v=0zQCv0Xb3pk&index=84&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa
my code in main Activity
public class MainActivity extends Activity {
ListView list;
String[] mimititles;
String[] description;
int[] images = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m4,
R.drawable.m5,R.drawable.m6,R.drawable.m7,R.drawable.m8,R.drawable.m9,R.drawable.m10};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res= getResources();
mimititles= res.getStringArray(R.array.title);
description = res.getStringArray(R.array.description);
list=(ListView)findViewById(R.id.listView);
ibrahimadapter adapter = new ibrahimadapter(this,mimititles,images,description);
list.setAdapter(adapter);
}
}
class ibrahimadapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String [] tiltearray;
String [] descrip;
ibrahimadapter(Context c,String[]titles,int[]imgs,String[] desc )
{
super(c,R.layout.single_row,R.id.textView1,titles);
this.context=c;
this.images=imgs;
this.tiltearray=titles;
this.descrip=desc;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View raw=inflate.inflate(R.layout.single_row,parent,false);
ImageView myimage= (ImageView) raw.findViewById(R.id.imageView1);
myimage.setImageResource(images[position]);
TextView mytitle = (TextView) raw.findViewById(R.id.textView1);
mytitle.setText(tiltearray[position]);
TextView mydescription = (TextView) raw.findViewById(R.id.textView2);
mydescription.setText(descrip[position]);
return raw;
}
}
my code in activity_main.xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
xmlns:android2="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android2:id="#+id/listView"
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
my code in single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="#drawable/m1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_toRightOf="#+id/imageView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
In your adapter:
your constructor should be: public ibrahimadapter(....) {.....}
getView function change to:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Holder holder;
View raw = converView;
if(raw == null){
raw=inflate.inflate(R.layout.single_row,parent,false);
holder = new Holder();
holder.imgView = (ImageView) raw.findViewById(R.id.imageView1);
holder.myTitle = (TextView) raw.findViewById(R.id.textView1);
holder.myDes = (TextView) raw.findViewById(R.id.textView2);
raw.setTag(holder)
} else {
holder=(Holder)raw.getTag();
}
holder.imgView.setImageResource(images[position]);
holder.myTitle.setText(tiltearray[position]);
holder.myDes.setText(descrip[position]);
return raw;
}
public static class Holder {
ImageView imgView;
TextView myTitle;
TextView myDes;
}
Try this:
Change this
super(c,R.layout.single_row,R.id.textView1,titles)
To this
super(c,R.layout.single_row,titles)
Please check that the number of items for each String[] mimititles, String[] description and int[] images are all of the same length.
E.g if you want to show 5 item in the List, then you must 5 images, 5 mimititles and 5 description.
So check your string-array files
I've Created a List view with ArrayAdapter and when i run it , its showing me an empty activity in the runtime and i don't know the wrong:
here is the MainActivity and the ArrayAdapter Classes
public class MainActivity extends Activity {
String [] title ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
title = getResources().getStringArray(R.array.titles);
list.setAdapter(new MyAdapter(MainActivity.this, title));
}
}
class MyAdapter extends ArrayAdapter{
String [] title;
Context context;
public MyAdapter(Context context, String[] title) {
super(context,R.layout.single_row,R.id.img);
this.title = title;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
ImageView iv;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, parent, false);
}
tv = (TextView) row.findViewById(R.id.title);
tv.setText(title[position]);
iv = (ImageView) row.findViewById(R.id.img);
iv.setImageResource(R.drawable.arrow);
return row;
}
}
activity_main.xml that contain the ListView
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
single_row.xml that contains the elements of the listview
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img"
android:src="#drawable/arrow"
android:layout_margin="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="#dimen/abc_action_bar_stacked_max_height"
android:id="#+id/title"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
Change this line of code in your adapter.
LayoutInflater inflater = LayoutInflater.from(context);
I am trying to get the positions of the views from my listview but the size of the SparseBooleanArray is always 0.
This is the code from my activity.
IngredientIndivAdapter adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_screen);
Intent intent = getIntent();
String prev_but = intent.getStringExtra("hi");
ArrayList<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
list1.add("e");
adapter = new IngredientIndivAdapter(this,list1);
lv = (ListView)findViewById(R.id.listview1);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
public void remove_ingredient_button(View v){
SparseBooleanArray remove_list = lv.getCheckedItemPositions();
Log.d("poo",""+remove_list.size());
if(remove_list!=null)
for(int i = 0; i < remove_list.size(); i++){
if(remove_list.valueAt(i)){
View remove_view = (View)findViewById(remove_list.keyAt(i));
((LinearLayout)remove_view.getParent()).removeView(remove_view);
}
}
adapter.notifyDataSetChanged();
}
This is the XML for that activity
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.JTA.recipeshoppinglist.ListScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<Button
android:id="#+id/add_b"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FFFF9900"
android:text="+"
android:onClick="add_button" />
<Button
android:id="#+id/remove_item"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="-"
android:onClick="remove_ingredient_button" />
This is my custom row 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" >
<CheckBox
android:id="#+id/ingredient_check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkable="true"
android:background="#FF0000" />
</LinearLayout>
And this is my custom array adapter.
public class IngredientIndivAdapter extends ArrayAdapter<String> {
static class ViewHolder{
CheckBox text;
}
ArrayList<String> list = new ArrayList<String>();
Context context;
ViewHolder viewHolder;
public IngredientIndivAdapter(Context context, ArrayList<String> iList){
super(context,R.layout.ingredient_row,iList);
this.list=iList;
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.ingredient_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (CheckBox) convertView.findViewById(R.id.ingredient_check);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.text.setText(list.get(position));
return convertView;
}
public boolean hasStableIds(){
return true;
}
}
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < listview.getCount(); i++){
if(checked.get(i)==true) {
// do something
}
}
I hope it will work.