我已经创建了一个将填充我的自定义列表视图的适配器,代码在我立即设置列表时起作用,我在下面已经注释掉了。我的列表每秒更新一次,我已经读过我需要更新我的适配器,这就是为什么我的列表视图没有更新。列表视图显示为空白,因为使用ArrayList的原始大小为空。我遇到的答案都没有修复我的代码,我无法弄清楚如何更新我的列表视图。
我尝试过notifyDataSetChanged并且无法让它做任何事情,我把它放在那里,这样你就可以看到我是如何使用它的。
这是我的代码,其中列表视图在TimerTask中设置,其中getDataForListView类是:
public class MainActivity extends ActionBarActivity {
//set variables
public ArrayList<String> use = new ArrayList<String>(); //holds app name
public ArrayList<Integer> useTime = new ArrayList<Integer>(); //holds app time
//TextView sepW; //sets text to apps being used
//TextView sepT; // sets text to time app has been used
Timer timers = new Timer(); //gets TimerTask to run on start
final Handler handler = new Handler(); //used in TimerTask
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//sepW = (TextView) findViewById(R.id.sepWord);
//sepT = (TextView) findViewById(R.id.dude);
timers.schedule(time, 1000,1000);
}
TimerTask time = new TimerTask(){
public void run() {
handler.post(new Runnable() {
public void run() {
//Makes a String of current app being used
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
StringBuilder b = new StringBuilder();
for (ActivityManager.RunningAppProcessInfo process: runningProcesses) {
b.append(process.processName);
b.append(" ");
}
//splits string of apps into app names
String wordS = b.toString();
String[] apps = wordS.split("\\ ");
String[] separated = apps[0].split("\\.");
//finds if app has already been used in day and if so what the index of the app is (spot int)
int i = 0;
int spot = -1;
if (use.size() > 0) {
while (i < use.size()) {
if ((use.get(i)).equals(separated[2])) {
spot = i;
i = use.size();
}
i = i + 1;
}
}
//goes to index of app being used and adds to time
if (spot < 0) {
use.add(separated[2]);
useTime.add(0);
} else if (spot >= 0) {
int hold = useTime.get(spot);
hold=hold+1;
useTime.set(spot, hold);
}
final String[] useArray = use.toArray(new String[use.size()]);
//make list view to show data
//ArrayAdapter<String> appNameAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, useArray);
TableViewList codeAppAdapter = new TableViewList();
ListView appNameS = (ListView)findViewById(R.id.showData);
codeAppAdapter.notifyDataSetChanged();
appNameS.setAdapter(codeAppAdapter);
//**sets text to help with debug for now
//sepT.setText(useTime.toString());
//sepW.setText(use.toString());
}
});
}
};
public List<CodeLearnApp> getDataForListView(){
List<CodeLearnApp> codeLearnAppList = new ArrayList<CodeLearnApp>();
int numApps = use.size();
for(int r=0;r<numApps;r++){
CodeLearnApp ap = new CodeLearnApp();
ap.appName = use.get(r);
ap.appTime = useTime.get(r).toString();
codeLearnAppList.add(ap);
}
/*if(numApps==0) {
for (int i = 0; i < 15; i++) {
CodeLearnApp app = new CodeLearnApp();
app.appName = "App " + i;
app.appTime = "" + i;
codeLearnAppList.add(app);
}
}*/
return codeLearnAppList;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的BaseAdapter:
public class TableViewList extends BaseAdapter {
//allows method getDataForListView to be called from MainActivity
MainActivity help = new MainActivity();
List<CodeLearnApp> codeLearnAppsList = help.getDataForListView();
@Override
public int getCount() {
return codeLearnAppsList.size();
}
@Override
public CodeLearnApp getItem(int arg0) {
return codeLearnAppsList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
/*Dont use ListViewWithBaseAdapte, use
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(parent.getContext());
}*/
@Override
public View getView(int arg0, View arg1, ViewGroup arg2){
if(arg1==null){
LayoutInflater inflater = LayoutInflater.from(arg2.getContext());
arg1 = inflater.inflate(R.layout.custom_listview_1, arg2,false);
}
TextView appName = (TextView)arg1.findViewById(R.id.textView1);
TextView appTime = (TextView)arg1.findViewById(R.id.textView2);
CodeLearnApp appp = codeLearnAppsList.get(arg0);
appName.setText(appp.appName);
appTime.setText(appp.appTime);
return arg1;
}
}
使用的附加课程:
public class CodeLearnApp {
String appName;
String appTime;
}
这是我的主要xml:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showData"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
ListView xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView1"
android:text="CodeLearn Chapter 1"
android:textSize="16sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/textView1"
android:text="Description" />
</RelativeLayout>