0

make activity with 2 resource files (or 2 layouts). 1 containg form to retrieve arraylist from database and 2nd to show that list in list view.. how to link these two resource files? i want to use inflator.. but don't know where to use it? after setting adapter or before calling the getter in onclick() here is what i've tried:

MainActivity.java

package com.example.bsitn_000.datalist;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener {

    private RelativeLayout layoutToAdd;
    EditText NoP;
    EditText eTime;
    EditText lTime;
    EditText date;
    Button check;

    ListView listView;
    ArrayAdapter<String> adapter;

    ArrayList<String> ResList;

    protected SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layoutToAdd = (RelativeLayout)findViewById(R.id.existedlayout);

        createDatabase();

        NoP = (EditText)findViewById(R.id.editText4);
        eTime = (EditText)findViewById(R.id.editText);
        lTime = (EditText)findViewById(R.id.editText2);
        date = (EditText)findViewById(R.id.editText3);

        listView = (ListView)findViewById(R.id.list_view);

        check =(Button)findViewById(R.id.button);
        check.setOnClickListener(this);

    }

    public void createDatabase() {
        db = openOrCreateDatabase("Restaurant.db", Context.MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS reserve(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                " NoP Text, eTime Text, lTime Text, date Text, res Text);");
    }

    public ArrayList<String> getResList(){
        String n = NoP.getText().toString();
        String e = eTime.getText().toString();
        String l = lTime.getText().toString();
        String d = date.getText().toString();
        ResList = new ArrayList<String>();

        String query = "SELECT res FROM reserve WHERE NoP = '"+n+"' AND eTime = '"+e+"'AND lTime = '"+l+"'AND date = '"+d+"'";
        Cursor cursor = db.rawQuery(query,null);
        if (cursor.moveToFirst()){
            do {
                ResList.add(cursor.getString(cursor.getColumnIndex("res")));
            }while (cursor.moveToNext());

        }
        return ResList;
    }

    @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);
    }

    private void callingMore(){
        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.activity_mylist,null);
        layoutToAdd.addView(view);
    }


    @Override
    public void onClick(View v) {
        if (v == check){
            getResList();
            if (ResList.size()!=0){
                adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ResList);
                listView.setAdapter(adapter);
                callingMore();
            }
            else {
                Toast.makeText(this,"no record",Toast.LENGTH_SHORT).show();

            }
        }

    }
}

activity_main.xml

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/existedlayout">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="40dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText3"
        android:layout_below="@+id/editText2"
        android:layout_alignRight="@+id/editText2"
        android:layout_alignEnd="@+id/editText2"
        android:inputType="text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/c"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="#bbff99" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText4"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText" />
</RelativeLayout>

activity_mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
Ash
  • 3
  • 5
  • you can add`ListView` to `activity_main.xml` as a child whenever you needed in the View just adjust visibility settings accordingly – AndroidDev Oct 28 '15 at 14:51
  • Please, at least put some effort into your English. I was going to suggest an edit to fix it, but I don't even get what you actually want... – Daniël van den Berg Oct 28 '15 at 15:07
  • i want when i will press the button "check" in activity_main.xml, it should navigate to activity_mylist.xml to show list retrieved from database – Ash Oct 28 '15 at 16:50
  • AndroidDev I've done this before. and it absolutely works.. but it doesn't fulfill my requirements, so I've to navigate to another layout – Ash Oct 28 '15 at 17:08

2 Answers2

0

I think you want one layout after filling up the data in the edit text and hitting the submit button. If so, you can priorly set the layout activity_main.xml on first when activity is created i.e. onCreate and once you fill and hit the submit button change the layout on click of button i.e. activity_mylist.xml

e.g.

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

    Button btnSubmit = (Button) findViewById(R.id.button);

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           setContentView(R.layout.activity_list);
           ListView lv = (ListView) findViewById(R.id.listView);
        }     

}

This is inappropriate but if you want to use it this way only then you can proceed. Ideally you should make two different activity and on button click you should move to new activity with help of Intent. Hope this helps.

  • i tried it as well, but app stops and if it moves to next activity, it shows blank page and not the list.. can u help me for intent?? where to put it.. – Ash Oct 28 '15 at 16:53
0

To specifically answer your question of where you should inflate the ListView, you should inflate the ListView before setting the adapter.

Since the inflation is done in callingMore(), instead of invoking callingMore() after listView.setAdapter(adapter);, you should invoke it before setting the adapter as shown below.

    @Override
    public void onClick(View v) {
        if (v == check){
            ResList = getResList();
            if (ResList.size()!=0){
                callingMore();
                adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ResList);
                listView = (ListView) findViewById(R.id.listView);
                listView.setAdapter(adapter);
            }
            else {
                Toast.makeText(this,"no record",Toast.LENGTH_SHORT).show();
            }
        }

It also seems that you have forgotten to set listView = (ListView) findViewById(R.id.listView); before calling listView.setAdapter(adapter);. This will cause a NullPointerException and might be why you were confused as to where you should do the inflation. You can only get a reference to your ListView with the id R.id.listView after it has been inflated.

You also forgot to assign the return value of getResList() to ResList. It should be ResList = getResList(); instead of just getResList();

Also, there might be a typo in onCreate(), listView = (ListView)findViewById(R.id.list_view);. You probably meant listView = (ListView)findViewById(R.id.listView); instead.

Oh and in callingMore() the two lines,

View view = inflater.inflate(R.layout.activity_mylist,null);
layoutToAdd.addView(view);

can be replaced with just View view = inflater.inflate(R.layout.activity_mylist, layoutToAdd); since putting layoutToAdd in the second parameter sets it as the parent view of view.

nickor
  • 18
  • 1
  • 4
  • Thank You so much for your answer...I've setlistView = (ListView) findViewById(R.id.listView); in onCreate method. and typos didn't made any problems as I've retrieved list in mainactivity first.. that runs fine but now i want that list must be shown in next interface/layout/activity – Ash Oct 28 '15 at 17:02
  • I tried your suggestions.. App still stops. what to do? – Ash Oct 28 '15 at 17:27
  • Setting `listView = (ListView) findViewById(R.id.listView);` in onCreate is useless as it will return null. At this point in time the ListView doesn't exist yet, as it has not been inflated and attached to the layout of the activity. Either set `listView = (ListView) findViewById(R.id.listView);` in onClick as I've mentioned, or inflate the ListView in onCreate first before doing `listView = (ListView) findViewById(R.id.listView);`. – nickor Oct 28 '15 at 18:24
  • To start a new activity see https://developer.android.com/training/basics/firstapp/starting-activity.html. Pay extra attention to the portion regarding Intents and play around with using them. They'll come in handy because you'll probably want to send data (e.g. the list ResList) to the new Activity, to populate the ListView. Starting a new Activity is quite basic and if you really want to develop Android apps, I recommend going through the tutorials on the Android Developer site, they're quite good. – nickor Oct 28 '15 at 18:26
  • As to why setting listView = (ListView) findViewById(R.id.listView); in onCreate is useless, note the line `setContentView(R.layout.activity_main);` in onCreate. What this line does is inflate the layout specified in activity_main.xml and associate it with MainActivity. This will only be done once and only for activity_main.xml. activity_mylist.xml will not be inflated or attached. Subsequent calls to setContentView will override the existing layout of the Activity with another layout. – nickor Oct 28 '15 at 18:40
  • I edited and wrote it in onClick() as you asked.. after that i commented its not working. app stops.. – Ash Oct 28 '15 at 18:49
  • What does logcat say? When the app stops it's most probably due to an exception. – nickor Oct 28 '15 at 18:56
  • thanks for ur recomendations.. I know how to start new activity and already tried to show list in new activity but the app stops. then i moved to show list in another layout instead of activity.. the problem is of null pointer on button click,as far as i understood.. – Ash Oct 28 '15 at 19:08
  • thanks for your help problem is solved i was missing ResList = getResList(); which was resulting in null pointer... Thank You soooo much.. now I am going to try it using two activities instead of layouts.. hope it will work now – Ash Oct 28 '15 at 19:19
  • excuse me... i tried it using an intent to start new activity.. but it first shows me a message "app stoped" and then start next activity (that is blank) but doesn't show list – Ash Oct 28 '15 at 19:36