I'm doing a custom adapter and I want it to choose to show the text or not in the list based on some properties of the Object
. Here is what I'm trying to do:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
// Array of Objects
CustomObject[] customObjects = new CustomObject[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating the array
customObjects[0] = new CustomObject("Object 0", false, false, true);
customObjects[1] = new CustomObject("Object 1", true, false, true);
customObjects[2] = new CustomObject("Object 2", true, false, true);
customObjects[3] = new CustomObject("Object 3", false, false, true);
customObjects[4] = new CustomObject("Object 4", true, false, true);
// Adapter
CustomListAdapter adapter = new CustomListAdapter(this, R.layout.list_layout, customObjects, "propertyOne");
// Creating ListView
ListView listViewItems = (ListView) findViewById(R.id.customList);
listViewItems.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.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:id="@+id/customList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
list_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<TextView
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="" />
CustomObject.java:
public class CustomObject {
private String name;
private boolean propertyOne;
private boolean propertyTwo;
private boolean propertyThree;
public CustomObject (String name, boolean propertyOne, boolean propertyTwo, boolean propertyThree){
this.name = name;
this.propertyOne = propertyOne;
this.propertyTwo = propertyTwo;
this.propertyThree = propertyThree;
}
public boolean compareProperty(String property) {
if (property.equals("propertyOne")) {
return this.propertyOne;
}
else {
return false;
}
}
public String getName(){
return name;
}
}
CustomListAapter.java:
public class CustomListAdapter extends ArrayAdapter<Spells> {
Context mContext;
int layoutResourceId;
CustomObject data[] = null;
private String property;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context mContext, int layoutResourceId, CustomObject[] customObjects, String property){
super (mContext, layoutResourceId, magias);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = customObjects;
this.property = property;
}
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
CustomObject object = data[position];
if (object.compareProperty(this.property)) {
TextView textViewItem = (TextView);
convertView.findViewById(R.id.textViewItem);
textViewItem.setText(object.getName());
}
return convertView;
}
}
Basically, when I send the String propertyOne
, I want to create a list that shows just the Objects
that have the boolean propertyOne
as true
. Can someone help me, please?
MainActivity.java:
public class MainActivity extends ActionBarActivity {
final int LENGTH = 5;
// Array of Objects
CustomObject[] customObjects = new CustomObject[LENGTH];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating the array
customObjects[0] = new CustomObject("Object 0", false, false, true);
customObjects[1] = new CustomObject("Object 1", true, false, true);
customObjects[2] = new CustomObject("Object 2", true, false, true);
customObjects[3] = new CustomObject("Object 3", false, false, true);
customObjects[4] = new CustomObject("Object 4", true, false, true);
int newLength = 0;
for (int i = 0; i < LENGTH; i++) {
if (customObjects[i].compareProperty("propertyOne")) {
newLength++;
}
}
CustomObject[] newCustomObjects = new CustomObject[newLength + 1];
for (int i = 0, j = 0; i < LENGTH; i++) {
if (customObjects[i].compareProperty("propertyOne")) {
newCustomObjects[j] = customObjects[i];
j++;
}
}
newCustomObjects[newLength] = new CustomObject (" ", " ", true, true, true)
//Adapter
CustomListAdapter adapter = new CustomListAdapter(this, R.layout.list_layout, newCustomObjects);
//Creating ListView
ListView listViewItems = (ListView) findViewById(R.id.customList);
listViewItems.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CustomListAapter.java:
public class CustomListAdapter extends ArrayAdapter<Spells> {
Context mContext;
int layoutResourceId;
CustomObject data[] = null;
private String property;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context mContext, int layoutResourceId, CustomObject[] customObjects){
super (mContext, layoutResourceId, magias);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = customObjects;
}
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
CustomObject object = data[position];
TextView textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
textViewItem.setText(object.getName());
return convertView;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.