Home > Mobile >  Simple, according to the Listview new_adapter error, can't find the reason
Simple, according to the Listview new_adapter error, can't find the reason

Time:09-16

Source code file with the layout file all posted:
MainActivity. Java
 
Package com. Baidu. Listview.
The import android. App. The Activity;
The import android. The content. The Context;
The import android. Graphics. Color;
The import android. OS. Bundle;
The import android. Util. Log;
The import android. View. The view;
The import android. View. ViewGroup;
The import android. Widget. BaseAdapter;
The import android. Widget. The Button;
The import android. Widget. LinearLayout;
The import android. Widget. ListView.
The import android. Widget. TextView;
The import android. Widget. Toast;
Import the Java. Util. ArrayList;
import java.util.List;

Public class MainActivity extends the Activity implements the View. An OnClickListener {
Private DatabaseHelper iDatabaseHelper;
Private Button Btn_CreateDB Btn_ShowListView;
Private DatabaseDAO iDatabaseDAO;
Private List IList.
Private LinearLayout LayoutRoot.
Private ListView iListView;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IDatabaseHelper=new DatabaseHelper (this);
Btn_CreateDB=(Button) the findViewById (R.i db tn_SetDatabase);
Btn_ShowListView=(Button) the findViewById (R.i db tn_ShowListView);
LayoutRoot=(LinearLayout) the findViewById (R.i d.L ayout_ShowListViewSpeace);
IListView=(ListView) the findViewById (R.i, dc ontrol_ListView);
IListView. SetAdapter (new MyAdapter ());
Btn_CreateDB. SetOnClickListener (this);
Btn_ShowListView. SetOnClickListener (this);

}

@ Override
Public void onClick (View v) {
The switch (v.g etId ()) {
Case R.i db tn_SetDatabase://create the database and the initialization data
Try {
IDatabaseHelper. GetWritableDatabase ();
} the catch (Exception e) {
ToastMessage (this, "exception! Reason: \ n "+ um participant etMessage (). The toString (), 0).
} the finally {
ToastMessage (this, "database creation success!" , 0);
IDatabaseDAO=new DatabaseDAO (MainActivity. This);
Long getLongNumber=iDatabaseDAO. DataAddToBase ();
If (getLongNumber!=0) {
ToastMessage (this, "data initialization success!! ", 0);
} else {
ToastMessage (this, "data initialization failed, please try later!!!!! ", 0);
}
}
break;
Case R.i db tn_ShowListView://show the ListView data

break;
}
}
Private class MyAdapter extends BaseAdapter {

@ Override
Public int getCount () {
Return iList. The size ();
}

@ Override
Public Object the getItem (int position) {
return null;
}

@ Override
Public long getItemId (int position) {
return 0;
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent) {
TextView TextControl=new TextView (getApplicationContext ());
TextControl. SetTextSize (20);
TextControl. SetTextColor (Color BLACK);
//DataGetSet iDataGetSet=iList. Get (position). ID;
Int id=iList. Get (position). Id.
String name=iList. Get (position) User_Name;
String number=iList. Get (position). Phone_Number.
//TextControl setText (iDataGetSet. ToString ());
TextControl. SetText (" ID: "+ ID +" t \ \ t "+ number +" t \ \ t + number ");
//LayoutRoot. AddView (TextControl);

Return TextControl;
}
}
Private void ToastMessage (Context Context, String MSG, int serial) {
Toast. MakeText (context, MSG, serial), show ();
}
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
DatabaseDAO. Java
 
Package com. Baidu. Listview.

The import android. Content. ContentValues;
The import android. The content. The Context;
The import android. Database. Cursor;
The import android. Database. Sqlite. SQLiteDatabase;

Import the Java. Util. ArrayList;
import java.util.List;

Public class DatabaseDAO {
Private DatabaseHelper iDatabaseHelper;
Public DatabaseDAO Context (Context) {
IDatabaseHelper=new DatabaseHelper (context);
}
Public long DataAddToBase () {
Long getLongState=0;
SQLiteDatabase db=iDatabaseHelper. GetWritableDatabase ();
ContentValues iValues=new ContentValues ();
for(int i=0; I<50; I++) {
IValues. Put (" user_name ", "king of speed" + I);
IValues. Put (" phone_number ", "13455433" + I);
GetLongState=db. Insert (" AppTable ", null, iValues);
}
The close ();
Return getLongState;
}
Public List FindDataAll () {
List IDatabase=new ArrayList (a);
SQLiteDatabase db=iDatabaseHelper. GetReadableDatabase ();
Cursor iCursor=db. Query (" AppTable ", new String [] {" id ", "user_name", "phone_number"}, null, null, null, null, null);
While (iCursor. MoveToNext ()) {
Int ID_get=iCursor. Get int (iCursor getColumnIndex (" id "));
String UserName_Get=iCursor. Get String (iCursor getColumnIndex (" user_name "));
String Phone_Number_Get=iCursor. Get String (iCursor getColumnIndex (" phone_number "));
DataGetSet iDataGetSet=new DataGetSet (ID_get UserName_Get, Phone_Number_Get);
IDatabase. Add (iDataGetSet);
}
Return iDatabase;
}
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull