Skip to content

Commit

Permalink
hzuapps#5 hzuapps#324 第5次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
wanshanghong committed Apr 3, 2019
1 parent 31e7dd9 commit 3f72b67
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,135 @@
package edu.hzuapps.androidlabs.Soft1714080902219;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import static android.database.sqlite.SQLiteDatabase.openOrCreateDatabase;

public class Soft1714080902219Fragment1 extends Fragment {
private SQLiteDatabase db1;
// private SQLiteDatabase db2;
private MyDatabaseUtil myDatabaseUtil;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.soft_1714080902219_fragment1,container,false);
View contentView = inflater.inflate(R.layout.soft_1714080902219_fragment1, container, false);
LinearLayout LinearLayout1 = contentView.findViewById(R.id.news1);
LinearLayout LinearLayout2 = contentView.findViewById(R.id.news2);
LinearLayout LinearLayout3 = contentView.findViewById(R.id.news3);
LinearLayout LinearLayout4 = contentView.findViewById(R.id.news4);
LinearLayout1.setOnClickListener(new MyOnclick());
LinearLayout2.setOnClickListener(new MyOnclick());
LinearLayout3.setOnClickListener(new MyOnclick());
LinearLayout4.setOnClickListener(new MyOnclick());

return contentView;
}
public class MyOnclick implements View.OnClickListener {
@Override
public void onClick(View view) {
//参数二是数据库文件名
myDatabaseUtil = new MyDatabaseUtil(getActivity(), "db1.db", null, 1);
db1 = getActivity().openOrCreateDatabase("db1.db", Context.MODE_PRIVATE, null);
/*/data/data/com.example.sqlite/databases必须存在,否则创建出错,最后是databases而不是database,不要忘了加's'*/
// db2 = SQLiteDatabase.openOrCreateDatabase("/data/data/com.example.sqlite/databases/db2.db3",null);---------------------
/*创建表,并判断是否已经存在此表,没创建,则创建并初始化*/
if (!myDatabaseUtil.tabIsExist("news")) {
db1.execSQL("CREATE TABLE news (news_id int)");
} else {
Log.i("+++++++++++", "已经创建了,无需再创建");
}
String id;
int user_id=0;
TextView tv;
System.out.print("db1开启");
switch (view.getId()) {
case R.id.news1:
tv=getActivity().findViewById(R.id.newsTextId1);
user_id=Integer.parseInt(tv.getText().toString());
break;
case R.id.news2:
tv=getActivity().findViewById(R.id.newsTextId2);
user_id=Integer.parseInt(tv.getText().toString());
break;
case R.id.news3:
tv=getActivity().findViewById(R.id.newsTextId3);
user_id=Integer.parseInt(tv.getText().toString());
break;
case R.id.news4:
tv=getActivity().findViewById(R.id.newsTextId4);
user_id=Integer.parseInt(tv.getText().toString());
break;
default:
break;

}
ContentValues values = new ContentValues();
values.put("news_id", user_id);
System.out.print("user_id:"+user_id);
db1.insert("news", "user_id", values);
Intent intent = new Intent(getActivity(), Soft1714080902219Activity.class);
startActivity(intent);
}
}

}

public class MyDatabaseUtil extends SQLiteOpenHelper {
public MyDatabaseUtil(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
//
}

// @Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}

/**
* * 判断某张表是否存在
* * @param tabName 表名 *
*
* @return
*/
public boolean tabIsExist(String tabName) {
boolean result = false;
if (tabName == null) {
return false;
}
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = this.getReadableDatabase();//此this是继承SQLiteOpenHelper类得到的
String sql = "select count(*) as c from sqlite_master where type ='table' and name ='" + tabName.trim() + "' ";
cursor = db.rawQuery(sql, null);
if (cursor.moveToNext()) {
int count = cursor.getInt(0);
if (count > 0) {
result = true;
}
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
android:id="@+id/textView2"
android:layout_width="218dp"
android:layout_height="match_parent"
android:layout_alignEnd="@+id/searh1"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="-6dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="70dp"
android:textColor="#000000"
android:textSize="15dp"
android:layout_marginEnd="-11dp"
android:inputType="text"
android:text="" />
android:text=""
android:textColor="#000000"
android:textSize="15dp" />

<SearchView
android:id="@+id/searh1"
Expand Down Expand Up @@ -138,5 +138,205 @@
android:background="#686868"
android:layout_below="@+id/LinearLayout1"
android:layout_width="match_parent"/>
<LinearLayout
android:id="@+id/news1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/view1"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="-2dp">

<ImageView
android:id="@+id/newsIV1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignTop="@+id/news1"
android:src="@drawable/title1" />

<RelativeLayout
android:layout_width="269dp"
android:layout_height="match_parent">

<TextView
android:id="@+id/tVi1"
android:layout_width="258dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="湖南一小学发生砍人事件值致2死2伤"
android:textSize="18dp" />

<TextView
android:id="@+id/newsTextId1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="1"
/>

<TextView
android:id="@+id/tV1"
android:layout_width="259dp"
android:layout_height="48dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="48dp"
android:textSize="12dp"
android:text="犯罪嫌疑人郑某君(男,31岁,宁运县柏家坪镇人)已被公安机关抓获,事件调" />
</RelativeLayout>

</LinearLayout>
<LinearLayout
android:id="@+id/news2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news1"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="-2dp">

<ImageView
android:id="@+id/newsIV2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignTop="@+id/news2"
android:src="@drawable/news2" />

<RelativeLayout
android:layout_width="269dp"
android:layout_height="match_parent">

<TextView
android:id="@+id/tVi2"
android:layout_width="258dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="10位志愿军烈士遗骸回国了!两架歼-11战机护航"
android:textSize="18dp" />

<TextView
android:id="@+id/newsTextId2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="2"
/>

<TextView
android:id="@+id/tV2"
android:layout_width="259dp"
android:layout_height="48dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="48dp"
android:textSize="12dp"
android:text="11时36分,军方伊尔-76运输机将10位赴韩作战志愿军烈士遗骸送达沈阳。" />
</RelativeLayout>

</LinearLayout>
<LinearLayout
android:id="@+id/news3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news2"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="-2dp">

<ImageView
android:id="@+id/newsIV3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignTop="@+id/news1"
android:src="@drawable/new3" />

<RelativeLayout
android:layout_width="269dp"
android:layout_height="match_parent">

<TextView
android:id="@+id/tVi3"
android:layout_width="258dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="印媒:印藏边境部队指挥部前移“应对中国”"
android:textSize="18dp" />

<TextView
android:id="@+id/newsTextId3"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="3"
/>

<TextView
android:id="@+id/tV3"
android:layout_width="259dp"
android:layout_height="48dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="48dp"
android:textSize="12dp"
android:text="据印度《经济时报》2日报道,隶属印度内政部的“印藏边境警察部队”1日将指挥部正式" />
</RelativeLayout>

</LinearLayout>
<LinearLayout
android:id="@+id/news4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news3"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="-2dp">

<ImageView
android:id="@+id/newsIV4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignTop="@+id/news1"
android:src="@drawable/news4" />

<RelativeLayout
android:layout_width="269dp"
android:layout_height="match_parent">

<TextView
android:id="@+id/tVi4"
android:layout_width="258dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:text="胡锡进再评赵立新言论:演艺界商界人士别乱碰政治"
android:textSize="18dp" />

<TextView
android:id="@+id/newsTextId4"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="4"
/>

<TextView
android:id="@+id/tV4"
android:layout_width="259dp"
android:layout_height="48dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="48dp"
android:textSize="12dp"
android:text="4月3日凌晨,赵立新在微博为自己引发争议的言论致歉,称自己的表达方式以及在与网" />
</RelativeLayout>

</LinearLayout>
</RelativeLayout>

0 comments on commit 3f72b67

Please sign in to comment.