使用2个资源文件(或2个布局)进行活动。 1包含从数据库中检索arraylist的形式 第二个在列表视图中显示该列表.. 如何链接这两个资源文件?我想用充气机..但不知道在哪里使用它?设置适配器之后或在onclick()中调用getter之前 这是我尝试过的:
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>
答案 0 :(得分:0)
我认为在填写编辑文本中的数据并点击提交按钮后,您需要一个布局。如果是这样,您可以在创建活动时首先设置布局activity_main.xml,即onCreate,一旦填充并点击提交按钮,单击按钮即更改布局,即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);
}
}
这是不合适的,但如果你想以这种方式使用它,那么你就可以继续了。理想情况下,您应该制作两个不同的活动,并在按钮点击时,您应该在Intent的帮助下移动到新的活动。希望这会有所帮助。
答案 1 :(得分:0)
要专门回答 你应该给ListView充气的问题,你应该在设置适配器之前对ListView 进行充气。
由于通货膨胀是在callingMore()
完成的,而不是在callingMore()
之后调用listView.setAdapter(adapter);
,因此您应该在>>设置适配器之前调用,如下所示。
@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();
}
}
在调用listView = (ListView) findViewById(R.id.listView);
;之前,您似乎忘记设置listView.setAdapter(adapter)
。这将导致NullPointerException,并且可能是您对应该在哪里进行通货膨胀感到困惑的原因。您只能获得对您的ListView的引用,其ID为R.id.listView 后它已被夸大。
您也忘了将getResList()的返回值赋给ResList。它应该是ResList = getResList();
,而不仅仅是getResList();
此外,onCreate(),listView = (ListView)findViewById(R.id.list_view)
;可能会出现拼写错误。你的意思可能是listView = (ListView)findViewById(R.id.listView);
。
哦,在调用More()两行,
View view = inflater.inflate(R.layout.activity_mylist,null);
layoutToAdd.addView(view);
只能替换为View view = inflater.inflate(R.layout.activity_mylist, layoutToAdd);
,因为在第二个参数中放置layoutToAdd会将其设置为view
的父视图。