我正在检索并显示数据库中的值,但所有查询都不会返回相同数量的列,因此我必须隐藏不需要的列。 的java:
package com.example.testback;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class Stations extends Activity implements OnItemSelectedListener {
String lat,lng,marker;
Spinner s1, s2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stations);
// TODO Auto-generated method stub
Display display =getWindowManager().getDefaultDisplay();
int height = display.getHeight();
int top=(int) (height/5.4);
LinearLayout l=(LinearLayout)findViewById(R.id.linearL2);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(0, top,0,0);
l.setLayoutParams(lp);
s1 = (Spinner) findViewById(R.id.countries);
s2 = (Spinner) findViewById(R.id.region);
s1.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
Bundle extras = getIntent().getExtras();
String name= extras.getString("name");
DataBaseHelper myDbHelper = new DataBaseHelper(
this.getApplicationContext());
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
// Open the database
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
// Get the readable version
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cur;
cur = db.rawQuery("SELECT * FROM REGIONS WHERE _id='"+name+"';", null);
int i = 0;
cur.moveToFirst();
TextView v=(TextView)findViewById(R.id.address);
v.setText(cur.getString(1));
int index;
index=cur.getColumnIndex("ph1");
if(index!=-1){
v=(TextView)findViewById(R.id.ph1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
v=(TextView)findViewById(R.id.tit_ph1);
v.setVisibility(View.VISIBLE);
} else{
v=(TextView)findViewById(R.id.ph1);
v.setVisibility(View.GONE);
v=(TextView)findViewById(R.id.tit_ph1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("ph2");
if(index!=-1){
v=(TextView)findViewById(R.id.ph2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
v=(TextView)findViewById(R.id.tit_ph2);
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.ph2);
v.setVisibility(View.GONE);
v=(TextView)findViewById(R.id.tit_ph2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("fax");
if(index!=-1){
v=(TextView)findViewById(R.id.fax);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
v=(TextView)findViewById(R.id.tit_fax);
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.fax);
v.setVisibility(View.GONE);
v=(TextView)findViewById(R.id.tit_fax);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("email1");
if(index!=-1){
v=(TextView)findViewById(R.id.email1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.email1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("email2");
if(index!=-1){
v=(TextView)findViewById(R.id.email2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.email2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("dom_ph1");
if(index!=-1){
v=(TextView)findViewById(R.id.dom_ph1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.dom_ph1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("dom_ph2");
if(index!=-1){
v=(TextView)findViewById(R.id.dom_ph2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.dom_ph2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("dom_fax");
if(index!=-1){
v=(TextView)findViewById(R.id.dom_fax);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.dom_fax);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("dom_email1");
if(index!=-1){
v=(TextView)findViewById(R.id.dom_email1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.dom_email1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("dom_email2");
if(index!=-1){
v=(TextView)findViewById(R.id.dom_email2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.dom_email2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("int_ph1");
if(index!=-1){
v=(TextView)findViewById(R.id.int_ph1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.int_ph1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("int_ph2");
if(index!=-1){
v=(TextView)findViewById(R.id.int_ph2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.int_ph2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("int_fax");
if(index!=-1){
v=(TextView)findViewById(R.id.int_fax);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.int_fax);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("int_email1");
if(index!=-1){
v=(TextView)findViewById(R.id.int_email1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.int_email1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("int_email2");
if(index!=-1){
v=(TextView)findViewById(R.id.int_email2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.int_email2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("cargo_ph1");
if(index!=-1){
v=(TextView)findViewById(R.id.cargo_ph1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.cargo_ph1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("cargo_ph2");
if(index!=-1){
v=(TextView)findViewById(R.id.cargo_ph2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.cargo_ph2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("cargo_fax");
if(index!=-1){
v=(TextView)findViewById(R.id.cargo_fax);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.cargo_fax);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("cargo_email1");
if(index!=-1){
v=(TextView)findViewById(R.id.cargo_email1);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.cargo_email1);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndex("cargo_email2");
if(index!=-1){
v=(TextView)findViewById(R.id.cargo_email2);
v.setText(cur.getString(index));
v.setVisibility(View.VISIBLE);
}else{
v=(TextView)findViewById(R.id.cargo_email2);
v.setVisibility(View.GONE);
}
index=cur.getColumnIndexOrThrow("loc_lat");
lat=cur.getString(index);
index=cur.getColumnIndexOrThrow("loc_long");
lng=cur.getString(index);
index=cur.getColumnIndexOrThrow("marker");
marker=cur.getString(index);
/*while (cur.isAfterLast() == false) {
//list.add(cur.getString(0));
cur.moveToNext();
}*/
cur.moveToPosition(0);
// Close
myDbHelper.close();
}
public void go(View view){
Intent i = new Intent(Stations.this, Map.class);
i.putExtra("lat",lat);
i.putExtra("long",lng);
i.putExtra("marker",marker);
startActivity(i);
finish();
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
if (s1.getSelectedItem().equals("India")) {
list.add("Select Station");
DataBaseHelper myDbHelper = new DataBaseHelper(
this.getApplicationContext());
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
// Open the database
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
// Get the readable version
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cur;
cur = db.rawQuery("SELECT * FROM REGIONS ORDER BY _id ASC;", null);
int i = 0;
cur.moveToFirst();
while (cur.isAfterLast() == false) {
list.add(cur.getString(0));
cur.moveToNext();
}
cur.moveToPosition(0);
// Close
myDbHelper.close();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
} else if (s1.getSelectedItem().equals("SAARC Countries")) {
list.add("Select Station");
list.add("Chittagong");
list.add("Colombo");
list.add("Dhaka");
list.add("Kabul");
list.add("Kathmandu");
list.add("Male");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
} else if (s1.getSelectedItem().equals("Russia")) {
list.add("Moscow");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
} else if (s1.getSelectedItem().equals("USA and Canada")) {
list.add("Select Station");
list.add("Chicago");
list.add("Los Angeles");
list.add("New York");
list.add("Toronto");
list.add("Washington DC");
list.add("Newark Airport (EWR)");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
} else if (s1.getSelectedItem().equals("Southeast Asia, Australia")) {
list.add("Select Station");
list.add("Bangkok");
list.add("Hongkong");
list.add("Jatarka");
list.add("Melbourne");
list.add("Sydney");
list.add("Yangon");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
} else if (s1.getSelectedItem().equals("Gulf and Middle East")) {
list.add("Select Station");
list.add("Bahrain");
list.add("Beirut");
list.add("Cairo");
list.add("Dubai");
list.add("Muscat");
list.add("Tehran");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
}
else if (s1.getSelectedItem().equals("UK and Europe")) {
list.add("Select Station");
list.add("Amsterdam");
list.add("Frankfurt");
list.add("London");
list.add("Madrid");
list.add("Zurich");
list.add("Paris");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
} else if (s1.getSelectedItem().equals("Far East")) {
list.add("Select Station");
list.add("Osaka");
list.add("Seoul");
list.add("Tokyo");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(dataAdapter);
}
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (s2.getSelectedItem().toString() != "Select Station") {
String s = s2.getSelectedItem().toString();
Intent i = new Intent(Stations.this, Stations.class);
i.putExtra("name", s);
startActivity(i);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
该元素的xml是:
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tit_fax"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:text="FAX"
android:visibility="visible"
android:textSize="14dp" />
<TextView
android:id="@+id/fax"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:visibility="visible"
android:text=" " />
</LinearLayout>
但它应该已经消失时仍然可见
答案 0 :(得分:2)
v=(TextView)findViewById(R.id.fax);
v1=(TextView)findViewById(R.id.tit_fax);
将其保留在全球范围内,然后进行工作,无需使用View.VISIBLE
,保持变量唯一
不要在XML中使用它,因为它会混淆
android:visibility="visible"
答案 1 :(得分:2)
在我的4.1.2设备上,TextView
(实际上是AutoCompleteTextView
)不想接受setVisibility(View.GONE)
,因为该框具有焦点。问题是这是屏幕上的最后一个元素,所以我花了一段时间才发现一个无辜的目标将焦点转移到。我必须将setFocusableInTouchMode(true)
用于其他视图,否则会忽略其requestFocus()
以及TextView的clearFocus()
。