【问题标题】:OnClick on customized ListVew (Image and Text) in androidOnClick on android中的自定义ListView(图像和文本)
【发布时间】:2014-05-22 10:56:44
【问题描述】:

我正在创建简单的单位转换器。在我的主屏幕上,我想为 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();

                }
            });




}

}

【问题讨论】:

    标签: android android-listview onclick customization


    【解决方案1】:

    从这样的活动开始一个新活动:

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

    【讨论】:

      【解决方案2】:

      试试下面的代码:

              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);
      
                  }
              });
      

      【讨论】:

        【解决方案3】:

        这样试试,

         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);
                    }
                });
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-19
          • 2013-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-01
          • 1970-01-01
          相关资源
          最近更新 更多