Introduce how to use sqlite with Android - read data from sqlite.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<ListView
android:id="@+id/lvScriptHistory"
android:layout_width="wrap_content"
android:layout_height="260dp" >
</ListView>
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/coltv"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" />
<TextView
android:id="@+id/col0tv"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" />
<TextView
android:id="@+id/col1tv"
android:layout_width="150dp"
android:layout_height="24dp"
android:layout_toRightOf="@+id/col0tv"
android:gravity="left"
android:text="TextView"
android:textColor="#FE2E64"
android:textSize="10dp" />
<TextView
android:id="@+id/col2tv"
android:layout_width="50dp"
android:layout_height="24dp"
android:layout_toRightOf="@+id/col1tv"
android:gravity="right"
android:text="TextView"
android:textColor="#FE2E64"
android:textSize="10dp" />
</RelativeLayout>
private Button save, urlhistory, btnClosePopup;
private PopupWindow pwindo;
...
...
public void onClick(View v) {
...
case R.id.bScriptList:
initiatePopupWindow();
Log.d("Checking poing 1 current url : ", "checklang");
break;
...
}
...
...
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) webview.findViewById(R.id.popup_element));
totalList = (ListView) layout.findViewById(R.id.lvScriptHistory);
pwindo = new PopupWindow(layout, 370, 500, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
collist.clear();
collist_0.clear();
collist_1.clear();
collist_2.clear();
DatabaseOperations dop = new DatabaseOperations(ctx);
Log.d("Checking poing 1 current url : ", dop.toString());
try {
Cursor cur = dop.getTotalScript(dop);
Log.d("Checking poing 1 current cur : ", cur.toString());
while (cur.moveToNext()) {
String valueofcol = cur.getString(0);
String valueofcol0 = cur.getString(1);
String valueofcol1 = cur.getString(2);
String valueofcol2 = cur.getString(3);
Log.d("Checking poing 1 current valueofcol1 : ",
valueofcol1);
Log.d("Checking poing 1 current valueofcol2 : ",
valueofcol2);
collist.add(valueofcol);
collist_0.add(valueofcol0);
collist_1.add(valueofcol1);
collist_2.add(valueofcol2);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
dop.close();
}
String[] from = new String[] { "col", "col_0", "col_1", "col_2" };
int[] to = new int[] { R.id.coltv, R.id.col0tv, R.id.col1tv,
R.id.col2tv };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < collist_1.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("col", collist.get(i));
map.put("col_0", collist_0.get(i));
map.put("col_1", collist_1.get(i));
map.put("col_2", collist_2.get(i));
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(ctx, fillMaps,
R.layout.popup_list, from, to) {
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
final View v = super.getView(position, convertView, parent);
return v;
}
};
totalList.setAdapter(adapter);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
totalList.setClickable(true);
totalList.setItemsCanFocus(false);
totalList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String url = ((TextView) view.findViewById(R.id.col0tv))
.getText().toString();
String url1 = ((TextView) view.findViewById(R.id.coltv))
.getText().toString();
Log.d("wanna click ", url);
// set selected id
currentid = url1;
pwindo.dismiss();
webview.loadUrl(url);
Log.d("clicked? ", url1);
}
});
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
...
...
public Cursor getTotalScript(DatabaseOperations dop) {
sql = dop.getReadableDatabase();
return sql.rawQuery("select * from urlhistory order by id desc", null);
}
...
1. screen_popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<ListView
android:id="@+id/lvScriptHistory"
android:layout_width="wrap_content"
android:layout_height="260dp" >
</ListView>
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
2. popup_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/coltv"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" />
<TextView
android:id="@+id/col0tv"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" />
<TextView
android:id="@+id/col1tv"
android:layout_width="150dp"
android:layout_height="24dp"
android:layout_toRightOf="@+id/col0tv"
android:gravity="left"
android:text="TextView"
android:textColor="#FE2E64"
android:textSize="10dp" />
<TextView
android:id="@+id/col2tv"
android:layout_width="50dp"
android:layout_height="24dp"
android:layout_toRightOf="@+id/col1tv"
android:gravity="right"
android:text="TextView"
android:textColor="#FE2E64"
android:textSize="10dp" />
</RelativeLayout>
3. WebviewActivity.java
...private Button save, urlhistory, btnClosePopup;
private PopupWindow pwindo;
...
...
public void onClick(View v) {
...
case R.id.bScriptList:
initiatePopupWindow();
Log.d("Checking poing 1 current url : ", "checklang");
break;
...
}
...
...
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) webview.findViewById(R.id.popup_element));
totalList = (ListView) layout.findViewById(R.id.lvScriptHistory);
pwindo = new PopupWindow(layout, 370, 500, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
collist.clear();
collist_0.clear();
collist_1.clear();
collist_2.clear();
DatabaseOperations dop = new DatabaseOperations(ctx);
Log.d("Checking poing 1 current url : ", dop.toString());
try {
Cursor cur = dop.getTotalScript(dop);
Log.d("Checking poing 1 current cur : ", cur.toString());
while (cur.moveToNext()) {
String valueofcol = cur.getString(0);
String valueofcol0 = cur.getString(1);
String valueofcol1 = cur.getString(2);
String valueofcol2 = cur.getString(3);
Log.d("Checking poing 1 current valueofcol1 : ",
valueofcol1);
Log.d("Checking poing 1 current valueofcol2 : ",
valueofcol2);
collist.add(valueofcol);
collist_0.add(valueofcol0);
collist_1.add(valueofcol1);
collist_2.add(valueofcol2);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
dop.close();
}
String[] from = new String[] { "col", "col_0", "col_1", "col_2" };
int[] to = new int[] { R.id.coltv, R.id.col0tv, R.id.col1tv,
R.id.col2tv };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < collist_1.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("col", collist.get(i));
map.put("col_0", collist_0.get(i));
map.put("col_1", collist_1.get(i));
map.put("col_2", collist_2.get(i));
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(ctx, fillMaps,
R.layout.popup_list, from, to) {
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
final View v = super.getView(position, convertView, parent);
return v;
}
};
totalList.setAdapter(adapter);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
totalList.setClickable(true);
totalList.setItemsCanFocus(false);
totalList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String url = ((TextView) view.findViewById(R.id.col0tv))
.getText().toString();
String url1 = ((TextView) view.findViewById(R.id.coltv))
.getText().toString();
Log.d("wanna click ", url);
// set selected id
currentid = url1;
pwindo.dismiss();
webview.loadUrl(url);
Log.d("clicked? ", url1);
}
});
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
...
4. DatabaseOperations.java
...
public Cursor getTotalScript(DatabaseOperations dop) {
sql = dop.getReadableDatabase();
return sql.rawQuery("select * from urlhistory order by id desc", null);
}
...
No comments:
Post a Comment