Button OnClickListener within ListView - java

I am having some trouble with my button listener in my listView. I have my custom adapter which is printing out my row (TextView..Button..Edittext..Button), Within this class I have set up my Listeners. What I am trying to do is when either one of the buttons is clicked they will increment or decrement the editText in the row. The trouble that I am having though is the onClickListener() and getLayoutInflater() are both coming up red. How do I fix this and is my code correct for what I am trying to do? Thanks
My custom adapter class
package com.example.rory.dbtest;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;
public abstract class CustomCursorAdapter extends CursorAdapter implements View.OnClickListener {
public int counter = 0;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row, parent, false);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0))));
}
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.row, null);
}
final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
Button plusButton = (Button) convertView.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new onClickListener()
{
public void onClick(View v)
{
counter++;
edit1.setText(Integer.toString(counter));
}
});
//final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
Button minusButton = (Button) convertView.findViewById(R.id.minusButton);
plusButton.setOnClickListener(new onClickListener()
{
public void onClick(View v)
{
counter--;
edit1.setText(Integer.toString(counter));
}
});
return convertView;
}
}
And if it is needed my XML file for each row
<?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/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingBottom="5dp"
android:hint="#string/hint"/>
<Button
android:id="#+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/plusButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/runningTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/hint"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/minusButton"
android:layout_toStartOf="#+id/minusButton"
android:layout_marginRight="30dp" />
<Button
android:id="#+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/minusButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

Instead of getLayoutInflater() use
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
you can get the context from the constructor:
Context context;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
And instead of onClickListener use View.onClickListener
You can remove the implements View.OnClickListener as well, you're not using it anyway.

Related

Why is my view changing color upon reloading?

I am developing an android app, and need to use a recyclerview for it. I want its background to be white, but for some reason when I run the app it turns out gray. That being said, if I reload the activity, or go back and then again to the activity, the color does turn white. Has anyone ever encountered such a problem?
This is how it looks when you first enter the app
And this is what happens if you reload the activity.
Here's my item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/trip"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#null"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/destination"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".333"
android:gravity="center"
android:textSize="24sp" />
<TextView
android:id="#+id/start_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:gravity="center"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="#+id/destination" />
<TextView
android:id="#+id/trip_length"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".333"
android:gravity="center" />
</LinearLayout>
and here is my main activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<ImageView
android:id="#+id/imageView3"
android:layout_width="180dp"
android:layout_height="180dp"
android:contentDescription="#string/app_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/app_logo" />
<TextView
android:id="#+id/main_name"
android:layout_width="359dp"
android:layout_height="26dp"
android:text="#string/welcome_message"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.865"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.178" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/trips"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:fadingEdge="horizontal|vertical"
android:maxHeight="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my adapter
package com.alonkh2.finalproject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.ViewHolder> {
private ArrayList<Trip> trips;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView destination, startDate, endDate, length;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
destination = (TextView) itemView.findViewById(R.id.destination);
startDate = (TextView) itemView.findViewById(R.id.start_date);
// endDate = (TextView) itemView.findViewById(R.id.end_date);
length = (TextView) itemView.findViewById(R.id.trip_length);
}
}
public TripAdapter(ArrayList<Trip> trips) {
this.trips = trips;
}
#NonNull
#Override
public TripAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.activity_listview, parent, false);
// Return a new holder instance
return new ViewHolder(contactView);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull TripAdapter.ViewHolder holder, int position) {
Trip trip = trips.get(position);
// Set item views based on your views and data model
TextView textView = holder.destination;
textView.setText(trip.getDestination());
textView = holder.startDate;
textView.setText(trip.getStrStartDate().substring(0, trip.getStrStartDate().indexOf(" ")));
// textView = holder.endDate;
// textView.setText(trip.getStrEndDate().substring(0, trip.getStrEndDate().indexOf(" ")));
textView = holder.length;
textView.setText(trip.getLength() + (trip.getLength() == 1 ? " day" : " days"));
}
#Override
public int getItemCount() {
return trips.size();
}
}
Put this code below to replace your TripAdapter code.
package com.alonkh2.finalproject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class TripAdapter extends RecyclerView.Adapter<TripAdapter.ViewHolder> {
Context context;
private ArrayList<Trip> trips;
public class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView destination, startDate, endDate, length;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
destination = (TextView) itemView.findViewById(R.id.destination);
startDate = (TextView) itemView.findViewById(R.id.start_date);
// endDate = (TextView) itemView.findViewById(R.id.end_date);
length = (TextView) itemView.findViewById(R.id.trip_length);
}
}
public TripAdapter(Context context, ArrayList<Trip> trips) {
this.context = context;
this.trips = trips;
}
#NonNull
#Override
public TripAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.activity_listview, parent, false);
// Return a new holder instance
return new ViewHolder(contactView);
}
#Override
public void onBindViewHolder(#NonNull TripAdapter.ViewHolder holder, int position) {
Trip trip = trips.get(position);
// Set item views based on your views and data model
holder.destination.setText(trip.getDestination());
holder.startDate.setText(trip.getStrStartDate().substring(0, trip.getStrStartDate().indexOf(" ")));
// holder.endDate.setText(trip.getStrEndDate().substring(0, trip.getStrEndDate().indexOf(" ")));
holder.length.setText(trip.getLength() + (trip.getLength() == 1 ? " day" : " days"));
}
#Override
public int getItemCount() {
return trips.size();
}
}

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>

Spinner Item leads to Activity (Android Studio)

I started programming a few month ago and stackoverflow always was a a good dite for solving my problems. So my codes are getting better but now I´m on a point that I Need your help again.
Programm: In my App you can choose items from a spinner, then it goes to next page and so on. You have to choose from several Spinners until you get a result...
Some Code preview (Code works, but i now have to ad some more Action and I don´t know how...
package com.sio.fmf;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Button;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;
public class Koerperform extends AppCompatActivity {
String[] koerperform = {" ", "spindel- oder torpedoförmig", "langgestreckt", "hochrückig", "schlangenförmig", "welsartig", "grundelartig"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.koerperform);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(new MyCustomAdapter(Koerperform.this,R.layout.spinner_layout, koerperform));
}
public void onClick(View v){}
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
private Button getSwtitchact;
{
final Button switchact = (Button) findViewById(R.id.button3);
switchact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent act = new Intent(view.getContext(), Maulstellung.class);
startActivity(act);
}
});
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_layout, parent, false);
TextView label = (TextView) row.findViewById(R.id.koerper);
label.setText(koerperform[position]);
if (position == 0) {
label.setTextColor(0xFFF00000);
}
return row;
}
}
}
So in this state of the app you can choose the string from the spinner and then if you press Botton 3 it changes to class Maulstellung
My Problem: I want that when string "a" is choosen it goes to page xy after Button 3 is pressed and when string "b" is choosen it goes to page xyz after Button 3 is pressed,..., and so on for each string...
Hope you can help me and sorry for my bad English
I will do in this way:
Not sure is this what you need.
Declare String selectedValue and Spinner mySpinner in global(add after line String[] koerperform).
Remove Spinner beside mySpinner:
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
Inside getSwtitchact, change to this
private Button getSwtitchact;
{
final Button switchact = (Button) findViewById(R.id.button3);
switchact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedValue=mySpinner.getSelectedItem().toString(); // here you get the selected items from spinner
if(selectedValue.equals("a"))
{
Intent act = new Intent(view.getContext(), xy.class);
startActivity(act);
}
else if(selectedValue.equals("B"))
{
Intent act = new Intent(view.getContext(), xyZ.class);
startActivity(act);
}
else
{
......
}
}
});
}
maybe this layout file helps
<pre>
<?xml version="1.0" encoding="utf-8"?>
<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=".Koerperform"
android:background="#023738">
<ImageView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/header" />
<ImageView
android:layout_width="400dp"
android:layout_height="100dp"
android:id="#+id/imageView5"
android:background="#drawable/frageeins"
android:layout_below="#+id/space4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="120dp"
android:id="#+id/imageView6"
android:background="#drawable/fischeins"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/imageView5" />
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="false"
android:contextClickable="false"
/>
<Button
android:layout_width="220dp"
android:layout_height="60dp"
android:id="#+id/button3"
android:layout_marginTop="69dp"
android:background="#drawable/costum_button_weiter_gehts"
android:layout_below="#+id/imageView6"
android:layout_centerHorizontal="true" />
<Space
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:id="#+id/space4" />
</RelativeLayout>

Display Data in GridView with DataAdapter in Android

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.

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..

Categories