Creating two expandable menus Android Java - java

I've created two expandable menus in my application but seem to have messed up somewhere. Each menu displays duplicate content when they should each have their own list content.
Here's my code:
MainActivity.java
String names[] = Constant.name;
String subNames[] = Constant.subNameRecoveryTools =
Constant.subNameAdmissions;
private List<TitleMenu> getList() {
List<TitleMenu> list = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
List<SubTitle> subTitles = new ArrayList<>();
for (int j = 0; j < subNames.length; j++) {
SubTitle subTitle = new SubTitle(subNames[j]);
subTitles.add(subTitle);
}
TitleMenu model = new TitleMenu(names[i], subTitles, null);
list.add(model);
}
return list;
}
Constant.java
public class Constant {
public static String [] name = {"Recovery Tools", "Admissions"};
public static String [] subNameRecoveryTools = {"Withdrawal Scales", "Shame Scale", "Drinking Limits", "Worksheets", "eBooks"};
public static String [] subNameAdmissions = {"Pre_Screen", "Intake Paperwork", "Client Handbook", "Meet your Therapist"};
}

Go throught this link. Hope it helps you.
https://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

Related

Try to fill ArrayList from object with String and int.Is not working

I have class Book with three fields - name, author and isbn
I`m trying to insert the fields in ArrayList and to print:
book1, author1, isbn
book2, author2, isbn2
and... to 10
code:
public class InsertBooks {
private static ArrayList<String> booksNames = new ArrayList<>();
private static ArrayList<String> booksAuthors = new ArrayList<>();
private static ArrayList<Integer> booksIsbn = new ArrayList<>();
private static ArrayList<Book> books = new ArrayList<>();
// adding books in ArrayList booksNames
private static void addBooksNames() {
for (int i = 0; i < 10; i++) {
booksNames.add("Book" + i);
}
}
// adding author in ArrayList booksAuthors
private static void addBooksAuthor() {
for (int i = 0; i < 10; i++) {
booksAuthors.add("Author" + i);
}
}
// adding author in ArrayList booksAuthors
private static void addBooksIsbn() {
for (int i = 0; i < 10; i++) {
booksIsbn.add(Integer.valueOf("isbn" + i));
}
}
public static void fillArrayListOfBooks() {
for (int i = 0; i < 10; i++) {
books.add(new Book((addBooksNames(), addBooksAuthor(), addBooksIsbn()));
}
}
}
You want to call all your add* functions first. Then in the loop of fillArrayListOfBooks() use those values.
void fillArrayListOfBooks()
{
addBooksNames();
addBooksAuthor();
addBooksIsbn();
for (int i = 0; i < 10; i++) {
dbooks.add(new Book(booksNames.get(i), booksAuthors.get(i), booksIsbn.get(i)));
}
}
You could easily get rid of those lists (unless you need them later):
void fillArrayListOfBooks()
{
for (int i = 0; i < 10; i++) {
dbooks.add(new Book("Book" + i, "Author" + i, "isbn" + i));
}
}

Logcat Doesn't work properly. why?

Logcat only appears inside the onCreate method of MainActivity. I'm trying to use the Log (TAG, "msg"); within a class, and no log appears. why? in MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("Test", "test");
}
It works!
But this class doesn't work:
public class Imgpixel {
private static final String TAG = "Quicknotes";
public Imgpixel() {
}
String src="C:\\path_of_image\\img.jpg";
Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR);
int lin = imgRead.rows(); //get the number of rows
int col = imgRead.cols(); //get the number of cols
List<double[]> pixels=new ArrayList<>();//arraylist to save array rgb below
public void cor() {
for (int i = 0; i < lin; i++) {
for (int j = 0; j < col; j++) {
double [] rgb = imgRead.get(i, j);
pixels.add(0, rgb);
}
}
}
}
I am surprised your code example would compile. In your Imgpixel class the method cor() is defined outside the class, the variable pixels wouldn't be accessible and you have an extra }.
Call your cor method from another class (eg. from inside MainActivity) like this:
Imgpixel imgPixel = new Imgpixel();
imgPixel.cor();
When your Imgpixel class looks like this:
public class Imgpixel{
private static final String TAG = "Quicknotes";
String src="C:\\path_of_image\\img.jpg";
Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR);
int lin = imgRead.rows(); //get the number of rows
int col = imgRead.cols(); //get the number of cols
List<double[]> pixels=new ArrayList<>();
public Imgpixel(){
// initializing
}
public void cor() {
Log.v(TAG, "test"); //Remove the quotation marks from TAG in order to get value from the variable you set earlier
for (int i = 0; i < lin; i++) {
for (int j = 0; j < col; j++) {
double [] rgb = imgRead.get(i, j);
pixels.add(0, rgb);
}
}
}
} //this marks the end of the Imgpixel class
But you can also make Imgpixel a static class.

Java null point exception error [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am very confused!!
In my Question class, I am trying to print a question in an array and the corresponding multiple choice answers stored in a separate 2D array. I put that in a loop and am passing int row from the main method and trying to get user input and pass that back to my Question class. Probably doesn't make sense but I get a null point error.
Here's all of my code
public class Question{
private int selectedAnswer;
public String[] questions =
{
"Favourite sweet",
"Favourite subject at Hogwarts",
"Dream vacation",
"Favourite Drink",
"Dream House",
"What do you desire the most?",
"Favourite dress robe colour",
"Pick a muggle career",
"Pick a creature"
};
private String[][] options =
{
{"1.Acid Pops","2.Sherbert Lemons","3.Bertie Bott's Every Flavour Beans",
"4.Cake","5.Hagrid's Rock Cakes","6.Chocolate Frogs","7.Ginger Newt",
"8.I hate sweets"},
{"1.Care of Magical Creatures","2.Charms","3.Defense Against the Dark Arts",
"4.Divination","5.Herbology","6.History of Magic","7.Muggle Studies","8.Potions",
"9.Study of Ancient Runes","10.Transfiguration"},
{"1.Anywhere with friends","2.Egypt","3.Hogwarts","4.Museum","5.India","6.Forest",
"7.Can't be bothered with a vacation"},
{"1.Unicorn blood", "2.Pumpkin Juice", "3.Butter beer", "4.Coca-Cola", "5.Tea", "6.Coffee", "7.Brandy"},
{"1.The Burrow", "2.A Cottage", "3.Thirteen Grimmauld Place", "4.Malfoy Manor"},
{"1.Friends", "2.Success", "3.Money", "4.Power"},
{"1.Black", "2.Red", "3.Pink", "4.Green", "5.Orange", "6.Blue"},
{"1.Lawyer", "2.Teacher", "3.Social Worker", "4.Prime Minister", "5.Google Employee"},
{"1.Centaur", "2.Basilisk", "3.Unicorn", "4.Thestral", "5.Phoenix", "6.Hippogriff", "7.Dementor"}
};
private String[] quizQuestion;
private String[][] quizOptions;
//new update:
private String quizQuestion;
private String[] quizOptions
public Question(int row){
for(int i= row; i< questions.length; i++){
**quizQuestion[i] = questions[i];** //null point error
quizQuestion = questions[i]; // new update
for(int j = row; j < options[i].length; j++){
quizOptions[i][j] = options[i][j];
quizOptions[i] = options[i][j]; // new update
}
}
}
public String[] getQuizQuestion(){
**return this.quizQuestion;** //null point error
}
public String[][] getQuizOptions(){
return this.quizOptions;
}
public void setSelectedAnswer(int userInput){
selectedAnswer = userInput;
}
}
Main method
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Question q = new Question(0);
System.out.println(q.getQuizQuestion());
System.out.println(q.getQuizOptions());
Scanner keyboard = new Scanner (System.in);
int userInput = keyboard.nextInt();
System.out.println("Select an answer: ");
q.setSelectedAnswer(userInput);
}
}
You have to create an array before you can fill it.
public Question(int row){
quizQuestion = new String[ <expected length> ];
quizOptions = new String[ <expected length> ][]; //Outer array
for(int i= row; i< questions.length; i++){
**quizQuestion[i] = questions[i];** //null point error
quizOptions[i] = new String[]; //Each inner array
for(int j = row; j < options[i].length; j++){
quizOptions[i][j] = options[i][j];
}
}
}
But, as far as I can tell, quizQuestion should just be one String. And quizOptions should be a String array. If a Question object holds the details of only one question, the question should be a String and the options should be only the options of that question.
Update:
private String quizQuestion;
private String[] quizOptions
public Question(int row){
quizQuestion = questions[row];
quizOptions = new String[options[row].length];
for(int j = 0; j < options[row].length; j++){
quizOptions[j] = options[row][j];
}
}
public String getQuizQuestion(){
return this.quizQuestion;
}
public String[] getQuizOptions(){
return this.quizOptions;
}
Also, you may not need to copy options at all:
private String quizQuestion;
private String[] quizOptions
public Question(int row){
quizQuestion = questions[row];
quizOptions = options[row]; //Just refer to the existing piece
}
public String getQuizQuestion(){
return this.quizQuestion;
}
public String[] getQuizOptions(){
return this.quizOptions;
}

android listview adapter combine two arrays

I'm wornig sqllite.i have some tables and i select name for example Customer Table and Price from AnotherTable. and i received two array list .first name's array list and secods price's array list.
this is a my source
private ArrayList<GetDictioanyClassByPosition> GetPKFromTable() {
price_array.clear();
ArrayList<GetDictioanyClassByPosition> my_list = d_Helper
.getAllPriceByPosition(0);
for (int i = 0; i < my_list.size(); i++) {
String AllPrice = my_list.get(i).getDictionaryPrice();
GetDictioanyClassByPosition cnt = new GetDictioanyClassByPosition();
System.err.println(AllPrice + "AllPrice");
cnt.setDictionaryPrice(AllPrice);
price_array.add(cnt);
}
return price_array;
}
this is a method to check price's array list and this method check name's array list
public void AllInfoFromLoanAnalysis() {
name_list.clear();
ArrayList<GetAllDictionariesClass> contact_array_from_db = d_Helper
.getAllInformationFromLoanAnalysis_Table(1, 1);
Log.e("Sizee", contact_array_from_db.size() + "sizee");
for (int i = 0; i < contact_array_from_db.size(); i++) {
int DictionaryID = contact_array_from_db.get(i).getDictionaryID();
System.out.println(DictionaryID + "DictionaryID");
GetDictioanyClassByPosition object = new GetDictioanyClassByPosition();
String name = null ;
ArrayList<GetDictioanyClassByPosition> DictionaryIdList = d_Helper
.GetDictionaryIdList(DictionaryID);
System.out.println(DictionaryIdList.size() + "DictionaryIdList");
Log.e("Sizee2", DictionaryIdList.size() + "sizee2");
for (int j = 0; j < DictionaryIdList.size(); j++) {
name= DictionaryIdList.get(j).getDictionaryName();
Log.e("object", name + "object");
object.setDictionaryName(name);
name_list.add(object);
}
for (int j = 0; j < price_array.size(); j++) {
String AllPrice = price_array.get(i).getDictionaryPrice();
object.setDictionaryPrice(AllPrice);
object.setDictionaryName(name);
price_array.add(object);
}
agroproductslistview.setAdapter(agroproduct_adapter);
}
}
and i called my BaseAdapter Like this
_adapter = new LoanProductAdapter(
getApplicationContext(), R.layout.productlistadapter,
name_list);
public class GetDictioanyClassByPosition {
private String DictionaryName;
private String DictionaryPrice;
public String getDictionaryName() {
return DictionaryName;
}
public void setDictionaryName(String DictionaryName) {
this.DictionaryName = DictionaryName;
}
public String getDictionaryPrice() {
return DictionaryPrice;
}
public void setDictionaryPrice(String DictionaryPrice) {
this.DictionaryPrice = DictionaryPrice;
}
}
i can selected and show my prices and names in different array list but i want to marge both array list and would adapter in my list view
.how i can solve my problem?
if anyone knows solution please help me
thanks
Please refer below example.
public ArrayList<customObject> _historyArrayList = new ArrayList<customObject>();
public ArrayList<customObject> _completedArraylist = new ArrayList<customObject>();
For merging simply use:
_historyArrayList.addAll(_completedArraylist);
Note: Make sure your customObject are same

Get a random subset from a result set in java

I have a set of 100 object.
How can i get a subset of 5 objects from this set ?
I'm doing this for now but it only returns me one object
int size = memberSet.size();
Set<Member> randomSet = new HashSet<Member>();
int item = new Random().nextInt(size);
int i = 0;
for(Member mbr : memberSet)
{
if (i == item){
randomSet.add(mbr);
}
i = i + 1;
}
List<Member> list = new LinkedList<Member>(memberSet);
Collections.shuffle(list);
Set<Member> randomSet = new HashSet<Member>(list.subList(0, 5));
Full example:
public static void main(String... args) {
Set<Member> memberSet = new HashSet<Member>();
for (int i = 0; i < 100; i++)
memberSet.add(new Member(i));
List<Member> list = new LinkedList<Member>(memberSet);
Collections.shuffle(list);
Set<Member> randomSet = new HashSet<Member>(list.subList(0, 5));
System.out.println(randomSet);
}
static class Member {
final int value;
public Member(int value) {
this.value = value;
}
#Override
public String toString() {
return "" + value;
}
}
Although #dacwe solution is much better I can't help myself, on joke, to just say put a for(int i=0; i<5; i++) around everything and move out the Set randomSet = new HashSet();
Outside the for loop :

Categories