• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Please Help with ListView

Cole001

Lurker
PLEASE help with ListView.. I cant solve this alone :(

Hi. I'm new in Android development (I'm not that bad in networking, web development, c#.. but here Im big noob). As my first task (after spending some time with books) I decided to make Class Schedule fore my daughter. So, I end up with huge problems with ListView (simple_expandable_list_item_1). Finally, I found some code online and (in the last few days) I'm trying to make it work. Probably I messed up a lot, but I really hope that someone can help. Please :(
Make it work :)

ListViewID is raspView

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleExpandableListAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.SimpleCursorTreeAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


List<Map<String, String>> groupData = new ArrayList<Map<String, String>>() {{
add(new HashMap<String, String>() {{
put("ROOT_NAME", "Group 1");
}});
add(new HashMap<String, String>() {{
put("ROOT_NAME", "Group 2");
}});
}};

List<List<Map<String, String>>> listOfChildGroups = new ArrayList<List<Map<String, String>>>();

List<Map<String, String>> childGroupForFirstGroupRow = new ArrayList<Map<String, String>>(){{
add(new HashMap<String, String>() {{
put("CHILD_NAME", "child in group 1");
}});
add(new HashMap<String, String>() {{
put("CHILD_NAME", "child in group 1");
}});
}};
listOfChildGroups.add(childGroupForFirstGroupRow);

List<Map<String, String>> childGroupForSecondGroupRow = new ArrayList<Map<String, String>>(){{
add(new HashMap<String, String>() {{
put("CHILD_NAME", "child in group 2");
}});
add(new HashMap<String, String>() {{
put("CHILD_NAME", "child in group 2");
}});
}};
listOfChildGroups.add(childGroupForSecondGroupRow);


new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { "ROOT_NAME" },
new int[] { android.R.id.text1 },

listOfChildGroups,
android.R.layout.simple_expandable_list_item_2,
new String[] { "CHILD_NAME", "CHILD_NAME" },
new int[] { android.R.id.text1, android.R.id.text2 }

);

ListAdapter casAdapter = new ArrayAdapter<String>(this, android.R.layout.expandable_list_content);

ListView raspView = (ListView) findViewById(R.id.raspView);
raspView.setAdapter(casAdapter);
}

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
 
Hey there, wrapping your head around android's way of using ListViews can be confusing for a while at first. Let me know if you are still struggling and we can try to walk you through it.
 
ListViews can be complicated, particularly when you add in custom adapters. What's the problem you're having?
Your code isn't formatted in the best way, use the code insert option to make it look more readable.
 
Back
Top Bottom