请看listview + imageview基本代码示例

时间:2012-03-19 18:37:59

标签: android listview imageview

我一直在浏览所有可能的网站,但还没有找到我的需求。我需要在android中找到一个基本listview的代码示例,让我点击列表中的项目并带我到显示图像的imageview(列表中的每个项目都不同)。请为此发布一个有效的样本代码,因为它让我发疯,我无法理解:(

3 个答案:

答案 0 :(得分:0)

check that link
您需要做的就是更改OnItemClickListener()方法

答案 1 :(得分:0)

我不打算详细介绍如何创建ListView或设置适配器或任何其他内容,Web上有很多其他示例可以做到这一点,而您的问题不是询问如何设置ListView适配器;)

  • 创建扩展ListActivity

  • 的活动
  • 为新ListActivity创建一个布局,并在其中放置一个ListView (@ + id / list)我的例子

  • 列表中有什么内容? idk,但是让我们说它是一个字符串的ArrayList

现在覆盖onListItemClick

中的ListActivity
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
        if (l.getItemAtPosition(position).equals("LIST1"))
        {
                // Do something with it
        }
}

或者,您不需要ListActivity,可以设置onItemClickListener e.g。

myListView.setOnItemClickListener(new onItemClickListener()
{
    @Override protected void onListItemClick(ListView l, View v, int position, long id) {
        // Do stuff
    }
}

答案 2 :(得分:0)

好了,这是代码,首先我们得到了列表菜单。

package item.list.lol;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {

String classes[] = {"startingPoint", "lol"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String pointer = classes[position];
    try{
    Class ourClass = Class.forName("item.list.lol." + pointer);
    Intent ourIntent = new Intent(Menu.this, ourClass);
    startActivity(ourIntent);
    }catch (ClassNotFoundException e){
        e.printStackTrace();
    }
}
}

然后处理imageView的类

package item.list.lol;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class startingPoint extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    image.setImageResource(R.drawable.ic_launcher);
}}

和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="fill_parent"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.67"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

如你所见,如果按下列表中的第一项,它现在只能显示图像,如果我点击不同的项目,任何想法,我需要一种方法让它显示不同的图像?