Created
February 11, 2017 12:57
-
-
Save menuka94/1ad2d4ed46177c3f8a925233b335b896 to your computer and use it in GitHub Desktop.
Android List View Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:id="@+id/activity_main" | |
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="com.example.menuka.adaptertest.MainActivity"> | |
<ListView | |
android:id="@+id/names_list_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.menuka.adaptertest; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private List<String> namesList; | |
private ArrayAdapter<String> namesAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
namesList = new ArrayList<>(); | |
namesList.add("Mark"); | |
namesList.add("John"); | |
namesList.add("Oliver"); | |
namesList.add("Finn"); | |
namesList.add("Nicole"); | |
namesList.add("Tom"); | |
namesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, namesList); | |
ListView namesListView = (ListView) findViewById(R.id.names_list_view); | |
namesListView.setAdapter(namesAdapter); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.menuka.adaptertest; | |
/** | |
* Created by menuka on 2/11/17. | |
*/ | |
public class Person { | |
private String name; | |
private String age; | |
public Person(String name, String age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getAge() { | |
return age; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.menuka.adaptertest; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
import java.util.List; | |
/** | |
* Created by menuka on 2/11/17. | |
*/ | |
public class PersonAdapter extends ArrayAdapter<Person>{ | |
private static final String TAG = PersonAdapter.class.getSimpleName(); | |
List<Person> personList; | |
public PersonAdapter(Context context, int resource, List<Person> objects) { | |
super(context, resource, objects); | |
personList = objects; | |
} | |
@NonNull | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View listItemView = convertView; | |
Person currentPerson = personList.get(position); | |
if(listItemView == null){ | |
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.student_list_item, parent, false); | |
} | |
// TextView nameTextView = | |
return listItemView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment