Ok, I am facing a problem while inflating a layout for listitem click. I am very new to android so please consider me in learning Phase. I have successfully implemented the customize
listview after 1 day of effort. As I told, I am newbie.
But now I want to make a listitem clickable, like when I click on list item it inflate a layout which have a imageview and a textview.
On each click these two imageview and textview will take the value from array according to position click.
I can do this by using the intent like this :
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(), Activity.class);
startActivity(int0);
}
But let supposed the situation where I have 1000 items in array,and i need to make each item
clickable. So in this case their will need of 1000 new class's and layout for intent that i don't wants.
I don't know, that how could I explain it. But i will try:
Please see picture for more clearance :
I have implemented the customize listview.
Now what I want to do :
When a user click on item then get a view like this: means top screen will show the image and bottom screen will show the text, that are coming from array's.
Here is my complete code for reference : 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>
One thing i want to say that onclick we are getting the value from array's, textonclick,imageonclick from MainActivity.
Many many thanks in advance, and please pardon me for complex explanation.