开发者

Populating ListView from Cursor

I've been looking on here for quite some time and I cannot find an answer to my problem. There are many similar issues. But I haven't been able to come up with any solution that works. I am very new to android/java programming so I really appreciate any help.

Background: I have an activity screen that contains 2 listviews. The first listview contains a list of names coming from a database. (This is working correctly).

Then when a user clicks on one of the names in the list, the onItemClick event fires and builds a cursor from all the data associated from the name. (Have verified that I am getting a cursor with valid results).

Problem: When I try to put the resulting cursor into a SimpleCursorAdapter to set my listadpter to, I am receiving no errors, but I'm not getting anything displayed to the screen.

I have data coming from a SQLite database. It is simply 4 columms with X number of rows. All I want to do is display everything on the list and then maybe do some analysis.

Here is my code:

public class TipCalculator_w_Database extends Activity    {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.databaseinfoscreen);

    ListView lv1 = null;
    lv1 = (ListView) findViewById (R.id.List1);
    final ListView lv2;
    lv2 = (ListView) findViewById (R.id.List2);
开发者_JS百科

    final DataBase showData = new DataBase(getApplicationContext());
    showData.open();

    Cursor NameList = showData.GetAllNames();
    startManagingCursor(NameList);

    // Create an ArrayAdapter, that will actually make the Strings above
    // appear in the ListView
        int i = NameList.getCount();
        String[] FinalString = new String[i];
        int j = 0;
        while (NameList.moveToNext()) {

                FinalString[j] = NameList.getString(0);
                j++;
        }
        if(j != 0)
        {
            lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, FinalString));  
        }
        NameList.close();
        showData.close();

        lv1.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              String selectedName = ((TextView) view).getText().toString();
              Context ctx= parent.getContext();
              final DataBase showData = new DataBase(getApplicationContext());
              showData.open();
            final String[] columns = new String[] { showData.KEY_ROWID, showData.KEY_NAME, showData.KEY_BILL, showData.KEY_TIP};
            final int[] to = new int[] { R.id.ID_list, R.id.Name_list, R.id.Bill_list, R.id.Tip_list};

              Cursor NewEntry = showData.GetRowByName(selectedName);
              startManagingCursor(NewEntry);

                lv2.setAdapter(new SimpleCursorAdapter(ctx, R.layout.listlayout, NewEntry, columns, to));

              showData.close();
              NewEntry.close();

    // Back Button Functionality
    Button backButton = (Button) findViewById(R.id.Back);   
    backButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
     Intent myIntent = new Intent(v.getContext(), Tipscreen.class);
     startActivityForResult(myIntent, 0);
    }
}
);
}

}

Here is my XML Files

listlayout.xml

 <?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="wrap_content" >
        <TextView
            android:id="@+id/ID_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10px" />
        <TextView
            android:id="@+id/Name_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10px" />
           <TextView
            android:id="@+id/Bill_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10px" />
           <TextView
            android:id="@+id/Tip_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10px"/> </LinearLayout>

-- databaseinfoscreen.xml --

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
<ListView android:text="List" android:id="@+id/List1" android:layout_width="wrap_content" android:textSize="18px" android:layout_height="fill_parent">
</ListView>  
  </LinearLayout>
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
<ListView android:text="List" android:id="@+id/List2"  android:layout_width="wrap_content" android:textSize="18px" android:layout_height="fill_parent">
</ListView>  
  </LinearLayout>
  <RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
     <Button android:text="@string/back" android:layout_alignParentBottom="true" android:id="@+id/Back" android:layout_width="wrap_content" android:textSize="18px" android:layout_height="40px"></Button>  
    </RelativeLayout>
</LinearLayout>

Hopefully this is enough to go off of, if there is any more information needed please let me know. I am getting all the information I need in the cursor it just isn't being displayed. I am open to alternate means of doing this. However, I cannot make this particular class extend ListActivity since it needs to be extending activity.

Also of note. I have been able to display to that listview using the following line of code. Unfortunately this is only displays one column rather than all columns which I want.

lv2.setAdapter(new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, FinalString));

Where FinalString is simply a full column of data from the database that was manipulated into an array.

Thanks for the help.


Well since no one responded, I figured I'd answer this myself.

Here are the steps, I've got this working for a different project so I'll summarize. I can go into more detail if anyone needs it.

XML:

<ListView android:text="List" android:id="@+id/android:list" android:layout_width="wrap_content" android:scrollbars="horizontal" android:textSize="18px" android:layout_height="fill_parent" android:minHeight="120px">
</ListView>  

Step 1: Create a Cursor and populate using DB functions (not shown)

    String dbCount = showData.getTotalCount();
    Cursor GetAllData = showData.GetAllData();
    startManagingCursor(GetAllData);

Step 2: Create an ArrayList that comprises a bunch of get values and fill the list with items from the cursor.

        int i = GetAllData.getCount();

        ArrayList<DB_Get_Values> dbList = new ArrayList<DB_Get_Values>();

        while (GetAllData.moveToNext()) {
            DB_Get_Values w = new DB_Get_Values(GetAllData.getString(0), GetAllData.getString(1), GetAllData.getString(2));
            dbList.add( w );
            j++;
        }

Step 3: Create an adapter class that will be used to control the display of the listview

          DBAdapter T_Adapter = new DBAdapter( 
                this,
                dbList ); 
        setListAdapter( T_Adapter );
        GetAllData.close();

DB_Get_Values Class: Full of Getters

public class DB_Get_Values {

  public String P_Key = "";
  public String Mac_addr = "";
  public String SDK_VRS = "";

 public DB_Get_Values( String P_Key, String Mac_addr, String SDK_VRS) 
  {
    this.P_Key = P_Key;
    this.Mac_addr = Mac_addr;
    this.SDK_VRS = SDK_VRS;
 }
  public String get_Mac_addr() {
    return Mac_addr;
  }
  public String get_P_Key() {
    return P_Key;
  }
  public String get_SDK_VRS() {
        return SDK_VRS;
      }
}

Class DBAdapter: Get's the values to put into the listview and allows for programatically controlling the display.

class AdapterView extends LinearLayout {        
        public AdapterView(Context context, 
                DB_Get_Values list ) {
            super( context );

            this.setOrientation(HORIZONTAL);        

            LinearLayout.LayoutParams PkeyParams = 
                new LinearLayout.LayoutParams(40, LayoutParams.WRAP_CONTENT);
            PkeyParams.setMargins(1, 1, 1, 1);

            TextView Pkey = new TextView( context );
            Pkey.setText( list.get_P_Key() );
            Pkey.setTextSize(14f);
            Pkey.setTextColor(Color.WHITE);
            addView( Pkey, PkeyParams);

            LinearLayout.LayoutParams MacAddrParams = 
                new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
            MacAddrParams.setMargins(1, 1, 1, 1);
            TextView MacAddr = new TextView( context );
            MacAddr.setText( list.get_Mac_addr() );
            MacAddr.setTextSize(14f);
            MacAddr.setTextColor(Color.WHITE);
            addView( MacAddr, MacAddrParams);

            LinearLayout.LayoutParams SDK_VRSParams = 
                new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);
            SDK_VRSParams.setMargins(1, 1, 1, 1);
            TextView SDK_VRS = new TextView( context );
            SDK_VRS.setText( list.get_SDK_VRS() );
            SDK_VRS.setTextSize(14f);
            SDK_VRS.setTextColor(Color.WHITE);
            addView( SDK_VRS, SDK_VRSParams);
}
}
public class DBAdapter extends BaseAdapter {

    private Context context;
    private List<DB_Get_Values> list;

    public DBAdapter(Context context, List<DB_Get_Values> list ) {
        this.context = context;
        this.list = list;
    }

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

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

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

Step 5: You are now done! Change the parameters in AdapterView class to modify how it looks. Hopefully this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜