ListView为什么没有报错,也没有显示?
代码一:private String[] judge(){
String values="";
String data="";
id=et_id.toString();
name=tv_name.toString();
learner=et_learner.toString();
if (id.length()>0){
values="_id";
data=id;
}else if(name.length()>0){
values="name";
data=name;
}else if (learner.length()>0){
values="learner";
data=learner;
}
return new String[]{values,data};
}
代码二:case R.id.bt_look:
Toast.makeText(LocalDataActivity.this, "点击", Toast.LENGTH_SHORT).show();
Cursor c;
String[] strings=judge();
if (strings[0].length()>0){
c=dao.get(strings[0],strings[1]);
}else {
c=dao.get();
}
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.local_item,c,
new String[]{"_id","name","learner"},new int[]{R.id.id_item,R.id.name_item,R.id.learner_item},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
Toast.makeText(LocalDataActivity.this, "处理完了", Toast.LENGTH_SHORT).show();
break;
代码三:
//查看
public Cursor get(String ... str){
String sql="select * from test_tb ";
if (str.length>0){
sql+=" where "+str[0]+" = '"+str[1]+"'";
}
Cursor c=db.rawQuery(sql,null);
return c;
}
正在回答
把这行代码去掉,因为cursor也是有生命周期的,如果没有把数据全部显示完就关闭了,就会造成显示问题。游标一般数据量小的情况不需要手动关闭,除非在数据量比较庞大时,你可以在activity的onDestroy()方法中再进行关闭就可以了
//文件:parts_list.xml
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/learner"
android:layout_width="52dp"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="12345"
android:textColor="#35A2C9"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="前端迷您MVVM框架,Avalon复杂绑定属性篇。"/>
</LinearLayout>
//文件:local_item.xml
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/id_item_"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/learner_item_"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/name_item_"/>
</LinearLayout>
//文件:activity_main.xml
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context="com.studio.sqlfree_programming.MainActivity">
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="本地数据"/>
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
//文件:activity_data_local.xml
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="30dp"
android:text="id:"/>
<EditText
android:id="@+id/et_id"
android:layout_width="50dp"
android:layout_height="match_parent" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="30dp"
android:text="编号:"/>
<EditText
android:id="@+id/et_learner"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="30dp"
android:text="内容:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:onClick="onClick"
android:textSize="20dp"
android:id="@+id/bt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:layout_weight="1"/>
<Button
android:onClick="onClick"
android:textSize="20dp"
android:layout_weight="1"
android:id="@+id/bt_look"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看"/>
<Button
android:onClick="onClick"
android:textSize="20dp"
android:layout_weight="1"
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:onClick="onClick"
android:textSize="20dp"
android:layout_weight="1"
android:id="@+id/bt_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"/>
</LinearLayout>
<!--<ScrollView
android:layout_marginBottom="100dp"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">-->
<ListView
android:layout_marginTop="20dp"
android:id="@+id/list_view_two"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--</ScrollView>-->
</LinearLayout>
public class ListViewAdapt extends BaseAdapter {
private Context context;
private List<Entity> entityList;
public ListViewAdapt(Context context, List<Entity> entityList){
this.context=context;
this.entityList=entityList;
}
@Override
public int getCount() {
return entityList.size();
}
@Override
public Object getItem(int position) {
return entityList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView==null){
convertView=View.inflate(context, R.layout.parts_list,null);
viewHolder=new ViewHolder();
viewHolder.intTextView= (TextView) convertView.findViewById(R.id.learner);
viewHolder.stringTextView= (TextView) convertView.findViewById(R.id.name);
convertView.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) convertView.getTag();
}
Entity entity=entityList.get(position);
viewHolder.intTextView.setText(entity.getLearner()+"");
viewHolder.stringTextView.setText(entity.getName());
return convertView;
}
private class ViewHolder{
TextView intTextView;
TextView stringTextView;
}
}
public class Dao {
private SQLiteDatabase db;
public Dao(Context c){
String path=c.getFilesDir()+"/test.db";
SQLiteOpenHelper helper=new SQLiteOpenHelper(c,path,null,1) {
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table test_tb (_id integer primary key autoincrement,name varchar,learner integer)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
db=helper.getReadableDatabase();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+path);
}
//添加
public void add(Entity entity){
System.out.println(entity.toString());
String sql="insert into test_tb (name,learner) values (?,?)";
db.execSQL(sql,new String[]{entity.getName(),""+entity.getLearner()});
}
//查看
public Cursor get(String ... str){
String sql="select * from test_tb ";
/* if (str.length>0){
sql+=" where "+str[0]+" = '"+str[1]+"'";
}*/
Cursor c=db.rawQuery(sql,null);
return c;
}
//删除
public void delete(String... str){
String sql="delete from test_tb where "+str[0]+"='"+str[1]+"'";
db.execSQL(sql);
}
//修改
public void update(String... str){
String sql="update test_tb set name='"+str[0]+"',learner='"+str[1]+"'where _id='"+str[2]+"'";
db.execSQL(sql);
}
}
public class LocalDataActivity extends AppCompatActivity {
private Button bt_add;
private Button bt_update;
private Button bt_delete;
private Button bt_look;
private ListView listView;
private List<Entity> entityList;
private Dao dao;
private EditText et_id;
private EditText et_learner;
private TextView tv_name;
private String id="";
private String name="";
private String learner="";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_local);
initView();
initData();
}
private void initData() {
Cursor c=dao.get();
if (c.getCount()==0){
for(int i=0;i<entityList.size();i++){
Entity entity;
entity=entityList.get(i);
dao.add(entity);
Toast.makeText(LocalDataActivity.this, entity.toString(), Toast.LENGTH_SHORT).show();
}
}
c.close();
}
private void initView() {
listView= (ListView) findViewById(R.id.list_view_two);
bt_add= (Button) findViewById(R.id.bt_add);
bt_delete= (Button) findViewById(R.id.bt_delete);
bt_look= (Button) findViewById(R.id.bt_look);
bt_update= (Button) findViewById(R.id.bt_update);
et_id= (EditText) findViewById(R.id.et_id);
et_learner= (EditText) findViewById(R.id.et_learner);
tv_name= (TextView) findViewById(R.id.tv_name);
entityList= (List<Entity>) getIntent().getSerializableExtra("list");
dao=new Dao(this);
}
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_add:
break;
case R.id.bt_look:
Toast.makeText(LocalDataActivity.this, "点击", Toast.LENGTH_SHORT).show();
Cursor c;
/* String[] strings=judge();*/
/* if (!strings[0].equals("")){
Toast.makeText(LocalDataActivity.this, "有条件", Toast.LENGTH_SHORT).show();
c=dao.get(strings[0],strings[1]);
}else {*/
/*Toast.makeText(LocalDataActivity.this, "没条件", Toast.LENGTH_SHORT).show();*/
c=dao.get();
/* }*/
if ((c.getCount()==0))
{
Toast.makeText(LocalDataActivity.this, "没有数据~!", Toast.LENGTH_SHORT).show();
}else {
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.local_item,c,
new String[]{"_id","name","learner"},new int[]{R.id.id_item_,R.id.name_item_,R.id.learner_item_},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
Toast.makeText(LocalDataActivity.this, "有数据!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(LocalDataActivity.this, "处理完了", Toast.LENGTH_SHORT).show();
c.close();
break;
case R.id.bt_update:
break;
case R.id.bt_delete:
break;
}
}
private String[] judge(){
String values="";
String data="";
id=et_id.toString();
name=tv_name.toString();
learner=et_learner.toString();
if (!id.equals("")){
values="_id";
data=id;
}else if(!name.equals("")){
values="name";
data=name;
}else if (!learner.equals("")){
values="learner";
data=learner;
}
String[] strings=new String[2];
strings[0]=values;
strings[1]=data;
return strings;
}
}
package com.studio.sqlfree_programming;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.studio.sqlfree_programming.adapt.ListViewAdapt;
import com.studio.sqlfree_programming.modle.Entity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final String HTTP_URL_STRING = "http://www.imooc.com/api/teacher?type=2&page=1";
public static final String DATA = "data";
public static final String LEARNER = "learner";
public static final String ID = "id";
public static final String NAME = "name";
public static final String PIC_SMALL = "picSmall";
public static final String PIC_BIG = "picBig";
public static final String DESCRIPTION = "description";
private ListView listView;
private Button button;
private List<Entity> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new RequestDataAsyncTask().execute();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (list.size()>0){
Intent intent=new Intent(MainActivity.this,LocalDataActivity.class);
intent.putExtra("list", (Serializable) list);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
}
private void initView() {
listView= (ListView) findViewById(R.id.list_view);
button= (Button) findViewById(R.id.bt);
}
public class RequestDataAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
//定义好下载路径
URL url=new URL(HTTP_URL_STRING);
//打开网页
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//设置超时时间10秒
connection.setConnectTimeout(10000);
//获取网址内容方式
connection.setRequestMethod("GET");
//开始链接
connection.connect();
//获取请求响应码
int responseCode=connection.getResponseCode();
if (responseCode==HttpURLConnection.HTTP_OK){
//请求成功后用InputStreamReader接收获取到的数据
InputStreamReader reader=new InputStreamReader(connection.getInputStream());
//转换成Buffered类型
BufferedReader reader1=new BufferedReader(reader);
StringBuilder builder=new StringBuilder();
String line;
while ((line=reader1.readLine())!=null){
builder.append(line);
}
reader.close();
reader1.close();
return builder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject=new JSONObject(s);
List<Entity> entityList= new ArrayList<>();
JSONArray jsonArray=jsonObject.getJSONArray(DATA);
//解析多个data
for(int i=0;i<jsonArray.length();i++){
Entity entity=new Entity();
JSONObject object= (JSONObject) jsonArray.get(i);
int learner=object.getInt(LEARNER);
entity.setLearner(learner);
String name=object.getString(NAME);
entity.setName(name);
int id=object.getInt(ID);
entity.setId(id);
String picSmall=object.getString(PIC_SMALL);
entity.setPicSmall(picSmall);
String picBig=object.getString(PIC_BIG);
entity.setPicBig(picBig);
String description=object.getString(DESCRIPTION);
entity.setDescription(description);
entityList.add(entity);
}
list=new ArrayList<>();
list=entityList;
listView.setAdapter(new ListViewAdapt(MainActivity.this,entityList));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public Dao(Context c){
String path=c.getFilesDir()+"test.db";
SQLiteOpenHelper helper=new SQLiteOpenHelper(c,path,null,1) {
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table test_tb (_id integer primary key autoincrement,name varchar,learner integer)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
db=helper.getReadableDatabase();
}
这个是创建的
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星