Reputation: 101
I'm new to Java and Android. I'm making an app (mostly for use by me and to get some knowledge), where I have a list view width 101 items. When I click one item, it starts a new activity, with different layout. The layout for each of the 101 items is the same, but the content is different. My question is: Can I make one .xml file for all the items, but make the content differ, based on what item is clicked?
I'll add the my code below (very experimental, and will certainly change the layout) :) Thanks for any answers!
MainAvtivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[]{"§01. Kongle", "§02. Binders",
"§03. Plaster", "§04. Kvitering", "§05. 'Ruter'-logo", "§06. Ølkork", "§07. Hjerteutklipp av russekort",
"§08. Ølkork i gull", "§09. Bussbillett", "§10. Bit av et sitteunderlag", "§11. Signatur fra helsesøster",
"§12. Skolens logo", "§13. Første bokstav", "§14. Bit av skilt", "§15. Bit av burgereske", "§16. Fjær", "§16. Bilde av Barney Stinson",
"§17. Stoffbit", "§18. Champagnekork i gull", "§19. Billettarmbånd", "§20. Amnesty-logo", "§21. Plastskje", "§22. Mobildeksel",
"§24. Legokloss", "§25. Tampong", "§26. Kritt", "§27. Undertøy", //Plus more
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_activated_2, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), fastnokkel.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), binders.class); //These classes will differ from witch item is clicked.
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), plaster.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), plaster.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), kongle.class);
startActivityForResult(myIntent, 0);
}
if (position == 5) {
Intent myIntent = new Intent(view.getContext(), binders.class);
startActivityForResult(myIntent, 0);
}
if (position == 6) {
Intent myIntent = new Intent(view.getContext(), kongle.class);
startActivityForResult(myIntent, 0);
}
if (position == 7) {
Intent myIntent = new Intent(view.getContext(), binders.class);
startActivityForResult(myIntent, 0);
}
}
});
}
Avtivity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical"
tools:context="com.example.sande.listview8.MainActivity"
android:weightSum="1">
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
Same_Layout_Different_Content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:contentDescription="@string/bildeBesk"
app:srcCompat="@drawable/kongle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/gjenstand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@string/kongle"
android:textSize="35sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/gjoremaal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/gjenstand"
android:layout_marginTop="19dp"
android:textSize="30sp"
android:text="@string/kongleBeskrivelse"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Upvotes: 1
Views: 635
Reputation: 5470
What you need to do is use a custom adapter that inherits BaseAdapter
.
This adapter will be able to update the visual part of each cell according to the your position and show/hide elements of a single xml to do so.
For example:
public class MyAdapter extends BaseAdapter {
String[] values = new String[]{"§01. Kongle", "§02. Binders",
"§03. Plaster", "§04. Kvitering", "§05. 'Ruter'-logo", "§06. Ølkork", "§07. Hjerteutklipp av russekort",
"§08. Ølkork i gull", "§09. Bussbillett", "§10. Bit av et sitteunderlag", "§11. Signatur fra helsesøster",
"§12. Skolens logo", "§13. Første bokstav", "§14. Bit av skilt", "§15. Bit av burgereske", "§16. Fjær", "§16. Bilde av Barney Stinson",
"§17. Stoffbit", "§18. Champagnekork i gull", "§19. Billettarmbånd", "§20. Amnesty-logo", "§21. Plastskje", "§22. Mobildeksel",
"§24. Legokloss", "§25. Tampong", "§26. Kritt", "§27. Undertøy", //Plus more
};
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int i) {
return values[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
Context context = parent.getContext();
convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
convertView.setTag(new ViewHolder());
}
// Here you set the view - set the text hide views etc... it would be beneficial to use ViewHolder pattern as well
ViewHolder holder = convertView.getTag();
holder.setData(i, values[i]);
}
}
Upvotes: 0
Reputation: 763
Yeah, put all type of view in one xml file. Like in this structure :
<ParentLayout>
<Type1/>
<Type2/>
......
</ParentLayout>
Keep visibility of all Type layout as Gone. Then when item is clicked in parent activity, pass a unique ID to identify the layout type. Then use a switch case in the second activity to activate the necessary layout.
Upvotes: 1