Open KnightBubble opened 9 years ago
package com.example.androidstudytest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
public List<NewsBean> newsList ;
public MyArrAdapter myDapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar abar = getActionBar();
abar.setDisplayHomeAsUpEnabled(true);
setOverflowShowingAlways();
newsList = new ArrayList<NewsBean>() ;
for (int i =0;i<20;i++) {
newsList.add(new NewsBean(R.drawable.avatar,"我是标题"+i,"我是内容"+i));
}
ListView lv = (ListView) findViewById(R.id.lv);
myDapter = new MyArrAdapter(this,newsList);
lv.setAdapter(myDapter);
lv.setOnScrollListener(new OnScrollListener() {
private Boolean flag = false;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if (flag) {
newsList.add(new NewsBean(R.drawable.avatar,"==我是标题","==我是内容"));
myDapter.notifyDataSetChanged();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount >= totalItemCount ) {
flag = true;
}
}
});
}
/**
* 展示overflow中折叠按钮
*/
private void setOverflowShowingAlways() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* menuitem click
* @param item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressLint("NewApi")
public void clickYourName(View view) {
myDialogFragment mdf = new myDialogFragment();
mdf.show(getFragmentManager(), "justTest");
}
public void readSharedPre(View view) {
SharedPreferences spf = getPreferences(Context.MODE_PRIVATE);
String myName = spf.getString("myName", "it is just empty");
Toast.makeText(this, myName, 1).show();
}
public void writeSharedPre(View view){
SharedPreferences spf = getPreferences(Context.MODE_PRIVATE);
Editor editor = spf.edit();
editor.putString("myName", "shibiao");
boolean flag = editor.commit();
if (flag) {
Toast.makeText(this, "success", 1).show();
}
else {
Toast.makeText(this, "fail", 1).show();
}
}
@SuppressLint("ShowToast")
public void writeData(View view) {
// Toast.makeText(this, "test", 1).show();
FileOutputStream fos = null;
try {
fos = openFileOutput("text.txt", Context.MODE_PRIVATE);
fos.write(new String("hello world").toString().getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, "write data ok", 1).show();
}
}
public void writeData2(View view){
File file = new File(getFilesDir(),"tes2.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
try {
fos.write(new String("hello shibiao").toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void readData1(View view) {
FileInputStream fis = null;
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
fis = openFileInput("text.txt");
br = new BufferedReader(new InputStreamReader(fis));
String line = null;
try {
while ( (line = br.readLine())!= null) {
sb.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, sb.toString(), 1).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (br!= null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}