1

我有 listview 从我的手机加载一些数据并显示它..我的问题是 listvew 行太小而且高度不同..有人可以给我样品吗 在此处输入图像描述

我的代码:包 info.superkiki.pocket.sampler;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Activityone extends Activity{

     ListView musiclist;
     Cursor musiccursor;
     int music_column_index;
     int count;
     MediaPlayer mMediaPlayer;

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           init_phone_music_grid();

     }

     private void init_phone_music_grid() {
           System.gc();
           String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
           musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
           count = musiccursor.getCount();
           musiclist = (ListView) findViewById(R.id.PhoneMusicList);
           musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

           musiclist.setOnItemClickListener(musicgridlistener);
           mMediaPlayer = new MediaPlayer();
     }

     private OnItemClickListener musicgridlistener = new OnItemClickListener() {
           public void onItemClick(AdapterView parent, View v, int position,
long id) {
                 System.gc();
                 music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                 musiccursor.moveToPosition(position);
                 String filename = musiccursor.getString(music_column_index);
Intent person = new Intent();
Bundle backpack = new Bundle();
backpack.putString("arnswer", filename);
person.putExtras(backpack);
setResult(RESULT_OK, person);
finish();
           }
     };

     public class MusicAdapter extends BaseAdapter {
           private Context mContext;

           public MusicAdapter(Context c) {
                 mContext = c;
           }

           public int getCount() {
                 return count;
           }

           public Object getItem(int position) {
                 return position;
           }

           public long getItemId(int position) {
                 return position;
           }

           public View getView(int position, View convertView, ViewGroup parent) {
                 System.gc();
                 TextView tv = new TextView(mContext.getApplicationContext());
                 String id = null;
                 if (convertView == null) {
                       music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                       musiccursor.moveToPosition(position);
                       id = musiccursor.getString(music_column_index);
                       music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                       musiccursor.moveToPosition(position);
                       id += musiccursor.getString(music_column_index);
                       tv.setText(id);
                 } else
                       tv = (TextView) convertView;
                 return tv;
           }
 }

}
4

1 回答 1

1

首先,为了让您自己更轻松,您应该创建一行作为布局 xml 文件。

然后在你的getView方法中做这样的事情:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View row = convertView;
  if(row == null)
  {
      LayoutInflater inflater = getLayoutInflater();
      row = inflater.inflate(R.layout.row_layout_name, parent); 
  }

  //use row.findViewById(...) to get your elements from the layout.

  …

  return v;
}

有关更多信息,请参阅此答案: 设置 ListView 项目高度

于 2013-02-15T14:33:48.287 回答