-4

I'm new to the this excellent site and in the Java programming Android. I started to make a small test application for the listing of my favorite places in my town. I tried to follow some tutorials on different pages, but when I do I put in my project in Eclipse always gives me more than a million mistakes although imports classes and other methods.

I want to build on the example image discotheque next to the image name discos and under that name Diskotek smaller text additional info.

It would be really grateful for all the help

4

2 回答 2

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 设置 Layout Resource,所以我们这里不调用它。

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 模式和等等等等。显然您需要阅读更多内容,但我希望这有助于作为一个起点。

于 2013-06-04T21:21:38.177 回答
0

ACustom ListView总共需要 4 个基本的东西 2 个设计(layout .xml)和 2 个类(.java

2 布局
a) 基本上,一个listview带有标题或按钮的容器取决于你
b) 每行应该是什么样子,如果它们有buttons, imagestextview你想要的样子。

2 Java Class File
a) 一个是Activity你一定会拥有的。
b)根据您的要求,Custom Adapter它说明您的哪个值Activity将转到哪个View( )。Button , image

最好的例子是遵循本教程

于 2013-06-04T21:16:35.713 回答