如何创建具有自定义外观的列表视图?

时间:2011-05-09 07:05:26

标签: java android xml listview

  

可能重复:
  How to create my above listview to look more professional?

问题:如何在我的代码中实现具有图标,文件名和文件大小的外观和感觉,同时在每个文件对象上看起来干净简单,如示例中所示链接here ??有人能告诉我如何将布局示例合并到我的代码列表视图中吗?

有人可以指导这件事,因为我在android / java中比较新...谢谢

这是我到目前为止尝试过的,它不起作用......

public class ListViewActivity extends ListActivity {

    private List<String> videoItems = null;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.videolistview);

        File filePath = new File("/sdcard/Video_List");

        if(!(filePath.exists()))
        {
            filePath = new File(Environment.getExternalStorageDirectory() + "/Video_List");
            filePath.mkdir();
        }   
        getVideoFiles(filePath.listFiles());
     }

     private void getVideoFiles(File[] videoList) 
     {
          videoItems = new ArrayList<String>();

          for (File file : videoList) 
          {
            videoItems.add(file.getName());
          }

          setListAdapter(new ArrayAdapter<String>(this, android.R.layout.row, videoItems)); 
     }

     @Override
     protected void onListItemClick(ListView l, View v, int position, long id)
     {
         startActivity(new Intent(getApplicationContext(), DialogBox.class));
     }
}

我的row.xml文件

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:padding="6dip">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="6dip"
            android:src="@drawable/icon" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="fill_parent" >
            <TextView
                android:id="@+id/toptext"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:gravity="center_vertical" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1" 
                android:id="@+id/bottomtext"
                android:singleLine="true"
                android:ellipsize="marquee" />
        </LinearLayout>
    </LinearLayout>

更新:

公共类ListViewActivity扩展了ListActivity {

private ArrayList<Object> videoItems = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.videolistview);

    File filePath = new File("/sdcard/Video_List");

    if(!(filePath.exists()))
    {
        filePath.mkdir();
    }   
    getVideoFiles(filePath.listFiles());
 }

 private void getVideoFiles(File[] videoList) 
 {
      videoItems = new ArrayList<Object>();

      for (File file : videoList) 
      {
        videoItems.add(file.getName());
      }

      setListAdapter(new ListViewAdapter(this, R.layout.row, videoItems));  
 }

 private class ListViewAdapter extends ArrayAdapter<Object>
 {
        private ArrayList<Object> items;

        public ListViewAdapter(Context context, int textViewResourceId, ArrayList<Object> items) 
        {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }

            Object info = items.get(position);
            if (info != null) {
                TextView txtName =(TextView) v.findViewById(R.id.toptext);
                TextView txtVideoSize = (TextView) v.findViewById(R.id.bottomtext);

                txtName.setText("Name: " + info.);    
                txtVideoSize.setText("Video Size: " + info.);
            }
            return v;
        }
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id)
 {
     startActivity(new Intent(getApplicationContext(), DialogBox.class));
 }

}

0 个答案:

没有答案