在Android中单击自定义ListView(图像和文本)

时间:2014-05-22 10:56:44

标签: android android-listview onclick customization

我正在创建简单的单位转换器。在我的主屏幕上,我想给ListView提供图像和文本。当用户点击ListView中的任何元素时,我不知道如何创建另一个活动。

这是我的代码。 MainActivity.java

package learn2crack.customlistview;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;


public class MainActivity extends Activity {
ListView list;
String[] web = {
            "Volume",
        "Temperature",
        "Length",
        "Bits/Bytes",
        "Mass/Weight",

} ;
Integer[] imageId = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    CustomList adapter = new
            CustomList(MainActivity.this, web, imageId);
    list=(ListView)findViewById(R.id.list);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(MainActivity.this, "You Clicked at " +web[+     position], Toast.LENGTH_SHORT).show();

                }
            });




}

}

3 个答案:

答案 0 :(得分:0)

从以下活动开始新活动:

Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);

答案 1 :(得分:0)

试试这个,

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent;
                if(position==0){
                    intent = new Intent(this, Activity1.class());
                } else if(position==1){
                    intent = new Intent(this, Activity2.class());
                } else if(position==2){
                    intent = new Intent(this, Activity3.class());
                } else if(position==3){
                    intent = new Intent(this, Activity4.class());
                } else if(position==4){
                    intent = new Intent(this, Activity5.class());                        
                }
                startActivity(intent);
            }
        });

我希望这会对你有所帮助。另外,请不要忘记在activity文件中为您的班级添加manifest

答案 2 :(得分:0)

尝试以下代码:

        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
               Intent intent=new Intent(MainActivity.this, NextActivity.class);
               startActivity(intent);

            }
        });