ListView控件和放大器;自定义的ListView [英] ListView & Custom ListView

查看:143
本文介绍了ListView控件和放大器;自定义的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要如何改变我下面的code用我自己的XML列表视图的帮助。该项目标签,标题,&安培;在我的光标详细描述需要被膨胀成itemLabel,itemTitle,&安培;在XML的itemDiscription。任何帮助将是AP preciated:我知道如何从一个简单的数组做到这一点。活动我创建获取数据从数据库的伟大工程 - 我只是不知道如何使用/显示自定义lisView瓦特/多线。日Thnx!

I need help on how to change my code below to use my own XML list view. The items 'label', 'title', & 'discription' in my cursor needs to be inflated into itemLabel, itemTitle, & itemDiscription of the xml. Any help would be appreciated: I know how to do this from a simple array. The activity I created to get the data from a database works great - I just dont know how to use/display a custom lisView w/multilines. THNX!

修订版:我设法获取数据使用自己的自定义XML文件,从数据库中显示。我现在的问题是所有三列在每个TextView中返回。即:列'标题','标签',从数据库中的所有说明膨胀成一个单一的TextView作为一个连续的线。我无法弄清楚如何打破它到正确的TextViews;标题= R.id.listTitle,标签= R.id.label和描述应该夸大成R.id.caption。救命啊!

REVISED: I managed to get the data to display from the database using my own custom XML file. The issue I have now is all three columns are returned in each TextView. ie: columns 'title', 'label', 'description' from the db all inflate into a single TextView as one continuous line. I cant figure out how to break it up into the correct TextViews; title = R.id.listTitle, label = R.id.label, and description should inflate into R.id.caption. Help!

ListView控件活动:(修订版):

The ListView Activity:(REVISED):

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

ADDED ADAPTER(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;

}

}

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

This is the XML that needs to be created (list_view.xml) in the 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):

and the list item (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" />   

推荐答案

我有正确的anwser 这里的的一起正确的code。

I have the correct anwser HERE along with the correct code.

这篇关于ListView控件和放大器;自定义的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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