How to Customize Listview Using Baseadapter

How to customize listview using baseadapter

main.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: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
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

</ListView>

</RelativeLayout>

custom.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="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

</LinearLayout>

</LinearLayout>

main.java:

package com.example.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {

ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}

class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;

dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}

public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;

}

public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}

public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);

return (row);
}
}
}

Try this.

Custom ListView using CardView BaseAdapter OnItemClick

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Model model= (Model) wisataAdapter.getItem(position);

if(position==0){
Intent intent = new Intent(GunungKidul.this, Pantai_Kesirat.class);
//To pass whole model data with intent:
intent.putExtra("MyClassKey", model);
startActivity(intent);
}
else if{
Intent intent = new Intent(GunungKidul.this, Goa_Pindul.class);
//To pass whole model data with intent:
intent.putExtra("MyClassKey", model);
startActivity(intent);
}
else if{
Intent intent = new Intent(GunungKidul.this, Pantai_Baron.class);
//To pass whole model data with intent:
intent.putExtra("MyClassKey", model);
startActivity(intent);
}
else if{
Intent intent = new Intent(GunungKidul.this, Goa_Jomblang.class);
//To pass whole model data with intent:
intent.putExtra("MyClassKey", model);
startActivity(intent);
}
else if{
Intent intent = new Intent(GunungKidul.this, AirTerjun.class);
//To pass whole model data with intent:
intent.putExtra("MyClassKey", model);
startActivity(intent);
}



}
})

;

use this code But always remember your card position or index value and which activity should open when clicked cardViewlike if u click First cardView open Pantai_Kesirat activity

Custom Base Adapter to ListView

Inside the UserRepo class you never initialize ArrayList<user> list. Change the code to initialize it.

public class UserRepo {
ArrayList<user> list = new ArrayList<>();
// The rest of your code
}

How can I using Custom baseAdapter to display data in ListView?

change

listAdapter = new FeedListAdapter(this, feedItems);

to

listAdapter = new FeedListAdapter(YourActivity.this, feedItems);

YourActivity is the name of Activity inside which you are calling addValueEventListener.
inside onDataChange method this == ValueEventListener, not an Activity, which is needed for FeedListAdapter creator

Unable to populate custom listView using BaseAdapter in Android

first in onCreateView()

change this

list.setAdapter(new myAdapter(c));

to

list.setAdapter(new myAdapter(this.getActivity()));

because 'c' will be null.

for next,
as in above.

change

for(int i=0;i<10;i++)

to

for(int i=0;i<oneofarray.length;i++)

Duplicate item custom listview with BaseAdapter

Your getView implementation is not correct. Try this -

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

TextView tvUsername, tvTitle;

if (convertView == null) {
LayoutInflater layoutInflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = layoutInflator.inflate(R.layout.custom_listview_home, parent, false);
}


map = membersList.get(position);

tvUsername = (TextView) convertView.findViewById(R.id.tvUsername);
tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);

tvUsername.setText(map.get(FragmentHome.USERNAME));
tvTitle.setText(map.get(FragmentHome.TITLE));

return convertView;
}


Related Topics



Leave a reply



Submit