2011-04-25 60 views
2

我需要怎么改变我下面的代码,用我自己的XML列表视图的帮助。该项目在我的光标“标签”,“标题”,&“详细描述”需要被膨胀成itemLabel,itemTitle中,XML的& itemDiscription。任何帮助将不胜感激:我知道如何从一个简单的数组做到这一点。我为从数据库中获取数据而创建的活动效果很好 - 我只是不知道如何使用/显示具有multilines的自定义lisView。日Thnx!ListView控件和定制的ListView

修订:我设法获取数据使用自己的自定义XML文件,从数据库中显示。我现在的问题是每个TextView都返回三个列。即:从数据库中的列'标题','标签','描述'都作为一个连续的线膨胀成一个单独的TextView。我无法弄清楚如何将它分解成正确的TextView; title = R.id.listTitle,label = R.id.label,描述应该膨胀到R.id.caption中。帮帮我!

的ListView的活动:(修订版):

public class List_AC extends ListActivity { 

private ArrayList<String> results = new ArrayList<String>(); 

File dbfile = new File("/mnt/sdcard/XXX/XXX/dB/XXX.db"); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    openAndQueryDatabase(); 
    displayResultList(); 
} 

private void displayResultList() { 
    AC_Adapter adapter = new AC_Adapter(getApplicationContext(),R.layout.list_item, results, results, results); 
    setListAdapter(adapter); 
} 

private void openAndQueryDatabase() { 
    try { 
     Cursor c = db.rawQuery("SELECT label, title, discription FROM AC_list", null); 
     if (c != null) { 
      if (c.moveToFirst()) { 
       do { 
        String i1 = c.getString(c.getColumnIndex("label")); 
        String i2 = c.getString(c.getColumnIndex("title")); 
        String i3 = c.getString(c.getColumnIndex("discription")); 
        results.add(i1 + i2 + i3); 
       } while (c.moveToNext()); 
      } 
     } 
    } catch (SQLiteException se) { 
     Log.e(getClass().getSimpleName(), 
       "Could not create or Open the database"); 
    } finally { 
     if (db != null) 
      db.close(); 
    } 
} 

}

添加的适配器(AC_Adapter.java):

public class AC_Adapter extends ArrayAdapter<String> { 
static List<String> Title = new ArrayList<String>(); 
static List<String> Label = new ArrayList<String>(); 
static List<String> Description = new ArrayList<String>(); 

Context myContext; 

public AC_Adapter (Context context, int resource, List<String> aTitle, List<String> aLabel, List<String> aDescription){ 

    super(context, resource, aTitle); 

    myContext = context; 
    Title= aTitle; 
    Label= aLabel; 
    Description = aDescription; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater) myContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.list_item, null); 
    } 

    TextView tv_label = (TextView) v.findViewById(R.id.label); 
    TextView tv_title = (TextView) v.findViewById(R.id.listTitle); 
    TextView tv_decription = (TextView) v.findViewById(R.id.caption); 

    if (tv_label != null) { 
     tv_label.setText(Label.get(position)); 
    } 
    if (tv_title != null) { 
     tv_title.setText(Title.get(position)); 
    } 
    if (tv_decription != null) { 
     tv_decription.setText(Description.get(position)); 
    } 

    return v; 

} 

}

这是需要的XML要创建(list_view.xml)在OnCreate:

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

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="30dip" 
    android:padding="4dip" 
    android:background="@drawable/gradient" > 

    <ImageButton 
     android:id="@+id/homeBtn" 
     android:src="@drawable/ic_menu_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:background="@null" /> 

    <TextView 
     android:id="@+id/titleBarTitle" 
     android:layout_centerInParent="true" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:textSize="18sp" /> 

    <ImageButton 
     android:id="@+id/toolBtn" 
     android:src="@drawable/ic_menu_list" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:background="@null" /> 

</RelativeLayout> 

<ListView 
    android:id="@+id/listItems" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" /> 

</LinearLayout> 

和列表项(list_item.xml):

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
    android:id="@+id/itemLabel" 
    style="@style/listAcronym" /> 

<TextView 
    android:id="@+id/itemTitle" 
    style="@style/listTitle" /> 

<TextView 
    android:id="@+id/itemDiscription" 
    style="@style/listDiscription"/>   

<ImageView 
    style="@style/listNextIcon" /> 

回答

2

的基本过程是创建一个定制适配器其中将包含布局R.layout .list_item

因此,更换这行setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results)); 01在你的代码通过

setListAdapter(new CustomAdapter(this,R.layout.list_item, results)); 

现在,您需要创建延伸ArrayAdapter或BaseAdapter一个CustomAdapter并重写getView方法膨胀R.layout.list_item

请马克·墨菲参阅本优秀教程 Custom ListView Adapter

如果您在尝试此操作后还有任何疑问,请在此处张贴。

+0

我修改了上面的代码-PLZ c REVISED在我的Q. Thx @chaitanya – CelticParser 2011-04-26 17:08:12

+0

你可能想要传递Activity而不是使用getApplicationContext(),你可以帮我一个忙,只需将你的项目压缩并发送给我一个链接,我可以修复它并在这里发布答案。此外,使用活动的LayoutInflater而不是上下文 – chaitanya 2011-04-26 18:27:22

+0

Thx @chaitanya这里是链接到zip。让我知道你什么时候有,所以我可以删除这条评论。此外,我只发送了此活动的支持文件/结构。数据库位于资产文件夹中,您必须手动将其放在仿真器SD卡下的“extStorageDirectory +”/xxx/xxx/dB/flyDroid.db“我必须从项目中省略尽可能多的数据,一些虚假的信息进入数据库(这是一个政府项目)抱歉。提前感谢您的帮助 - 我有很多东西要学习...... – CelticParser 2011-04-26 19:41:02

2

一切都很好,只需添加一个类从ArrayAdapter延伸,做这样的事情:

public class CustomAdapter extends ArrayAdapter<String> 
{ 
    List<String> Title= new ArrayList<String>(); 
    List<String> Label= new ArrayList<String>(); 


    Context myContext; 


public CustomAdapter (Context context, int resource, 
               int textviewresourceid, 
               List<String> aTitle, 
               List<String> aLabel) 
{ 


    super(context, resource,textviewresourceid,aType); 

    myContext = context; 
    Title= aTitle; 
    Label= aLabel; 



} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View v = convertView; 
    if (v == null) 
    { 
      LayoutInflater vi = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.list_view, null); 
    } 

    TextView tv_title = (TextView) v.findViewById(R.id.id_tv_Title); 
    TextView tv_Label= (TextView) v.findViewById(R.id.id_tv_Label); 


    if(tv_title != null) 
    { 
     tv_title.setText(Title.get(position)); 
    } 
    if(tv_Label != null) 
    { 
     tv_Label.setText(Label.get(position)); 
    } 

    return v; 


} 

然后使用这个适配器,如:

CustomAdapter adapter = new CustomAdapter(getAppContext(),R.layout.list_view,Textview Title Id,your lists...); 

setListAdapter(adapter); 

事情是这样的....希望它可以帮助...

+0

首先我要感谢您 - 为您快速回复:@Farhan和@chaitanya(对不起,我不能早点回复)我跟着你的建议,以及我知道如何和活动显示w/o强制关闭或任何DDMS错误,但没有数据返回(空白屏幕)我是我确定我有什么不对的地方,所以如果你能看看,我会很感激的。我在上面的Q做了修改。Thnx。 – CelticParser 2011-04-26 15:02:33

+0

我修改了上面的代码--PLZ c修改了我的Q. Thx @Farhan – CelticParser 2011-04-26 17:07:55

+0

一个错误:在适配器中传递结果3次,而你期待3个不同的列表...所以你为什么不打破水库将其转换为3个差异列表并将其传递给适配器。 – Farhan 2011-04-26 18:40:21