Can anyone please tell me how to fix the below code? I am trying to setonclick listner and its causing null exception
ArrayList<Contact> imageArry = new ArrayList<Contact>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
Edit
--
When I click on the image its not sending me to another activity
this my activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Latest" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_vertical"
android:text="Recomanded" />
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#4A9C67" >
</ListView>
</LinearLayout>
</ScrollView>
Your variable listView where you want to add a listener seems not initialized
Your listView is dataList not listView
You should write :
dataList.setOnItemClickListener(...)
Set onClickListener() to your ListView variable datalist. You're currently setting it to listView which exists no where else in your code.
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList.setOnItemClickListener(new OnItemClickListener() {...}
Change your code to this :
ArrayList<Contact> imageArry = new ArrayList<Contact>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList .setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
Your code were fine, the problem what you have faced is this listview is NULL because listview it's not declared, I guess you misstyped the ListView, you only have to change listview to dataList.
Related
I m raeding csv file and showin it using the listview and textview so i want set setOnItemClickListener on list view of text...and when i click on item then other activity will get open..so please tell how do i code for this..
Here is my code...
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String listItem = (String) listView.getItemAtPosition(position);
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
}
Here is catlog report..
12-08 11:17:28.630 20936-20936/com.technostark.com.myloginactivity W/ResourceType: Skipping entry 0x108035a in package table 0 because it is not complex!
Update
<RelativeLayout
xmlns:android= "schemas.android.com/apk/res/android";
xmlns:tools= "schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape">
<ListView android:id="#+id/editlistview"
android:layout_width="match_parent"
android:layout_height= "wrap_content" />
</RelativeLayout>
Here is textview xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/et_Studname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Student Name"
android:textColorHint="#660000"
android:textSize="20dp"
android:textColor="#660000"
android:clickable="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:ems="20" />
</LinearLayout>
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView c = (TextView) view.findViewById(R.id.rating);
TextView d = (TextView) view.findViewById(R.id.title);
TextView e = (TextView) view.findViewById(R.id.releaseYear);
//Toast.makeText(Video_Main_List.this,Config.YOUTUBE_VIDEO_CODE, Toast.LENGTH_SHORT).show();
Intent launch = new Intent(Video_Main_List.this,Youtube_Player_Activity.class);
launch.putExtra("c", c.getText().toString());
launch.putExtra("d", d.getText().toString());
launch.putExtra("e", e.getText().toString());
startActivity(launch);
}});
// you can also take value from listview items and send into second activity that i mansion above.
ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
I'm trying to create a app where you can add strings to a list from another activity, but every time i add a new string to the list it replaces the old one.
How can I fix this?
Code where the list is.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);
Button button = (Button) findViewById(R.id.addBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(ShowList.this, AddToList.class));
}
});
Intent i = getIntent();
Bundle b = i.getExtras();
if(b!=null) {
String textToList = b.getString("listText");
List<String> stuff = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stuff);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
adapter.add(textToList);
adapter.notifyDataSetChanged();
}
}
the xml files code:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".ShowList">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/addBtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="List:"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_centerHorizontal="true"
android:layout_above="#+id/addBtn"
android:layout_below="#+id/textView" />
Code for the file where you input text:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_list);
final Bundle b = new Bundle();
final Intent i = new Intent(AddToList.this, ShowList.class);
final EditText text = (EditText)this.findViewById(R.id.listText);
Button btn = (Button) findViewById(R.id.btnAdd);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s = text.getText().toString();
b.putString("listText", s);
i.putExtras(b);
startActivity(i);
}
});
}
And finally the xml code for the previous peace of code:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.mathias.listapp.AddToList">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listText"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAdd"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
This happens because you are creating a new List everytime you enter in your activity. So the field in which you save your list has to be a "permanent" one. I recommend you to declare a static list and check if it's not null:
Code where your list is:
public class Nameofmyclass{
static List<String> stuff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);
if(stuff==null){
stuff = new ArrayList<String>();
}
ListView list = (ListView) findViewById(R.id.listView);
Button button = (Button) findViewById(R.id.addBtn);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stuff);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(ShowList.this, AddToList.class));
}
});
Intent i = getIntent();
Bundle b = i.getExtras();
if(b!=null) {
String textToList = b.getString("listText");
stuff.add(textToList);
}
list.setAdapter(adapter);
}
}
By the way there are several "problems" in your code:
You added the item to the adapter, not to the list
You initialized your listview only if your bundle isn't null, why? Your listview will remain everytime, you don't need to initialize it with this check
You created a final intent in your other class outside the click. This isn't programmatically wrong, but there isn't any motivation in this case for which you need to do it. It's better do it locally inside the click as you did in the first class
try this ,
String textToList = b.getString("listText");
List<String> stuff = new ArrayList<String>();
stuff.add(textToList)
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stuff);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Trying to implement the following: Two ListViews next to each other with choice mode CHOICE_MODE_SINGLE but only one of the ListViews can have a selected element at a time. So if an element is selected in one ListView, the selected element (if available) of the other ListView should be deselected.
However every time I programmatically uncheck the selected item of the other ListView, the current ListView also looses the selected item. Clicking on items shows the hover/active state but none are permanently selected.
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:listSelector="#f00"/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:listSelector="#f00"/>
</LinearLayout>
Java code:
public class TestActivity extends Activity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
final ListView list1 = (ListView) findViewById(R.id.list1);
final ListView list2 = (ListView) findViewById(R.id.list2);
final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter1.addAll("Test 1", "Test 2");
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter2.addAll("Test 3", "Test 4");
list1.setAdapter(adapter1);
list2.setAdapter(adapter2);
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
// problem
deselect(list2);
}
});
list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
// problem
deselect(list1);
}
});
}
private void deselect(final ListView list) {
list.clearChoices();
list.requestLayout();
// doesn't work either
//list.setItemChecked(list.getSelectedItemPosition(), false);
}
}
If I comment out calls to deselect() then each ListView on its own works as expected, keeping a single selected item. But that's not what I want.
Found a solution:
I had to implement a custom ListAdapter which returns custom Views implementing Checkable. Apparently ListView only restores selected/checked items properly when they implement Checkable.
You need to eliminate the onItemClickListener
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:listSelector="#f00"
android:onClick="clickListened"/>
then:
public void clickListened(View view){
deselect(list2);
}
Hopefully that will work.
In My application i am using two list view's in one activity, one for only items and one list view i am have add check box in every row.
My requirement is in below list view if item is checked i have to display on text view in the main activity if not have to display another activity .
i was not used ListActivity for this i just created from Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="60px">
<ListView android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_weight="0.5"
android:layout_height="wrap_content" />
</LinearLayout>
above list view for only items for below list view i have to add check box. how can i achieve this.
my activity is..
public class Settings extends Activity {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ListView list1 = (ListView) findViewById(R.id.list1);
ListView list2 = (ListView) findViewById(R.id.list2);
list2.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, Display));
list2.setTextFilterEnabled(true);
list2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, Categories));
list1.setTextFilterEnabled(true);
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
}
});
}
static final String[] Categories = new String[] {
"Alarm1","Alarm2","Alarm3","Alarm4","Alarm5"
};
static final String[] Display = new String[] {
"24 Hours","Display Seconds"
};
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.provider, null);
TextView location = (TextView) convertView
.findViewById(R.id.description);
checkBoxImage = (ImageView) convertView.findViewById(R.id.check_id);
Provider p = (Provider) getItem(position);
location.setText(p.Description + " - " + p.Location);
return convertView;
}
Well here is the Complete Tutorial how you can include CheckBox in a List. Its the Best Tutorial Regarding the ListView & ListActivity.
mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
lvUsers.setClickable(true);
lvUsers.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
// TODO Auto-generated method stub
Object o = lvUsers.getItemAtPosition(position);
UserBO obj = (UserBO) o;
Toast.makeText(Select.this, "Record Selected= "+obj.getId()+" "+obj.getName()+" "+obj.getEmail()+" "+obj.getContact(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(Select.this,Update.class);
startActivity(intent);
}
});
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="6px"
android:orientation="vertical"
android:layout_height="67px" android:id="#+id/rlt_main"
android:background="#E0FFFF" android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="16px" android:id="#+id/rlt_main"
android:background="#E0FFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_pid"
android:text="12"
android:layout_centerVertical="true"
android:textColor="#E0FFFF" >
</TextView>
</LinearLayout>
select.xml
<ListView
android:layout_height="wrap_content"
android:id="#+id/lv_user"
android:layout_width="fill_parent"
android:clickable="true"
>
Hi,above code is of onclick listview items selection.but when i click on item from listview nnothing is happening instead i want to call update activity.what is the mistake in my code??if anyone want to see code of XML layout then let me know..
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
Toast.makeText(Context,"Selected item is "+ position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(ListActivity.this, SecondActivity.class);
ListActivity.this.startActivity(i);
}
}
Hi I think you should try like this,
Intent intent = new Intent();
intent.setClassName(Select.this, <packagename>.Update.class.getName());
startActivityForResult(intent,0);