I have tried so many different tutorials trying to get it work I'm wondering if something is wrong with my machine.. This is my MainActivity where I create myadapter and set it to the ListView.
public class MainActivity extends Activity {
UserRepo userRepo;
ArrayList<user> userList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
userRepo = new UserRepo();
userList = userRepo.getUserArrayList();
UserAdapter myadapter = new UserAdapter(this, userList );
list.setAdapter(myadapter);
}
}
This is my custom user class.
public class user {
String name;
String description;
public user(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
This is my custom UserAdapter. I have tried so many different versions of this from so many different tutorials and no matter what I do I can't get it to work with the ArrayList that I'm passing it.
public class UserAdapter extends BaseAdapter {
ArrayList<user> list;
Context context;
LayoutInflater mInflater;
public UserAdapter(Context c, ArrayList<user> list)
{
context = c;
this.list = list;
mInflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rowlayout, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
user currentListData = (user) getItem(position);
mViewHolder.tv_name.setText(currentListData.getName());
mViewHolder.tv_description.setText(currentListData.getDescription());
return convertView;
}
private class MyViewHolder {
TextView tv_name, tv_description;
public MyViewHolder(View item) {
tv_name = (TextView) item.findViewById(R.id.tv_name);
tv_description = (TextView) item.findViewById(R.id.tv_description);
}
}
}
This is my UserRepo where I create the ArrayList and have a method that I can use to create it and return one.
public class UserRepo {
ArrayList<user> list;
public ArrayList<user> getUserArrayList(){
user john = new user("John", "Samoan Flatmate");
list.add(john);
user ariki = new user ("Ariki", "Māori Flatmate");
list.add(ariki);
user dion = new user ("Dion", "Asian Flatemate");
list.add(dion);
return list;
}
}
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.petakirikiri.customlistview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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
}