真机无法运行,直接黑屏闪退,求问一下原因,谢谢!
package com.example.listviewdemo;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@TargetApi(Build.VERSION_CODES.M)
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,View.OnScrollChangeListener{
private ListView listView;
private ArrayAdapter<String> arr_adapter;
public SimpleAdapter sim_adapter;
private List<Map<String,Object>>datalist;
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
String[]arr_data={"慕课网1","慕课网2","慕课网3","慕课网4"};
datalist=new ArrayList<Map<String,Object>>();
arr_adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr_data);
sim_adapter=new SimpleAdapter(this,getDatalist(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
// listView.setAdapter(arr_adapter);
listView.setAdapter(sim_adapter);
listView.setOnClickListener((View.OnClickListener) this);
listView.setOnScrollChangeListener(this);
}
private List<Map<String,Object>> getDatalist(){
for(int i=0;i<20;i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("pic",R.drawable.ic_launcher);
map.put("text","慕课网"+i);
datalist.add(map);
}
return datalist;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str= listView.getItemAtPosition(position) +"";
Toast.makeText(MainActivity.this,"position="+position+" text="+str,Toast.LENGTH_SHORT).show();
Log.i("toast",String.valueOf(position));
}
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
}
}