Last active
December 23, 2015 07:19
-
-
Save joeyjiron06/6599676 to your computer and use it in GitHub Desktop.
Here is a custom listview for android. Customizable cells, displays custom objects. Feel free to download, fork, customize and make use of it. Add these files to your Android project.
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
... |
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
<!-- layout_cell.xml --> | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#444444"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/textView1" | |
android:text="Text 1" | |
android:textSize="20sp" | |
android:textColor="#ffffff" | |
android:layout_gravity="center"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/textView2" | |
android:text="Text 2" | |
android:textColor="#ffffff" | |
android:layout_gravity="center"/> | |
</LinearLayout> | |
<!-- layout_cell.xml --> |
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
//ListAdapter.java | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import java.util.List; | |
/** | |
* Created by joey on 7/12/13. | |
* This is a custom List Adapter that takes in a list of objects to display | |
*/ | |
public class ListAdapter extends BaseAdapter | |
{ | |
private List<MyObject> objects; | |
public ListAdapter(List<MyObject> objs) { | |
setList(objs); | |
} | |
public void setList(List<MyObject> list) { | |
if (objects == null) { | |
throw new IllegalArgumentException("cannot assign a null list"); | |
} | |
objects = list; | |
} | |
@Override | |
public int getCount() { | |
return objects.size(); | |
} | |
@Override | |
public MyObject getItem(int position) { | |
return objects.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if(convertView == null) { | |
convertView = View.inflate(parent.getContext(), R.layout.layout_cell, null); | |
} | |
MyObject model = getItem(position); | |
//set text of each text view | |
((TextView)(convertView.findViewById(R.id.textView1))).setText(model.text1); | |
((TextView)(convertView.findViewById(R.id.textView2))).setText(model.text2); | |
return convertView; | |
} | |
} | |
//ListAdapter.java |
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
//MainActivity.java | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import java.util.ArrayList; | |
public class MainActivity extends Activity | |
implements AdapterView.OnItemClickListener | |
{ | |
private ListView listview; | |
private ArrayList<MyObject> objects; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
objects = new ArrayList<MyObject>(); | |
objects.add(new MyObject("object1 text1", "object1 text2")); | |
objects.add(new MyObject("object2 text1", "object2 text2")); | |
objects.add(new MyObject("object3 text1", "object3 text2")); | |
objects.add(new MyObject("object4 text1", "object4 text2")); | |
listview = (ListView) findViewById(R.id.listview); | |
listview.setAdapter(new ListAdapter(objects)); | |
listview.setOnItemClickListener(this); | |
} | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int postition, long l) { | |
MyObject obj = (MyObject) adapterView.getAdapter().getItem(postition); | |
Log.d("MainActivity", "you clicked item " + postition + " " + obj.toString()); | |
} | |
} | |
//MainActivity.java |
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
<!-- MainActivity.xml --> | |
<LinearLayout | |
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" | |
> | |
<ListView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/listview" | |
/> | |
</LinearLayout> | |
<!-- MainActivity.xml --> |
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
//MyObject.java | |
public class MyObject | |
{ | |
public String text1; | |
public String text2; | |
public MyObject(String string1, String string2) | |
{ | |
text1 = string1; | |
text2 = string2; | |
} | |
@Override | |
public String toString() { | |
return text1 + " " + text2; | |
} | |
} | |
//MyObject.java |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment