I am working on a menu list for an app I am making. I followed this tutorial for creating it (changing values as needed) - http://www.ezzylearning.com/tutorial.aspx?tid=1763429.
Now my list isn't super complex. It's going to have the same data every time and there is only two sections. So far, I have my list working but I need to replace the 7th item with a different view/layout in xml or to add that view in.
Current Code....
Main Activity:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class HU_main extends Activity {
private ListView f_menu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hu_main);
Front_menu front_menu_data[] = new Front_menu[] {
new Front_menu("Cadences", "Reference chart of all the primary cadences."),
new Front_menu("Chords and Non-Chord Tones", "Reference chart of chord types and non-chord tones and associated rules of NCTs."),
new Front_menu("Key Signatures", "Reference chart for key signature rules."),
new Front_menu("Scale/Mode Triads", "List of triads and degrees for all standard diatonic scale modes."),
new Front_menu("Harmonic Progression", "Reference chart for scale and mode chord progressions."),
new Front_menu("Terminology Translation", "Reference list if primary music terms and instruments in German, Italian and French."),
new Front_menu("Replace", "Replace"),
new Front_menu("Chord Finder", "Enter notes to find chords."),
new Front_menu("Modulation", "Reference chart of chord types and non-chord tones and associated rules of NCTs."),
new Front_menu("Scale Finder", "Find scales and modes by notes."),
new Front_menu("Transposition", "Show changes to and from concert pitch for transposing instruments or determine overall transposition of key.")
};
Front_menuAdapter adapter = new Front_menuAdapter(this,
R.layout.item_layout, front_menu_data);
f_menu = (ListView)findViewById(R.id.f_menu);
f_menu.setAdapter(adapter);
}
}
Front_menuAdapter:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class Front_menuAdapter extends ArrayAdapter<Front_menu> {
Context context;
int layoutResourceId;
Front_menu data[] = null;
public Front_menuAdapter(Context context, int layoutResourceId, Front_menu[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Front_menuHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new Front_menuHolder();
holder.txtTitle1 = (TextView)row.findViewById(R.id.menu_item);
holder.txtTitle2 = (TextView)row.findViewById(R.id.item_description);
holder.header2 = (TextView)row.findViewById(R.id.tools_header);
row.setTag(holder);
if (position != 6) {
holder.txtTitle1.setVisibility(View.VISIBLE);
holder.txtTitle2.setVisibility(View.VISIBLE);
holder.header2.setVisibility(View.GONE);
}
else if (position == 6) {
holder.header2.setVisibility(View.VISIBLE);
holder.txtTitle1.setVisibility(View.GONE);
holder.txtTitle2.setVisibility(View.GONE);
}
}
else {
holder = (Front_menuHolder)row.getTag();
}
Front_menu front_menu = data[position];
holder.txtTitle1.setText(front_menu.item);
holder.txtTitle2.setText(front_menu.desc);
return row;
}
class Front_menuHolder {
TextView txtTitle1;
TextView txtTitle2;
TextView header2;
}
}
Front_menu (constructor):
public class Front_menu {
public String item;
public String desc;
public Front_menu() {
super();
}
public Front_menu(String item, String desc) {
super();
this.item = item;
this.desc = desc;
}
}
Instead of worrying about inflating a new View
for the one item to be shown differently, why not just define another layout
in the current XML?
Take for example, an actual piece of code from an app of mine which shows the user's Facebook newsfeed (I have shortened it and taken off the unwanted code from it). A user could have a Status Update / Photo or a Video on his / her newsfeed. So instead of creating multiple layout XMLs, I define them all in the one XML that I inflate in the adapter
.
XML Example:
<!-- STATUS | PHOTO -->
<LinearLayout
android:id="@+id/linlaStatusPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
.... OTHER ELEMENTS NEEDED
</LinearLayout>
<!-- VIDEO LAYOUT -->
<LinearLayout
android:id="@+id/linlaVideo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
.... OTHER ELEMENTS NEEDED
</LinearLayout>
Now in the adapter, I check for the condition for the type of feed and show the necessary layout while hiding the other.
if (feedType.equals("status") || feedType.equals("photo")) {
// SHOW THE NECESSARY LAYOUT CONTAINER
holder.linlaStatusPhoto.setVisibility(View.VISIBLE);
// HIDE THE OTHERS
holder.linlaVideo.setVisibility(View.GONE);
// I HAVE MORE CONDITIONS TO CHECK. HENCE THE else if().
} else if (feedType.equals("video")) {
// SHOW THE NECESSARY LAYOUT CONTAINER
holder.linlaVideo.setVisibility(View.VISIBLE);
// HIDE THE OTHERS
holder.linlaStatusPhoto.setVisibility(View.GONE);
}
You can adapt this code easily to fit in your scheme of things. You will need to use some check to determine which Widget
has to be shown. But that is true even if you were to inflate another View
. If it is always the 7th item in the List, you can add one more parameter to your front_menu_data[]
that will hold the item number and use that to trigger the above example.
UPDATE:
Move the if...else
code block from here:
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new Front_menuHolder();
holder.txtTitle1 = (TextView)row.findViewById(R.id.menu_item);
holder.txtTitle2 = (TextView)row.findViewById(R.id.item_description);
holder.header2 = (TextView)row.findViewById(R.id.tools_header);
row.setTag(holder);
// MOVE THIS BLOCK
if (position != 6) {
holder.txtTitle1.setVisibility(View.VISIBLE);
holder.txtTitle2.setVisibility(View.VISIBLE);
holder.header2.setVisibility(View.GONE);
} else if (position == 6) {
holder.header2.setVisibility(View.VISIBLE);
holder.txtTitle1.setVisibility(View.GONE);
holder.txtTitle2.setVisibility(View.GONE);
}
} else {
holder = (Front_menuHolder)row.getTag();
}
To:
Front_menu front_menu = data[position];
if (position != 6) {
holder.txtTitle1.setVisibility(View.VISIBLE);
holder.txtTitle2.setVisibility(View.VISIBLE);
holder.header2.setVisibility(View.GONE);
holder.txtTitle1.setText(front_menu.item);
holder.txtTitle2.setText(front_menu.desc);
} else if (position == 6) {
holder.header2.setVisibility(View.VISIBLE);
holder.txtTitle1.setVisibility(View.GONE);
holder.txtTitle2.setVisibility(View.GONE);
holder.txtTitle1.setText(front_menu.item);
holder.txtTitle2.setText(front_menu.desc);
}