如何将simple_list_item_1更改为自定义列表?

时间:2014-03-05 06:19:46

标签: android sqlite android-listview

我编写此代码用于从DataBase获取数据并在listView中显示。我想在我的自定义列表中显示我的数据,包含3个textView和1个ImageView。我怎么能这样做?

这是我的代码:

private void Name() {
        ListView tvName = (ListView) findViewById(R.id.lvListOfObject);
        DataBase infoName = new DataBase(this);
        infoName.open();
        ArrayList<String> dataName = infoName.getDataName();
        infoName.close();
        tvName.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, dataName));

这是我的数据库:

package com.kalagar.warehouse;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBase {

    private static final String LOGTAG = "WAREHOUSE";

    private static final String DATABASE_TABLE = "WareHouse";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "SellList";

    public static final String ROW_ID = "_id";
    public static final String ROW_NAME = "nameOfObject";
    public static final String ROW_KHARID = "ghBuy";
    public static final String ROW_FOROUSH = "ghSell";
    public static final String ROW_PICTURE = "picture";

    private static final String TABLE_CREATE = "CREATE TABLE " + DATABASE_TABLE
            + " (" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAME
            + " TEXT, " + ROW_KHARID + " NUMERIC, " + ROW_FOROUSH
            + " NUMERIC, " + ROW_PICTURE + " TEXT " + ")";

    private WareHouseDdbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class WareHouseDdbHelper extends SQLiteOpenHelper {

        public WareHouseDdbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TABLE_CREATE);
            Log.i(LOGTAG, "Table has been create");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);
        }

    }

    public DataBase(Context c) {
        ourContext = c;
    }

    public DataBase open() throws SQLException {
        ourHelper = new WareHouseDdbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        ourHelper.close();
    }

    public long createEntry(String name, String kharid, String foroush) {
        ContentValues cv = new ContentValues();
        cv.put(ROW_NAME, name);
        cv.put(ROW_KHARID, kharid);
        cv.put(ROW_FOROUSH, foroush);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public ArrayList<String> getDataName() {
        String[] columns = new String[] { ROW_ID, ROW_NAME, ROW_KHARID,
                ROW_FOROUSH };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        ArrayList<String> result = new ArrayList<String>();

        int iRow = c.getColumnIndex(ROW_ID);
        int iName = c.getColumnIndex(ROW_NAME);
        int iKharid = c.getColumnIndex(ROW_KHARID);
        int iForoush = c.getColumnIndex(ROW_FOROUSH);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result.add(c.getString(iRow) + " " + c.getString(iName) + " "
                    + c.getString(iKharid) + " " + c.getString(iForoush));
        }

        return result;
    }
}

3 个答案:

答案 0 :(得分:0)

为此你需要一个自定义列表视图首先你应该在main.xml(只是一个例子)文件中添加一个listview然后创建一个这样的类

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      private final Context context;
      private final String[] values;
      DataHelper dh;

      public MySimpleArrayAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;

        dh=new DataHelper(getApplicationContext());
      }

      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_name, parent, false);
       textView = (TextView) rowView.findViewById(R.id.textname);

        textView.setText(values[position]);
        // Change the icon for Windows and iPhone
        textView.setOnClickListener(new View.OnClickListener() {
               public void onClick(View v) 
               {
                   Toast.makeText(this,""+values[position],10000).show();
               }
               });


        return rowView;
} 
}

R.layout.list_name这将是新的xml文件,它将内容加载到列表视图

并且你的on create方法的最后一步就是这样做

con = (ListView) findViewById(R.id.main_listView); 
 MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(MainActivity.this,  R.id.main_listView ,data);// data is String array valu to be added in list view
    //setting the adapter
    con.setAdapter(adapter);

来源link

答案 1 :(得分:0)

您可以使用自定义列表视图适配器。以下是一些示例。

Android Custom ListView with Image and Text

Using lists in Android (ListView) - Tutorial

答案 2 :(得分:0)

请按照以下步骤操作:

  1. 为列表视图的列表项创建XML。
  2. 为那些将显示为列表项的数据创建一个POJO类。
  3. 使用该XML创建自定义适配器。使用BaseAdapter创建自定义适配器。
  4. 假设您已创建名为CustomAdapter.java的自定义适配器和名为POJO的{​​{1}}类。现在将此适配器分配到RowItem.java,如下所示......

    Listview
  5. 您可以按照本教程...

    Android: Custom ListView with Image and Text using BaseAdapter

    ListView using BaseAdapter – Android