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

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

问题描述

我正在尝试从 .sqlite 获取数据并将其显示到 ListView 中!

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

为此实现了一个 GetterSetter 类,用于通过此方法获取和设置 ArrayList 中的数据 &我成功地将从数据库获得的值打印到控制台:

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();

那么应该实现什么代码,以在 ListView 中显示它?

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

*更新 *:activity_main.xml

*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

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

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

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

推荐答案

你有

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

但是您在哪里将项目填充到值array.我在您的代码中没有看到这一点.

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

您可以将自定义 ListView 与自定义适配器一起使用

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

自定义适配器

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

customlayout.xml 中有一个 ID 为 textview1 的 TextView.

Have a TextView with id textview1 in customlayout.xml.

在这种情况下使用 SimpleCursorAdapter 是合适的.

Using SimpleCursorAdapter would be appropriate in this case.

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

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