ListView & Custom ListView

人盡茶涼 提交于 2019-12-24 01:45:11

问题


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!

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!

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

}

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;

}

}

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>

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" />   


回答1:


The basic process is to create a Custom Adapter which will contain the layout R.layout.list_item

Thus, you replace this line setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results)); in your code by

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

Now you need to create a CustomAdapter which extends ArrayAdapter or BaseAdapter and override the getView method to inflate R.layout.list_item

Please refer to this excellent tutorial by Mark Murphy Custom ListView Adapter

If you do have any other doubts after trying this, please post it over here.




回答2:


Everything is fine, just add a class extends from ArrayAdapter and do something like this:

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;


}

And then use this adapter like:

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

setListAdapter(adapter);

Something like this.... Hope it helps...




回答3:


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



来源:https://stackoverflow.com/questions/5781776/listview-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!