Display Data in GridView with DataAdapter in Android - java

I have try the following code for displaying data from data adapter in gridview.
Following link is for GridView with DataAdapter Tutorial.
I am wanted the following output :
I have follow the tutorial and get the output as i wanted, but unfortunetly when scroll the page,
application will be closed automatically.
ERROR : "Application Failure Detected, Please Try Again"
How to resolve these problem?
code : main.xml file.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip" />
code : MainActivity.java file.
package com.example.gridviewdata;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new DataAdapter(this));
}
code : customgrid.xml file
<?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="fill_parent"
android:orientation="vertical">
<TableLayout android:id="#+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow android:id="#+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtId"
android:layout_gravity="center_horizontal" />
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
code : DataAdapter.java file
package com.example.gridviewdata;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DataAdapter extends BaseAdapter {
Context mContext;
private String [] id = {"S001","S002","S003","S004","S005","S006","S007","S008","S009","S010","S011","S012"};
private String [] name={"Rohit","Rahul","Ravi","Amit","Arun","Anil","Kashif","Nayan","Jay","Sagar","Jairaj","Vishal"};
private LayoutInflater mInflater;
public DataAdapter(Context c)
{
mContext=c;
mInflater = LayoutInflater.from(c);
}
public int getCount()
{
return id.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder=null;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.customgrid,parent,false);
holder = new ViewHolder();
holder.txtId=(TextView)convertView.findViewById(R.id.txtId);
holder.txtId.setPadding(100, 10,10 , 10);
holder.txtName=(TextView)convertView.findViewById(R.id.txtName);
holder.txtName.setPadding(100, 10, 10, 10);
if(position==0)
{
convertView.setTag(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtId.setText(id[position]);
holder.txtName.setText(name[position]);
return convertView;
}
static class ViewHolder
{
TextView txtId;
TextView txtName;
}
}
Give any solution for these problem.
if any other resource is available then give me an reference.

Solution :
In getView() method return all items, and i have setting the tag just in the first item,
when android calls again getView for position 1, i am try to get the tag from convertView,
but i didn't called setTag for convertView 1, then getTag returns a null object, and i am try to get the object txtName from a null object, and then the error occurs.
removing the if(position==0) then application work perfectly and give the scrolling.

Related

Why do my Custom GridView show only one column?

I'v tried making a Custom GridView that adds proggramatically TextView items.
The TextViews needs to act as an ImageView only that it has a text within it.
But for some odd reason it shows me only 1 column out of 4 (but if its too big then 3 is my minimum capcity).
I look through similar cases for people who attempted the same thing, but all the same as it ended failing.
All I get is a one column Gridview each time.
I'd like to hear some opinions about it, in fix this issue.
My GridView:
<GridView
android:id="#+id/gridView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="73dp"
android:layout_toEndOf="#+id/imageView15"
android:columnCount="4"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth" />
My item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:backgroundTint="#android:color/black"
android:alpha="100"
android:layoutDirection="ltr">
<TextView
android:id="#+id/item"
android:layout_width="150px"
android:layout_height="150px"
android:layout_marginRight="50px"
android:textColor="#android:color/black"
android:textSize="10sp" />
</LinearLayout>
And finally the Adapter:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CustomListAdapter extends BaseAdapter {
private Activity context;
private List<TextView> items;
private static LayoutInflater inflater;
public CustomListAdapter(Activity context, List<TextView> items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.inventory_item, null);
Holder holder = new Holder();
holder.tv = (TextView) rowView.findViewById(R.id.item);
holder.tv.setText(items.get(position).getText());
holder.tv.setBackground(items.get(position).getBackground());
holder.tv.setTextColor(items.get(position).getTextColors());
return rowView;
}
}
Thanks in advance.
shows me only 1 column out of 4
you need to use: android:numColumns="4"
in your code:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="#color/color_white"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none|horizontal"
android:stretchMode="columnWidth"></GridView>

Get access to other view in fragment

I have a problem with setting the visibility of an TextView.
I have two xmls. One for the fragment itself and the other for the RecyclerView.
In the fragment_settings.xml I'm setting the RecyclerView. In the settings_list_row.xml I define the RecyclerView row.
When I create a new RecyclerView row I sometimes have a row where twice of the TextView's are filled and sometimes just one. Because of this I want to set the visibility of one TextView to gone if it's empty.
Because my View is the fragment_settings.xml I don't have access to the row view. Is there a solution for this? Thanks a lot!
fragment_settings.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.myfitindustry.myfitindustry.app.SettingsFragment">
<TextView
android:id="#+id/accountTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:text="#string/accountTitle" />
<android.support.v7.widget.RecyclerView
android:id="#+id/settings_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
settings_list_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:focusable="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/settingTitle"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:textSize="16dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/settingSubtitle"
android:layout_below="#id/settingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Fragments onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.settings_recycler_view);
sAdapter = new SettingsAdapter(settingList);
RecyclerView.LayoutManager sLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()) {
// Disable scrolling in the RecyclerView
#Override
public boolean canScrollVertically() {
return false;
}
};
recyclerView.setLayoutManager(sLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
prepareSettingsData();
recyclerView.setAdapter(sAdapter);
// HERE I WANT TO CHANGE THE VISIBILITY
TextView settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
if(!settingSubtitle.getText().equals("")) {
settingSubtitle.setVisibility(TextView.GONE);
}
return view;
}
My RecyclerView Adapter:
package de.myfirstapp.app;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
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.List;
/**
* Created by Johannes on 19.01.2017.
*/
public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.MySettingHolder> {
private List<Settings> settingList;
public class MySettingHolder extends RecyclerView.ViewHolder {
public TextView settingTitle, settingSubtitle;
public MySettingHolder(View view) {
super(view);
settingTitle = (TextView) view.findViewById(R.id.settingTitle);
settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
}
}
public SettingsAdapter (List<Settings> settingList) {
this.settingList = settingList;
}
#Override
public MySettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.settings_list_row, parent, false);
return new MySettingHolder(itemView);
}
#Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
#Override
public int getItemCount() {
return settingList.size();
}
}
Why you don't do it in SettingsAdapter onBindViewHolder method? Imagine, you have bunch of items in your RecyclerView and bunch of settingSubtitle, how system will understand which TextView do you really want?
Update
#Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
if (setting.getSettingSubtitle().equals("")) {
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(View.GONE);
} else {
holder.settingSubtitle.setVisibility(View.VISIBLE);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
}

ListView with Custom extended BaseAdapter not showing with SherlockFragment, need hints or suggestions

I am new to android programming and I seem to have come at a stand still for several days now. I am having trouble finding a solution to my problem and tried many different solutions without success. As the title suggests, my code runs successfully but the ListView does not show up on the selected Tabs. Any suggestions of tips would be helpful.
ItemGuide.Java ------------------------------------
package com.example.alzuni_project;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class ItemGuide extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setIcon(R.drawable.leathertab_image), LeatherTab.class, null);
mTabsAdapter.addTab(bar.newTab().setIcon(R.drawable.leathertab_image), SilverTab.class, null);
}
}
LeatherTab.java --------------------------------------------------------------
package com.example.alzuni_project;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
public class LeatherTab extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.leather_fragment, container, false); //Fragment Layout inflated
TextView text = (TextView) view.findViewById(R.id.boxtest);//TextView for layout testing
text.setText("Hello");
ListView leather_listview = (ListView) view.findViewById(R.id.leather_list); // List is initialized
leather_listview.setAdapter(new LeatherAdapter(getActivity())); //Custom list adapter passes Context
return view;
}
}
LeatherAdapter.java ------------------------------------------------------
package com.example.alzuni_project;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
class LeatherAdapter extends BaseAdapter {
ArrayList<SingleRow> list;
Context context;
public LeatherAdapter(Context c) {
context = c;
list = new ArrayList<SingleRow>();
Resources res = c.getResources();
String[] titles = res.getStringArray(R.array.leather_list_titles);
String[] descriptions = res.getStringArray(R.array.leather_list_description);
int[] images = {R.drawable.belt, R.drawable.wallet, R.drawable.coincase};
for (int i=0;i<images.length;i++) //Was originally 3
{
new SingleRow(titles[i], descriptions[i], images[i]);
}
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewGroup, false);
TextView title = (TextView) row.findViewById(R.id.leather_title);
TextView description = (TextView) row.findViewById(R.id.leather_description);
ImageView image = (ImageView) row.findViewById(R.id.leather_icon);
SingleRow temp = list.get(position);
title.setText(temp.title);
description.setText(temp.description);
image.setImageResource(temp.image);
return row;//returns the rootView of single_row.xml
}
}
leatherfragment.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"
android:orientation="vertical" >
<TextView
android:id="#+id/boxtest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCDDFF" />
<ListView
android:id="#+id/leather_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/boxtest" />
</RelativeLayout>
SingleRow.java -----------------------------------------------------
package com.example.alzuni_project;
class SingleRow {
String title;
String description;
int image;
SingleRow(String title, String description, int image) {
this.title=title;
this.description=description;
this.image=image;
}
}
single_row.xml ---------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/leather_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="48dp"
android:contentDescription="#string/todo" />
<TextView
android:id="#+id/leather_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/leather_icon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#CCCCCC" />
<TextView
android:id="#+id/leather_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/leather_icon"
android:layout_alignLeft="#+id/leather_icon"
android:layout_alignParentRight="true"
android:layout_below="#+id/leather_title"
android:background="#CCDDFF" />
Your list seems empty , try this in your LeatherAdapter:
list.add(new SingleRow(titles[i],descriptions[i], images[i]));
Inside the for loop .

My Custom ListView display nothing

I am newbie in android. I want to create a custom listview arrayadapter. I have followed some tutorial but my emulator shows nothing for the Custom ListView. Can anyone help me figure out where is wrong with my code? Thanks in advance.
Here is my custom_listview_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customListView" />
</LinearLayout>
This is my custom_listview_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"
android:gravity="center_vertical"
android:minHeight="64dp">
<!-- <ImageView
android:id="#+id/clv_imageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:contentDescription="#string/empty"
android:layout_alignParentLeft="true"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"/> -->
<TextView
android:id="#+id/clv_textView2"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="#string/tv_definition"
android:textIsSelectable="true" />
<TextView
android:id="#+id/clv_textView"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="#string/tv_word"
android:textIsSelectable="true" />
</RelativeLayout>
Here is my MyPerformanceArrayAdapter.java
import java.util.List;
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 MyPerformanceArrayAdapter extends ArrayAdapter<DefinitionObject>{
private List<DefinitionObject> entries;
private Activity activity;
public MyPerformanceArrayAdapter(Activity a, int textViewResourceId, List<DefinitionObject> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
#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);
v = vi.inflate(R.layout.custom_listview_row, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.clv_textView);
holder.item2 = (TextView) v.findViewById(R.id.clv_textView2);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final DefinitionObject custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getWord());
holder.item2.setText(custom.getFav());
}
return v;
}
}
and lastly, this is my Activity named TempCLV.java
package com.example.myidictionary;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
public class TempCLV extends Activity {
private MySQLiteDefinitionHelper db;
String tblName = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview_main);
Intent msjIntent = getIntent();
tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
}
public void refresh()
{
db = new MySQLiteDefinitionHelper(this);
final List<DefinitionObject> values = db.getAllWords(tblName);
ListView mylist = (ListView)findViewById(R.id.customListView);
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, R.id.customListView, values);
mylist.setAdapter(adapter);
}
}
You need to call refresh in onCreate since you are setting the adapter to list view there.
Make sure your values has some data
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview_main);
Intent msjIntent = getIntent();
tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
refresh();
}
Also you can move the inflater initialization to the constructor of adapter class. No need to initialize in getView
LayoutInflater vi;
public MyPerformanceArrayAdapter(Activity a, int textViewResourceId,List<DefinitionObject> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
vi =(LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Please call refresh method at least for first time to set the the adapter in your MainActivity' onCreate method..

Show an Image and Text in a ListView

I'm developing an App, that shows a ListView with 30 items.
In each row, there should be a text and an Image.
I searches for different tutorials, but unfortunately all don't work for me.
So I'm asking you to have a short look to my code and tell me what's wrong in there, because when I start the app, the ListView is empty.
mainActivity.java:
setContentView(R.layout.auswertung);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new MyListAdapter(this, fragen));
MyListAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<Frage>{
private final Context context;
private ArrayList<Frage> fragen;
public MyListAdapter(Context context, ArrayList<Frage> f) {
super(context, R.layout.reihe);
this.context = context;
fragen = f;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.reihe, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.textView4);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img1);
textView.setText("Frage " + (position+1));
if (fragen.get(position).getRichtig()==true)
{
imageView.setImageResource(R.drawable.richtig);
}
else
{
imageView.setImageResource(R.drawable.falsch);
}
return rowView;
}
}
auswertung.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"
android:layout_weight="0.96"
android:background="#drawable/hintergrund"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="19dp"
android:text="Auswertung"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="460dp"
android:layout_marginTop="60dp">
</ListView>
</RelativeLayout>
reihe.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/textView4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="5"
android:textSize="16dp"
android:textColor="#000000" />
<ImageView
android:id="#+id/img1"
android:layout_width="25dp"
android:layout_height="25dp" />
</LinearLayout>
As you see, I have a background image, and would also ask you, how can I make the background of the ListView invisible, so that I can see my background behind the text and the Image?
you should pass the the ArrayAdapter constructour your Frage data set, or at least,
ovveride ArrayAdapter.getCount(), and let it returns the number of items in ArrayList<Frage> f
int getCount() {
return (f == null) ? 0 : f.size();
}
here the doc for getCount()

Categories