首先,,创建数据库//
sqLiteDatabase.execSQL("create table item(id integer primary key autoincrement,title text)");
MainActivity中进行逻辑操作/
public class MainActivity extends AppCompatActivity {
private GridView gridView_my;
private GridView gridView_other;
private SQLiteDatabase db;
private myBaseAdapter adapter_other;
private myBaseAdapter adapter_my;
private List<String> list_my;
private List<String> list_other;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
mySqliteDataBase database = new mySqliteDataBase(this);
db = database.getWritableDatabase();
list_my = new ArrayList<>();
list_my.add("热点");
list_my.add("财经");
list_my.add("科技");
list_my.add("段子");
list_my.add("汽车");
list_my.add("时尚");
list_my.add("房产");
list_my.add("彩票");
list_my.add("独家");
list_other = new ArrayList<>();
list_other.add("头条");
list_other.add("首页");
list_other.add("娱乐");
list_other.add("图片");
list_other.add("游戏");
list_other.add("体育");
list_other.add("北京");
list_other.add("军事");
list_other.add("历史");
list_other.add("教育");
adapter_other = new myBaseAdapter(list_other, this);
gridView_other.setAdapter(adapter_other);
adapter_my = new myBaseAdapter(list_my, MainActivity.this);
gridView_my.setAdapter(adapter_my);
gridView_my.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = (String) adapter_my.getItem(i);
ContentValues values=new ContentValues();
values.put("title",item);
db.insert("item",null,values);
list_my.remove(i);
adapter_my.notifyDataSetChanged();
Cursor cursor = db.query("item", null, null, null, null, null, null);
String str=null;
while (cursor.moveToNext()){
str= cursor.getString(cursor.getColumnIndex("title"));
}
list_other.add(str);
adapter_other.notifyDataSetChanged();
db.delete("item",null,null);
}
});
gridView_other.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = (String) adapter_other.getItem(i);
ContentValues values=new ContentValues();
values.put("title",item);
db.insert("item",null,values);
list_other.remove(i);
adapter_other.notifyDataSetChanged();
Cursor cursor = db.query("item", null, null, null, null, null, null);
String str=null;
while (cursor.moveToNext()){
str= cursor.getString(cursor.getColumnIndex("title"));
}
list_my.add(str);
adapter_my.notifyDataSetChanged();
db.delete("item",null,null);
}
});
}
private void init() {
gridView_my = (GridView) findViewById(R.id.my_pindao);
gridView_other = (GridView) findViewById(R.id.other_pindao);
}
}
进行优化//
public class myBaseAdapter extends BaseAdapter{
private List<String> list;
private Context context;
public myBaseAdapter(List<String> list, Context context) {
this.list = list;
this.context=context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder=null;
if (convertView==null){
holder=new viewHolder();
convertView=convertView.inflate(context,R.layout.adapter_mygridview_item,null);
holder.tvContent= (TextView) convertView.findViewById(R.id.text_item);
convertView.setTag(holder);
}else{
holder= (viewHolder) convertView.getTag();
}
holder.tvContent.setText(list.get(position));
return convertView;
}
class viewHolder{
TextView tvContent;
}
}
上布局
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:textColor="#f22"
android:textSize="35dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的频道"
/>
<TextView
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
<GridView
android:numColumns="4"
android:id="@+id/my_pindao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></GridView>
<TextView
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
</LinearLayout>
<TextView
android:layout_marginTop="80dp"
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:textColor="#f22"
android:textSize="35dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="其他频道"
/>
<TextView
android:background="#585858"
android:layout_width="match_parent"
android:layout_height="1dp" />
<GridView
android:numColumns="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/other_pindao"
></GridView>
</LinearLayout>
//
<TextView
android:id="@+id/text_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:minHeight="38.0dip"
android:minWidth="72.0dip"
android:textSize="14.0sp" />