2013-06-04 153 views
-4

我是新来的这个优秀的网站和Java编程的Android。我开始做一个小型测试应用程序,以便在我的城市中列出我最喜欢的地方。我试图在不同的页面上看一些教程,但是当我在Eclipse中放入我的项目时,虽然导入了类和其他方法,但总会给我带来超过一百万个错误。自定义Android ListView

我想建立在图像名称迪斯科舞厅旁边的示例图像迪斯科舞厅,并以该名称Diskotek较小的文本附加信息。

这将是所有帮助真的很感激

+2

帮助什么?你没有提出任何问题或给我们提供关于你遇到的问题的任何细节 – dymmeh

+0

问题是:我如何创建一个像我所解释的那样的ListView – Andro

回答

0

一个Custom ListView需要设计共4个基本的东西2(layout .xml)& 2类(.java

2布局
一)基本上是一个带有标题或按钮的listview的容器取决于你
b)每行应该如何看起来应该如何buttons,images,textview你想要什么。

2 Java类文件
一)一个是Activity,你将不得不肯定。
b)Custom Adapter其中说明根据您的要求,您的Activity中的哪个值将转到哪个ViewButton , image)。

最好的例子就是遵循这一Tutorial

+0

这正是我所要求的。先生非常感谢您! – Andro

+0

@Andro然后请做+1它:D很高兴帮助 – MDMalik

+0

对不起,我的名誉很低... – Andro

0

我认为,当有人说的第一件事,“我试图按照一些教程......”但他们不工作,它得到一种很难相信。

  • 你试过的代码在哪里?
  • 编辑器上的导入错误是什么?

这将是一个更容易解决的问题。

为了给你一个简单的ListView例如:

首先,建立在自己的偏好资源文件(的example.xml)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
<ImageView 
     android:id="@+id/disco_image" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_home" 
     android:layout_width="96dp" 
     android:layout_height="96dp"/> 
<TextView 
     android:id="@+id/disco_title" 
     android:padding="12dp" 
     android:layout_toRightOf="@+id/disco_image" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
<TextView 
     android:id="@+id/disco_info" 
     android:padding="12dp" 
     android:layout_toRightOf="@+id/disco_image" 
     android:layout_below="@+id/disco_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

然后,创建一个自定义适配器,他们是非常简单的。现在就扩展一个BaseAdapter。

public class ExampleAdapter extends BaseAdapter { 

//Let's create some constants first, to fill out the rows 
private static final String [] DISCO_NAMES = {"Disco One", "Disco Two", "Disco Three", "Disco Four"}; 
private static final String [] DISCO_INFO = {"Some Info One", "Some Info Two", "Some Info Three", "Some Info Four"}; 

private LayoutInflater mInflater; 

//Our custom adapter needs a constructor, so you can create from your activity. 
public ExampleAdapter (final Context context) { 
    //for now, let's just get the context, we'll need it to inflate views 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    //this needs to return to the amount of rows you want to display. 
    //right now we return a fixed value, this could vary based on your needs 
    return DISCO_NAMES.length; 
} 

@Override 
public Object getItem(int pos) { 
    //this is useful for knowing what item is at what position 
    //for now, let's just return the disco name shall we? 
    return DISCO_NAMES[pos]; 
} 

@Override 
public long getItemId(int pos) { 
    //This returns an id to the item 
    //personally I don't use this, so you can just return the position 
    return pos; 
} 

@Override 
public View getView(int position, View view, ViewGroup viewGroup) { 
    //Ha, here's the important part 
    //ListViews reuse rows, so let's check if the view (also known as convertview) is new or being reused 
    if (view == null) { 
     //this means it's a new view, so we need to inflate it 
     view = mInflater.inflate(R.layout.example, null); 
    } 
    ((TextView) view.findViewById(R.id.disco_title)).setText(DISCO_NAMES[position]); 
    ((TextView) view.findViewById(R.id.disco_info)).setText(DISCO_INFO[position]); 
    //You can also set some images to the imageview on the layout we created earlier 
    return view; 
    } 
} 

然后,让我们创建一个ListActivity作为示例。 注意 ListActivit不需要通过setContentView设置布局资源,所以我们不会在这里调用它。

public class ExampleListActivity extends ListActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //create the adapter 
    ExampleAdapter mExampleAdapter = new ExampleAdapter(this); 

    //fill the listView 
    setListAdapter(mExampleAdapter); 
    } 
} 

这应该就是编译,但你可能想看看ViewHolder模式等等等等性能方面的原因等等。显然你需要阅读更多,但我希望这有助于作为一个起点。

+0

谢谢先生,它有帮助! – Andro