在listitem上膨胀布局单击以从阵列获取自定义视图

时间:2014-01-06 05:27:40

标签: android listview android-inflate

好的,我在为listitem点击进行布局时遇到了问题。我对android很新,所以请在学习阶段考虑我。我已经成功实现了自定义 经过1天的努力,listview。正如我所说,我是新手。

但是现在我想让listitem可点击,比如我点击列表项时会膨胀一个有图像视图和文本视图的布局。

每次单击时,这两个imageview和textview将根据位置单击从数组中获取值。

我可以通过使用这样的意图来做到这一点:

 @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    if (position == 0) {
        Intent int0 = new Intent(getApplicationContext(), Activity.class);
        startActivity(int0);
    }

但是假设我在数组中有1000个项目的情况,我需要让每个项目为
点击。所以在这种情况下,他们需要1000个新类和布局用于我不想要的意图。

我不知道,我怎么能解释它。但我会尝试:

请查看图片了解更多信息:

我已经实现了自定义列表视图。

enter image description here

现在我想做什么:

当用户点击项目然后获得这样的视图:意味着顶部屏幕将显示图像,底部屏幕将显示来自数组的文本。

enter image description here

以下是我的完整代码供参考: MainActivity.java

  package com.diljeet.customlistview;

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

public class MainActivity extends Activity {
    ListView list;
    String[] web = { "Diljeet", "sweet", "kaur", "preet", "manjeet", "dillun",
            "rupal" };
    Integer[] imageId = { R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4, R.drawable.image5,
            R.drawable.image6, R.drawable.image7

    };

    String textonclick[] = { "a", "b", "c", "d", "e", "f", "i", "j", "k" };

    Integer[] imageonclick = { R.drawable.r, R.drawable.ra, R.drawable.rb,
            R.drawable.rc, R.drawable.rd, R.drawable.re, R.drawable.rf

    };

    @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) {

                    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                    intent.putExtra("key",web[position]);
                    intent.putExtra("key1",imageonclick[position]);
                    intent.putExtra("key2",textonclick[position]);
                    startActivity(intent); 

                }
            });

    }

}

CustomList.java

package com.diljeet.customlistview;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String>{

  Activity context;
  String[] web;
  Integer[] imageId;
  CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}

SecondActivity.java

package com.diljeet.customlistview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class SecondActivity extends Activity {

    public void oncreate(Bundle savedInstanseState) {
        super.onCreate(savedInstanseState);
        setContentView(R.layout.getintant);

        TextView txtnew = (TextView) findViewById(R.id.get_txt);
        ImageView imgnew = (ImageView) findViewById(R.id.get_img);

        Intent i = getIntent();
        // Receiving the Data
        String name = i.getStringExtra("key");
        String email = i.getStringExtra("key2");
        txtnew.setText(name);

        Bitmap image = imgnew.getDrawingCache();
        Bundle extras = getIntent().getExtras();
        Bitmap bmp = (Bitmap) extras.getParcelable("key1");

        image.setImageBitmap(bmp);

    }

}

activity_main.xml中

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

list_single.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_height="wrap_content" />

</RelativeLayout>

getintant.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/get_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/get_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:textSize="20sp" />

</RelativeLayout>

有一件事我想说onclick我们从MainActivity获得数组的值,textonclick,imageonclick。

许多人提前感谢,请原谅我复杂的解释。

2 个答案:

答案 0 :(得分:1)

使用以下内容。您可以使用intent在活动之间传递值。

  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            intent.putExtra("key",web[position]);
            intent.putExtra("key1",imageonclick[position]);
            intent.putExtra("key2",imageId[position]);
            startActivity(intent); 

        }
    });

编辑:

对于图像

int id = i.getIntExtra("key2",R.drawable.ic_launcher);
imgnew.setImageResource(id);

http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,int)

public int getIntExtra (String name, int defaultValue)

Added in API level 1
Retrieve extended data from the intent.

Parameters
name    The name of the desired item.
defaultValue    the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.

答案 1 :(得分:0)

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {

   Intent int0 = new Intent(getApplicationContext(), Activity.class);
   int0.putExtra("text",textonclick[position]);
   startActivity(int0);
}

使用此选项将列表项值从一个活动传递到另一个活动。