我为音频文件创建了一个列表视图,listView没有显示数据 只显示图像3次没有显示音频我不知道我在哪里做错了请帮帮我...
ListActivity
public class MainActivity extends Activity {
Cursor c;
int index;
int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.audio_list);
String cols[] = {MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA
};
c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols, null, null, null);
String imagePath = Environment.getExternalStorageDirectory() + "/audio/recording.3gp`";
File f=new File(imagePath);
Uri uri = Uri.fromFile(f);
List<String> data = new ArrayList<String>();
for (int i = 0; i < c.getColumnCount(); i++){
data.add(String.valueOf(f));
}
CustomAdapter customAdapter = new CustomAdapter(this,data);
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
适配器
public class CustomAdapter extends BaseAdapter {
private final Context context;
private final List<String> items;
private final Map<View, Map<Integer, View>>
cache = new HashMap<View, Map<Integer, View>>();
public CustomAdapter(Context context, List<String> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return getItem(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView tv;
ImageView iv;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.item, parent, false);
}
Map<Integer, View> itemMap = cache.get(v);
if (itemMap == null) {
itemMap = new HashMap<Integer, View>();
tv = (TextView) v.findViewById(android.R.id.text1);
iv = (ImageView) v.findViewById(R.id.imageView);
itemMap.put(android.R.id.text1, tv);
itemMap.put(R.id.imageView, iv);
cache.put(v, itemMap);
} else {
tv = (TextView) itemMap.get(android.R.id.text1);
iv = (ImageView) itemMap.get(R.id.imageView);
}
final String item = (String) getItem(position);
tv.setText(item);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,
String.format("Image clicked: %s", item),
Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_width="match_parent" />
itm.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:id="@android:id/text1"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:id="@+id/imageView"
android:layout_height="wrap_content"
android:clickable="true" />
答案 0 :(得分:1)
您尚未将任何数据初始化为列出“数据”
你必须在for循环中初始化数据。
List<String> data = new ArrayList<String>();
for (int i = 0; i < c.getColumnCount(); i++){
}
CustomAdapter customAdapter = new CustomAdapter(this,data);
用以下代码替换for循环
if (c!=null && c.moveToFirst()) {
do{
String audio_name_with_path = c.getString(2).toString();
data.add(audio_name_with_path);
}while(c.moveToNext());
}