2013-04-29 85 views
2

我在Assets中有一個預填充的數據庫,它將在App啓動後複製並打開。 我有誰顯示與rawQueryAndroid自定義ListView與ArrayAdapter <String>可能嗎?

Cursor c = database.rawQuery("SELECT _id, Name from DB 
ORDER BY Name ASC", null); 

     ArrayList<String> values = new ArrayList<String>(); 
     while (c.moveToNext()) { 
      values.add(c.getString(c.getColumnIndex("Name"))); 

     } 
     c.close(); 

列名,並開始onItemClick一個新的意圖誰顯示其他colums(新活動得到與i.putExtra「名稱」變量)

private void getSQLData() { 
Bundle extras = getIntent().getExtras(); 
String Name = extras.getString("Name"); 

DataBaseHelper myDbHelper = new DataBaseHelper(null); 
myDbHelper = new DataBaseHelper(this); 
SQLiteDatabase database = myDbHelper.getWritableDatabase(); 
ListView lv = (ListView)findViewById(R.id.listView1); 

Cursor c = database.rawQuery("SELECT * from DB WHERE Name='"+Name+"' ORDER BY Name ASC", null); 

ArrayList<String> values = new ArrayList<String>(); 
    while (c.moveToNext()) { 
values.add(c.getString(c.getColumnIndex("Name"))); 
values.add(this.getString(R.string.x) + (c.getString(c.getColumnIndex("x"))+" "+this.getString(R.string.x)+":")); 
values.add((c.getString(c.getColumnIndex("y")) + this.getString(R.string.y) + " " + this.getString(R.string.y))); 
} 
c.close(); 
ArrayAdapter<String> NamenDetails = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, 
values); 
} 
lv.setAdapter(NamenDetails); } 

一個活動我得到了我需要的所有SQL數據,但在默認視圖中。 但我需要它定製像〔實施例:

我試着用自定義的ListView和simplecursoradapter很多很多的教程,但我認爲所有 由ArrayListString被擊敗。

我希望有人能幫助我,我感到很沮喪..

謝謝!

+0

你是什麼意思_all被ArrayListString_擊敗?你想使用'ArrayAdapter'還是'SimpleCursorAdapter'? – zbr 2013-04-29 13:50:43

回答

4

這是一個listview的例子,其單行有兩個textview。你想這樣的東西:

CustomListView.java:

package com.customlistview; 

import java.util.ArrayList; 

import resources.PlacesListAdapter; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ListView; 

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

    private ArrayList<String> mPlacesData1 = new ArrayList<String>(); 
    private ArrayList<String> mPlacesData2 = new ArrayList<String>(); 
    PlacesListAdapter mPLAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mPlacesData1.clear(); 
     mPlacesData2.clear(); 

     mPlacesData1.add("ICD1"); 
     mPlacesData2.add("SubTitle1"); 
     mPlacesData1.add("ICD2"); 
     mPlacesData2.add("SubTitle2"); 
     mPlacesData1.add("ICD3"); 
     mPlacesData2.add("SubTitle3"); 
     mPlacesData1.add("ICD4"); 
     mPlacesData2.add("SubTitle4"); 
     mPlacesData1.add("ICD5"); 
     mPlacesData2.add("SubTitle5"); 
     mPlacesData1.add("ICD6"); 
     mPlacesData2.add("SubTitle6"); 
     mPlacesData1.add("ICD7"); 
     mPlacesData2.add("SubTitle7"); 
     mPlacesData1.add("ICD8"); 
     mPlacesData2.add("SubTitle8"); 

     ListView listView = (ListView) findViewById(R.id.listview); 

     mPLAdapter = new PlacesListAdapter(CustomListView.this, mPlacesData1, mPlacesData2); 
     listView.setAdapter(mPLAdapter); 
    } 
} 

PlaceListAdapter.java:

package resources; 

import java.util.ArrayList; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import com.customlistview.R; 

public class PlacesListAdapter extends BaseAdapter { 
    // private Context mContext; 
    private LayoutInflater mInflater; 
    private ArrayList<String> AL_id_text = new ArrayList<String>(); 
    private ArrayList<String> AL_text = new ArrayList<String>(); 

    public PlacesListAdapter(Context c, ArrayList<String> AL_name_time, 
      ArrayList<String> AL_name_time1) { 
     mInflater = LayoutInflater.from(c); 
     // mContext = c; 
     this.AL_id_text = AL_name_time; 
     this.AL_text = AL_name_time1; 
    } 

    public int getCount() { 
     return AL_id_text.size(); 
    } 

    public Object getItem(int position) { 
     return AL_id_text.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder holder; 
     if (convertView == null) 
     { 
      convertView = mInflater.inflate(R.layout.place_row, null); 
      holder = new ViewHolder(); 

      holder.txt_maintext = (TextView) convertView 
        .findViewById(R.id.txt_maintext); 
      holder.txt_mtext = (TextView) convertView 
        .findViewById(R.id.txt_mtext); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.txt_maintext.setText(AL_id_text.get(position)); 
     holder.txt_mtext.setText(AL_text.get(position)); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView txt_maintext; 
     TextView txt_mtext; 
    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/listview"> </ListView> </LinearLayout> 

place_row.xml:

<?xml version="1.0" encoding="UTF-8"?> 
-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> -<LinearLayout android:orientation="vertical" android:layout_height="70dip" android:layout_width="match_parent" android:id="@+id/lin_main"> <TextView android:layout_height="20dip" android:layout_width="fill_parent" android:id="@+id/txt_maintext" android:singleLine="true" android:paddingRight="5dip" android:paddingLeft="5dip" android:layout_marginTop="5dip" android:textColor="#fff"/> <TextView android:layout_height="20dip" android:layout_width="fill_parent" android:id="@+id/txt_mtext" android:singleLine="true" android:paddingRight="5dip" android:paddingLeft="5dip" android:layout_marginTop="15dip" android:textColor="#fff"/> </LinearLayout> <ImageView android:layout_height="3dip" android:layout_width="match_parent" android:background="#0000ff"/> </LinearLayout> 

這是一個例子。您可以進行必要的編輯以達到您想要的效果。

+0

它工作!必須添加一個新的遊標。非常感謝! – Oli 2013-04-29 14:49:17

+0

不客氣,朋友。 – 2013-04-30 05:58:53

+0

非常感謝您的支持!我正在做一個類似於這個問題的項目,你剛剛解決了我最大的問題之一!謝謝。 – 2014-08-12 19:19:19