2014-04-04 14 views
0

Android中是否可以上下左右滚动大型数据表?我有一个从SQLite数据库生成的表。我希望能够浏览它,既上下左右。我花了很多时间使用GridView,意识到它只适应上下浏览。我也用TwoWayGridView做了一个快速实现,它可以上下左右或者左右浏览,但不能同时在同一张桌面上执行。 在Android中实现双向数据表的双向浏览的方法是什么? 在此先感谢...Android中是否可以向上和向下滚动大型数据表?

回答

0

Android不可能提供任何默认控件来管理这种情况。您必须使您自己的自定义画廊左右滑动。但根据我的经验,它只能解决您的RAW库文件的轻量操作问题。你必须是最佳的。

库列表视图并不好主意,因为画廊弃用 ,但它可以,至少给你的想法用不同的水平适配器 像tow-way-view

/** 主类 垂直列表中的**/

public class Demo extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.demo); 
     ListView listView = (ListView)findViewById(R.id.listView1); 
     List list = new List(Demo.this); 
     listView.setAdapter(list); 

    } 
    } 

/** 定义适配器,我已经花了列表视图,但你可以ü SE您的表布局 **/

public class List extends BaseAdapter { 
    private LayoutInflater inflater; 
    private Context context; 

    public List(Context context) { 
     this.context = context; 
     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 

     return 50; 
    } 

    @Override 
    public Object getItem(int position) { 

     return position; 
    } 

    @Override 
    public long getItemId(int position) { 

     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      GalleryAdepter adepter; 
      adepter = new GalleryAdepter(context); 
      convertView = inflater.inflate(R.layout.raw_list, null); 
      BetterGallery betterGallery = (BetterGallery) convertView 
        .findViewById(R.id.betterGallery1); 
      betterGallery.setAdapter(adepter); 

     } 

     return convertView; 
    } 

} 

/** 画廊适配器 **/

public class GalleryAdepter extends BaseAdapter { 
    private LayoutInflater inflater; 
    private Context context; 
    public GalleryAdepter(Context context) { 
     this.context=context; 
     inflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 

     return 50; 
    } 

    @Override 
    public Object getItem(int position) { 

     return position; 
    } 

    @Override 
    public long getItemId(int position) { 

     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView= inflater.inflate(R.layout.raw_gallery, null); 
     } 
     return convertView; 
    } 

} 

/** 自定义库 **/

public class BetterGallery extends Gallery { 
    private boolean scrollingHorizontally = false; 

    public BetterGallery(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public BetterGallery(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public BetterGallery(Context context) { 
     super(context); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     super.onInterceptTouchEvent(ev); 
     return scrollingHorizontally; 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     scrollingHorizontally = true; 
     return super.onScroll(e1, e2, distanceX, distanceY); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     switch(event.getAction()) { 
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_CANCEL: 
      scrollingHorizontally = false; 
     } 

     return super.onTouchEvent(event); 
    } 
} 
0

2014 04 04 07:58根据user2009847添加的可能解决方案...

main.xml中

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

    <HorizontalScrollView 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"> 



      <TableLayout 
       android:id="@+id/tableLayout1" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:stretchColumns="0,1" > 

       <TableRow 
        android:id="@+id/tableRow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 

        <TextView 
         android:id="@+id/textView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="No." 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 
        <TextView 
         android:id="@+id/textView2" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="NAME" 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 

        <TextView 
         android:id="@+id/textView3" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="LOCATION" 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 
       </TableRow> 
      </TableLayout> 
     </HorizontalScrollView> 
    </ScrollView> 

SampleTableActivity.java

package com.sample.table; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.ColorStateList; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.Toast; 
import android.widget.TableRow.LayoutParams; 
import android.widget.TextView; 

public class SampleTableActivity extends Activity { 
    /** Called when the activity is first created. */ 


    SQLiteDatabase database; 
    private static String DBNAME = "sample.db";  
    private static String TABLE = "test"; 

    TableLayout tableLayout; 
    TableRow row; 
    TextView firstCol; 
    TextView secondCol; 
    TextView thirdCol; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tableLayout=(TableLayout)findViewById(R.id.tableLayout1); 

     createDB(); 
     insertValues(); 
     displayDB(); 
    } 


    private void displayDB() { 
     // TODO Auto-generated method stub 
     database=openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null); 
     if(database!=null) 
     { 
      Cursor cursor=database.rawQuery("SELECT * FROM "+ TABLE, null); 

      Integer index0=cursor.getColumnIndex("ID"); 
      Integer index1 = cursor.getColumnIndex("NAME"); 
      Integer index2 = cursor.getColumnIndex("LOCATION"); 
      if(cursor.getCount()>0) 
      { 
       cursor.moveToFirst(); 
       do 
       { 
        row=new TableRow(this); 
        row.setId(100); 
        row.setLayoutParams(new LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT)); 

        /*Setting up the first coloumn parameters*/ 
        firstCol=new TextView(this); 
        firstCol.setText(cursor.getString(index0)); 
        firstCol.setTextSize(24); 
        firstCol.setTextColor(Color.GREEN); 
        firstCol.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        row.addView(firstCol); //adding coloumn to row 

        /*Setting up the second coloumn parameters*/    
        secondCol=new TextView(this); 
        secondCol.setText(cursor.getString(index1)); 
        secondCol.setTextColor(Color.YELLOW); 
        secondCol.setTextSize(24); 
        secondCol.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        row.addView(secondCol); //adding coloumn to row 

        /*Setting up the third coloumn parameters*/ 
        thirdCol=new TextView(this); 
        thirdCol.setText(cursor.getString(index2)); 
        thirdCol.setTextColor(Color.MAGENTA); 
        thirdCol.setTextSize(24); 
        thirdCol.setLayoutParams(new LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT)); 
        row.addView(thirdCol); //adding coloumn to row 

        /*Adding the row to the tablelayout*/ 
        tableLayout.addView(row,new TableLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT)); 

       }while(cursor.moveToNext()); 
       database.close(); 
      } 
      else 
      { 
       Toast.makeText(getBaseContext(), "NOT AVAILABLE", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 


    private void insertValues() { 
     // TODO Auto-generated method stub 
     try 
     { 
      database=openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(1,'African Elephant','INDIA')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(2,'French Bulldog','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(3,'Giant Panda','Toronto')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(4,'African Elephant','Amundson-Scott Station')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(6,'French Bulldog','Charleston, WV')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(7,'Giant Panda','Miami')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(8,'African Elephant','Mexico City')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(9,'French Bulldog','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(10,'Great White Shark','Great Barrier Reef')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(11,'Giant Panda','Charleston, WV')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(12,'African Elephant','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(13,'Giant Panda','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(14,'French Bulldog','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(15,'African Elephant','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(16,'Giant Panda','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(17,'African Elephant','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(18,'French Bulldog','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(19,'Giant Panda','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(20,'African Elephant','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(21,'French Bulldog','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(22,'African Elephant','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(23,'French Bulldog','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(24,'African Elephant','Charleston, WV')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(25,'Giant Panda','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(26,'African Elephant','INDIA')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(27,'French Bulldog','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(28,'Giant Panda','Toronto')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(29,'African Elephant','Amundson-Scott Station')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(30,'French Bulldog','Charleston, WV')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(31,'Giant Panda','Miami')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(32,'African Elephant','Mexico City')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(33,'French Bulldog','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(34,'Great White Shark','Great Barrier Reef')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(35,'Giant Panda','Charleston, WV')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(36,'African Elephant','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(37,'Giant Panda','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(38,'French Bulldog','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(39,'African Elephant','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(40,'Giant Panda','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(41,'African Elephant','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(42,'French Bulldog','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(43,'Giant Panda','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(44,'African Elephant','Vladivostok, SI')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(45,'French Bulldog','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(46,'African Elephant','Brooklyn')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(47,'French Bulldog','Tokyo, JP')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(48,'African Elephant','Charleston, WV')"); 
      database.execSQL("INSERT INTO " + TABLE + "(ID,NAME, LOCATION) VALUES(49,'Giant Panda','Vladivostok, SI')"); 
      database.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return; 

    } 


    private void createDB() { 
     // TODO Auto-generated method stub 
     try 
     { 
      database=openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null); 
      database.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME TEXT, LOCATION TEXT);"); 
      database.close(); 
     } 
     catch(Exception e) 
     { 
      Log.e("Database Creation", "Error "+e.toString()); 
     } 
    } 
} 
相关问题