欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

频道管理简单的移动

程序员文章站 2022-05-17 08:14:00
...

图片展示一下效果
频道管理简单的移动
布局文件
Activity布局

<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.pindaogaunlidemo.MainActivity">

  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      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" />
</LinearLayout>

adapter_mygridview_item.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:layout_margin="10dp"
    >
<TextView
    android:id="@+id/text_item"
    android:textSize="30sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

主Activity

package com.example.pindaogaunlidemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<String> list_my;
    private List<String> list_other;
    private GridView gridView_my;
    private GridView gridView_other;
    private myBaseAdapter adapter_other;
    private myBaseAdapter adapter_my;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView_my = (GridView) findViewById(R.id.my_pindao);
        gridView_other = (GridView) findViewById(R.id.other_pindao);
        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, MainActivity.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("user", null, values);
                list_my.remove(i);
                adapter_my.notifyDataSetChanged();

                Cursor cursor = db.query("user", 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("user", 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("user", null, values);
                list_other.remove(i);
                adapter_other.notifyDataSetChanged();

                Cursor cursor = db.query("user", 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("user", null, null);

            }
        });

    }
}

myBaseAdapter

package com.example.pindaogaunlidemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;


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;

    }
}

mySqliteDataBase

package com.example.pindaogaunlidemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class mySqliteDataBase extends SQLiteOpenHelper {

    public mySqliteDataBase(Context context) {
        super(context, "user", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table user(id integer primary key  autoincrement ,title verchar  )");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
相关标签: 频道管理