如何显示从ArrayList中&LT特定数据; GetterSetter>反对的ListView? [英] How to display particular data from ArrayList<GetterSetter> object to ListView?

查看:105
本文介绍了如何显示从ArrayList中&LT特定数据; GetterSetter>反对的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从.sqlite获取数据并显示到ListView中!

对于实施GetterSetter类用于获取和利用该方法与功放设置ArrayList中的数据;我成功地打印值得到了数据库控制台:

 公开的ArrayList< GS>的getData()
{
    尝试{
    游标c1 = sdb.rawQuery(SELECT DISTINCT * FROM章程,NULL);
    GS =新的ArrayList< GS>();
    而(c1.moveToNext())
    {
        GS Q1 =新GS();        q1.setId(c1.getString(0));
        q1.setA_id(c1.getString(1));
        q1.setA_name(c1.getString(2));
        q1.setAS_name(c1.getString(3)); // - 我想这种类型的列表是在ListView--
        q1.setDesc_art(c1.getString(4));
        Log.v(ID,q1.AS_name +);
        gs.add(Q1);
    }
    }
    赶上(例外五){
        e.printStackTrace();
    }
    返回GS;
}

我把这种方法是:

 的ArrayList< GS> Q = db.getData();

所以C $ C应实行什么$,在ListView中显示呢?

*的更新* 的:activity_main.xml中

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>< ListView控件
    机器人:ID =@ + ID / ListView1的
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真正的>
< /&的ListView GT;< / RelativeLayout的>

customlayout.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=TextView的/>< / LinearLayout中>

DBAdapter.java

 公共类DBAdapter扩展SQLiteOpenHelper
 {
CustomAdapter适配器;
静态字符串名称=law.sqlite;
静态字符串路径=;
静态的ArrayList< GS> GS;
静态SQLiteDatabase SDB;@覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
    // TODO自动生成方法存根
}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
    // TODO自动生成方法存根
}私人DBAdapter(上下文V)
{
    超级(ⅴ,名称,无效,1);
    PATH =/数据/数据​​/+ v.getApplicationContext()getPackageName()+/数据库。
}公共布尔checkDatabase()
{
    SQLiteDatabase DB = NULL;
    尝试
    {
        DB = SQLiteDatabase.openDatabase(路径+/+名称,空,SQLiteDatabase.OPEN_READONLY);
    }赶上(例外五)
    {
        e.printStackTrace();
    }
    如果(DB == NULL)
    {
        返回false;
    }
    其他
    {
        db.close();
        返回true;
    }
}公共静态同步DBAdapter getDBAdapter(上下文V)
{
    返回(新DBAdapter(ⅴ));
}公共无效的CreateDatabase(上下文V)
{
    this.getReadableDatabase();
    尝试
    {
        InputStream的myInput = v.getAssets()开(名称)。
        //路径刚刚创建的空分贝
    字符串outFileName =路径+/+名;
        //打开空分贝的输出流
    的OutputStream myOutput =新的FileOutputStream(outFileName);
        //传递从inputfile中字节到OUTPUTFILE
    字节[]缓冲区=新的字节[1024];
    INT长;
    而((长度= myInput.read(缓冲液))大于0)
    {
        myOutput.write(缓冲液,0,长度);
    }
        //关闭流
    myOutput.flush();
    myOutput.close();
    myInput.close();    / *        InputStream为= v.getAssets()开(quiz.sqlite);
        //的System.out.println(is.available());
        的System.out.println(新文件(路径+/+姓名).getAbsolutePath());
        FileOutputStream中FOS =新的FileOutputStream(路径+/+名称);
        INT NUM = 0;
        而((NUM = is.​​read())0){
            fos.write((字节)NUM);
        }
        fos.close();
        is.close(); * /
    }赶上(IOException异常E)
    {
        的System.out.println(E);
    }
}公共无效的openDatabase()
{
    尝试
    {
        SDB = SQLiteDatabase.openDatabase(路径+/+姓名,空,
                SQLiteDatabase.OPEN_READWRITE);
    }赶上(例外五)
    {
        的System.out.println(E);
    }
}公众的ArrayList< GS>的getData()
{
    尝试{
    游标c1 = sdb.rawQuery(SELECT DISTINCT * FROM章程,NULL);
    GS =新的ArrayList< GS>();
    而(c1.moveToNext())
    {
        GS Q1 =新GS();        q1.setId(c1.getString(0));
        q1.setA_id(c1.getString(1));
        q1.setA_name(c1.getString(2));
        q1.setAS_name(c1.getString(3));
        q1.setDesc_art(c1.getString(4));
        Log.v(ID,q1.AS_name +);
        gs.add(Q1);    }
    }
    赶上(例外五){
        e.printStackTrace();
    }
    返回GS;
}
}

MainActivity.java

 公共类MainActivity延伸活动{ArrayList的< GS> Q =新的ArrayList< GS>();
CustomAdapter适配器;
LV的ListView;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
       //从XML的ListView对象
     LV =(ListView控件)findViewById(R.id.listView1);    DBAdapter分贝= DBAdapter.getDBAdapter(getApplicationContext());
    如果(!db.checkDatabase())
    {
        db.createDatabase(getApplicationContext());
    }
    db.openDatabase();    Q = db.getData();
    的for(int i = 0; I< q.size();我++)
    {
        Log.i(外,+ q.get(ⅰ).getAS_name());
    }
    LV =(ListView控件)findViewById(R.id.listView1);
    lv.setAdapter(新CustomAdapter(MainActivity.this,Q));
    lv.setAdapter(适配器);}类CustomAdapter扩展ArrayAdapter< GS>
  {
       ArrayList的< GS>清单;
       LayoutInflater mInfalter;
       公共CustomAdapter(上下文的背景下,ArrayList的< GS>清单)
       {
          超(背景下,R.layout.customlayout,清单);
          this.list =清单;
          mInfalter = LayoutInflater.from(上下文);
        的for(int i = 0; I< q.size();我++)
        {
            Log.i(................,+ list.get(ⅰ).getAS_name());
        }       }
       @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
          ViewHolder持有人;
          Log.i(..........,你好在getView);
          如果(convertView == NULL)
          {
               convertView = mInfalter.inflate(R.layout.customlayout,父母,假);
               持有人=新ViewHolder();
               holder.tv1 =(TextView中)convertView.findViewById(R.id.textView1);
               convertView.setTag(保持器);
          }其他{
                支架=(ViewHolder)convertView.getTag();
          }                holder.tv1.setText(list.get(位置).getAS_name());
          返回convertView;
    }  }
 静态类ViewHolder
    {
        TextView的TV1;
    }


解决方案

 的String []值=新的String [q.size()];
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
      android.R.layout.simple_list_item_1,android.R.id.text1,价值观);

但是,在你填写物品价值阵列。我不明白,在你的code。

您可以使用自定义的ListView使用自定义的适配器

 的ArrayList< GS> Q = db.getData();
 LV的ListView =(ListView控件)findViewById(R.id.listView);
 lv.setAdapter(新CustomAdapter(MainActivity.this,Q));

CustomAdapter

 类CustomAdapter扩展ArrayAdapter< GS>
  {
       ArrayList的< GS>清单;
       LayoutInfalter mInfalter;
       公共CustomAdapter(上下文的背景下,ArrayList的< GS>清单)
       {
          超(背景下,R.layout.customlayout,清单);
          this.list =名单
          mInfalter = LayoutInfalter.from(上下文);
       }
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
          ViewHolder持有人;
          如果(convertView == NULL)
          {
               convertView = mInflater.inflate(R.layout.customlayout,父母,假);
               持有人=新ViewHolder();
               holder.tv1 =(TextView中)convertView.findViewById(R.id.textView1);
               convertView.setTag(保持器);
          }其他{
                支架=(ViewHolder)convertVire.getTag();
          }                holder.tv1.setText(list.get(位置).getAS_name());
          返回convertVIew;
    }
    静态类ViewHolder
    {
        TextView的TV1;
    }
  }

有一个的TextView customlayout.xml ID textview1。

或者

使用 SimpleCursorAdapter 将是在这种情况下适当的。

I am trying to fetch data from .sqlite and display it into the ListView !

For that implemented a GetterSetter class for getting and setting the data in the ArrayList by this method & i was successful in printing the values got from database to console :

public ArrayList<GS> getData() 
{
    try{
    Cursor c1 = sdb.rawQuery("SELECT DISTINCT * FROM Articles", null);
    gs = new ArrayList<GS>();
    while (c1.moveToNext())
    {
        GS q1 = new GS();

        q1.setId(c1.getString(0));
        q1.setA_id(c1.getString(1));
        q1.setA_name(c1.getString(2));
        q1.setAS_name(c1.getString(3));  //--I want this type of list to be in ListView--
        q1.setDesc_art(c1.getString(4));
        Log.v("id",q1.AS_name+"");
        gs.add(q1);
    }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return gs;
}

I call this method by :

ArrayList<GS> q = db.getData();

So what code should be implemented, to display it in ListView ?

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</ListView>

</RelativeLayout>

customlayout.xml

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

DBAdapter.java

 public class DBAdapter extends SQLiteOpenHelper
 {
CustomAdapter adapter;
static String name = "law.sqlite";
static String path = "";
static ArrayList<GS> gs;
static SQLiteDatabase sdb;

@Override
public void onCreate(SQLiteDatabase db)
{
    // TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    // TODO Auto-generated method stub
} 

private DBAdapter(Context v) 
{
    super(v, name, null, 1);
    path = "/data/data/" + v.getApplicationContext().getPackageName() + "/databases";
}

public boolean checkDatabase()
{
    SQLiteDatabase db = null;
    try 
    {
        db = SQLiteDatabase.openDatabase(path + "/" + name, null, SQLiteDatabase.OPEN_READONLY);
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
    if (db == null) 
    {
        return false;
    } 
    else
    {
        db.close();
        return true;
    }
}

public static synchronized DBAdapter getDBAdapter(Context v)
{
    return (new DBAdapter(v));
}

public void createDatabase(Context v) 
{
    this.getReadableDatabase();
    try
    {
        InputStream myInput = v.getAssets().open(name);
        // Path to the just created empty db
    String outFileName = path +"/"+ name;
        // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) 
    {
        myOutput.write(buffer, 0, length);
    }
        // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

    /*  



        InputStream is = v.getAssets().open("quiz.sqlite");
        // System.out.println(is.available());
        System.out.println(new File(path + "/" + name).getAbsolutePath());
        FileOutputStream fos = new FileOutputStream(path + "/" + name);
        int num = 0;
        while ((num = is.read()) > 0) {
            fos.write((byte) num);
        }
        fos.close();
        is.close();*/
    } catch (IOException e) 
    {
        System.out.println(e);
    }
}

public void openDatabase() 
{
    try 
    {
        sdb = SQLiteDatabase.openDatabase(path + "/" + name, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (Exception e) 
    {
        System.out.println(e);
    }
}

public ArrayList<GS> getData() 
{
    try{
    Cursor c1 = sdb.rawQuery("SELECT DISTINCT * FROM Articles", null);
    gs = new ArrayList<GS>();
    while (c1.moveToNext())
    {
        GS q1 = new GS();

        q1.setId(c1.getString(0));
        q1.setA_id(c1.getString(1));
        q1.setA_name(c1.getString(2));
        q1.setAS_name(c1.getString(3));
        q1.setDesc_art(c1.getString(4));
        Log.v("id",q1.AS_name+"");
        gs.add(q1);

    }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return gs;
}
}

MainActivity.java

public class MainActivity extends Activity {

ArrayList<GS> q = new ArrayList<GS>();
CustomAdapter adapter;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       // Get ListView object from xml
     lv = (ListView) findViewById(R.id.listView1);

    DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
    if (!db.checkDatabase()) 
    {
        db.createDatabase(getApplicationContext());
    }
    db.openDatabase();

    q = db.getData();
    for(int i=0;i<q.size();i++)
    {
        Log.i("outside",""+q.get(i).getAS_name());
    }
    lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(new CustomAdapter(MainActivity.this,q));
    lv.setAdapter(adapter);

}

class CustomAdapter extends ArrayAdapter<GS>
  {
       ArrayList<GS> list;
       LayoutInflater mInfalter;    
       public CustomAdapter(Context context, ArrayList<GS> list)
       {
          super(context,R.layout.customlayout,list);
          this.list= list;  
          mInfalter = LayoutInflater.from(context);
        for(int i=0;i<q.size();i++)
        {
            Log.i("................",""+list.get(i).getAS_name());
        }

       }
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          Log.i("..........","Hello in getView");
          if(convertView==null)
          {
               convertView = mInfalter.inflate(R.layout.customlayout,parent,false);
               holder = new ViewHolder();
               holder.tv1 = (TextView)convertView.findViewById(R.id.textView1); 
               convertView.setTag(holder); 
          }else{
                holder = (ViewHolder)convertView.getTag();
          } 

                holder.tv1.setText(list.get(position).getAS_name());
          return convertView;
    }

  }
 static class ViewHolder
    {
        TextView tv1;
    }  

解决方案

You have

String[] values = new String[q.size()]; 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, android.R.id.text1, values);

But where do you populate items to values array. I don't see that in your code.

You can use Custom ListView with a Custom Adapter

 ArrayList<GS> q = db.getData();
 ListView lv = (ListView) findViewById(R.id.listView);
 lv.setAdapter(new CustomAdapter(MainActivity.this,q));

CustomAdapter

class CustomAdapter extends ArrayAdapter<GS>
  {
       ArrayList<GS> list;
       LayoutInfalter mInfalter;    
       public CustomAdapter(Context context, ArrayList<GS> list)
       {
          super(context,R.layout.customlayout,list);
          this.list= list  
          mInfalter = LayoutInfalter.from(context);
       }   
        public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if(convertView==null)
          {
               convertView = mInflater.inflate(R.layout.customlayout,parent,false);
               holder = new ViewHolder();
               holder.tv1 = (TextView)convertView.findViewById(R.id.textView1); 
               convertView.setTag(holder); 
          }else{
                holder = (ViewHolder)convertVire.getTag();
          } 

                holder.tv1.setText(list.get(postion).getAS_name());
          return convertVIew;
    }
    static class ViewHolder
    {
        TextView tv1;
    }    
  }

Have a TextView with id textview1 in customlayout.xml.

Or

Using SimpleCursorAdapter would be appropriate in this case.

这篇关于如何显示从ArrayList中&LT特定数据; GetterSetter&GT;反对的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆