Updated!
I've created a custom listview and so far i've tried implementing listeners on a button but it doesnt work
here's my main activity
public class homepage extends Activity {
ListView list;
String[] Name = {
"Lovelle Ong",
"Ryan Lopez",
"Melissa Gan"
} ;
String[] Location = {
"Botanical Gardens",
"Cape Town",
"Gardens By the Bay"
} ;
Integer[] imageId = {
R.drawable.lovelle,
R.drawable.ryanlopez,
R.drawable.melissa
};
Integer[] mainId = {
R.drawable.likedpage1,
R.drawable.commentpage,
R.drawable.melissap
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
CustomList adapter = new
CustomList(homepage.this, Name, Location, imageId, mainId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + Name [position], Toast.LENGTH_LONG)
.show(); //this doesn't work too
}
});
ImageButton back = (ImageButton) findViewById(R.id.imageButton1);
back.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainPage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
}
}
my adapter class
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] Name, Location;
private final Integer[] imageId, mainId;
public CustomList(Activity context,
String[] Name, String[] Location, Integer[] imageId, Integer[] mainId) {
super(context, R.layout.list_single, Name);
this.context = context;
this.Name = Name;
this.imageId = imageId;
this.Location = Location;
this.mainId = mainId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView name = (TextView) rowView.findViewById(R.id.textView1);
TextView location = (TextView) rowView.findViewById(R.id.textView2);
ImageView profileView = (ImageView) rowView.findViewById(R.id.imageView1);
Button mainImage = (Button) rowView.findViewById(R.id.profilepagelist1);
Button comment = (Button) rowView.findViewById(R.id.buttonComment);
final Button emptyheart = (Button) rowView.findViewById(R.id.imageView3);
final Button filledheart = (Button) rowView.findViewById(R.id.ImageViewRight);
emptyheart.setOnClickListener(new View.OnClickListener() //thisworks
{
@Override
public void onClick(View v) {
emptyheart.setVisibility(View.INVISIBLE);
filledheart.setVisibility(View.VISIBLE);
}
});
filledheart.setOnClickListener(new View.OnClickListener() //thisworks
{
@Override
public void onClick(View v) {
emptyheart.setVisibility(View.VISIBLE);
filledheart.setVisibility(View.INVISIBLE);
}
});
comment.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Log.d("fail", null, null);
// this.startActivity(new Intent(getBaseContext().this, commentpage.class)); <---- errors here
}
});
name.setText(Name[position]);
location.setText(Location[position]);
profileView.setImageResource(imageId[position]);
mainImage.setBackgroundResource(mainId[position]);
return rowView;
}
}
my list_single.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.muc2.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/mega1">
<LinearLayout
android:layout_width="match_parent"
android:paddingLeft="2dp"
android:paddingTop="2dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dp"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameholders"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:text="Lovelle Ong"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/buttonComment"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/imageView3"
android:background="@drawable/comment" />
<ImageView
android:id="@+id/imageView3"
android:layout_alignParentRight="true"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/like" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="23dp"
android:layout_height="23dp"
android:src="@drawable/geoicon" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="2dp"
android:paddingTop="2dp" >
<Button
android:id="@+id/profilepagelist1"
android:layout_width="fill_parent"
android:layout_height="310dp"
android:layout_centerInParent="true"
android:paddingBottom="5dp"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
</RelativeLayout>
</LinearLayout>
and lastly the xml file that contains the list view
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
tools:context="com.example.muc2.MainActivity$PlaceholderFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
>
<RelativeLayout
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/exit"
android:maxHeight="35dp"
android:maxWidth="35dp"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:layout_alignParentRight="false"
/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/momentlogowhite"
android:maxHeight="35dp"
android:maxWidth="35dp"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Is there a way for me to implement a listener on a button and upon clicking it, it leads me to the next activity?
thanks and sorry for the long post!