Can someone please tell me how to create a custom adpater for this list, as i dont want toast to display when a user clicks the list item, but, When a user clicks on Google, he will be navigated to "www.google.com" and "www.yahoo.com" and same for msn.com,,
cant figure out at all, stuck for last 1 week, even though i know how to create a intent and call a URI but not working or right for this,
can someone just modify this please ?
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FurtherEducationCourses extends ListActivity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, FURTHER_EDUCATION));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] FURTHER_EDUCATION = new String[] {
"GOOGLE", "YAHOO", "MSN"
};
}
xml file, dunno why u required :s
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
The trick is to get the item at that position, then depends on the position (or even the value at the position), you call the correct link.
So you need to index the value like this, put this code after your static final... statement.
HashMap<String, String> valueToLink = new HashMap<String, String>;// key is Google, Yahoo, value is www.google.com
valueToLink.put("GOOGLE", "www.google.com");
//add yahoo,.etc.
In onItemClick() function, replace the toast by this:
String link = valueToLink.get(((TextView) view).getText());
//code to open the link here
Related
The title of this post says it all.
This code works without any problems:
package abc.AvailableCars;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class carListActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car_list_layout);
final ListView carListview = (ListView) findViewById(R.id.list_view);
final Button dButton = (Button) findViewById(R.id.disable_button);
String[] cars = {"Maxima GXE", "Passat", "Focus SE", "Mazda6", "Avalon", :Sentra GXE"};
final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_of_cars);
carListview.setAdapter(arrayAdapter);
dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int chosenRow = 3;
carListview.getChildAt(3).setEnabled(false);
carListview.getChildAt(3).setBackgroundColor(Color.parseColor("#3f51b5"));
}
});
}
}
This is in my listview .xml file:
<Button
android:id="#+id/disable_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable A Row"
/>
But, when I comment-out everything that belongs to the button, like below, and the Car List class is called, the app crashes with the error in the Logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference:
final ListView carListview = (ListView) findViewById(R.id.list_view);
//final Button dButton = (Button) findViewById(R.id.disable_button);
String[] cars = {"Maxima GXE", "Passat", "Focus SE", "Mazda6", "Avalon"};
final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_of_cars);
carListview.setAdapter(arrayAdapter);
//dButton.setOnClickListener(new View.OnClickListener() {
//#Override
//public void onClick(View v) {
int chosenRow = 3;
carListview.getChildAt(chosenRow).setEnabled(false);
carListview.getChildAt(chosenRow).setBackgroundColor(Color.parseColor("#3f51b5"));
}
//});
}
//}
I'm not an Android newbie anymore, but this is eluding me.
I want the chosen row to be disabled and the color set as soon as the listview is shown.
How can I do this programmatically without a button?
I have tried every variation I can think of, getView(), even a fake click.
Just in case it makes a difference, this code is in a separate class and file than the MainActivity.java file, and is called in that file.
There has to be a simple answer. What do I need to change?
Please be verbose.
Thank you.
You are calling carListview.getChildAt(chosenRow) when you set up your list view, in onCreate. Your list view is not ready yet. Try moving this code to your onResume - should look something like this:
#Override
public void onResume(){
super.onResume();
arrayAdapter.notifyDataSetChanged();
int chosenRow = 3;
carListview.getChildAt(chosenRow).setEnabled(false);
carListview.getChildAt(chosenRow).setBackgroundColor(Color.parseColor("#3f51b5"));
}
This is a pretty simple case - your chosenRow number is generated by you. You might need a custom Adapter if you need it to be algorithmic or user-driven. Have a look at this tutorial.
From my understanding, since listViews are views, they have to be Overridden for some things to be changed in them.
I chose not to disable the required rows, but check for them in code.
The complete code that works is below.
Some credit goes to Raghunandan for his/her answer at-
Android - Change background color of specific item of ListView
Again, sorry, but the indentation of the code wouldn't work correctly for some reason.
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CarListActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car_list_layout);
final ListView carListview = (ListView) findViewById(R.id.list_view);
String[] cars = {"European Cars:", "Mercedes", "Passat", "Bently", "Porsche", "BMW", "Yugo","Land Rover",
"Japanese Cars:", "Maxima GXE", "Mazda6", "Avalon", "Toyota", "Honda", ""};
final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, list_of_cars);
//------------------------------------------
carListview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_of_cars) {
// Since listViews are views, they have to be Overrdden for some things to be changed in them.
#Override
public View getView(int rowPosition, View convertView, ViewGroup parent) {
View row = super.getView(rowPosition, convertView, parent);
//------------------------------------------
// This works. I have only tried this for two rows, the two I wanted. I expected this line to crash the app, but it didn't.
if(getItem(rowPosition).equals("European Cars:") || getItem(rowPosition).equals("Japanese Cars:")) {
// Make the two rows have a white background color.
row.setBackgroundColor(Color.WHITE); // this command WORKS fine by itself.
// row.setEnabled(false); this command caused "bleeding" over into other rows, so I will check for the rows in a condition.
} // both of the getItems end here.
else {
// All of the other rows should have this color.
row.setBackgroundColor(Color.parseColor("#EEE8AA"));
// the default color
} // else ends here.
//------------------------------------------
return row;
//------------------------------------------
} // getView ends here.
}); // carListview.setAdapter ends here.
} // onCreate ends here.
} // CarListActivity ends here.
Thanks, and I hope this helps others.
I am doing an application in android. The application saves a quote or an expression in a textfield. The user has the opportunity to change this text. But I wanted that when the quote is appeared in the TextView, it is not appeared at all. I wanted only the first 5 characters of the quote to be appeared. I try to do this using the substring, but when I open the TextView nothing appears. The TextView is empty. What can I do?
Can anyone help me , please.
Thanks in advance.
This is the line where I use substring:
ListAdapter adapter = new SimpleAdapter( Quote.this,quoteList, R.layout.quote_entry, new String[] { "quoteId", "textQuote".substring(0, 4)}, new int[] {R.id.quoteId, R.id.textQuote});
And here is the entire class
package com.example.prova1;
/**This class is the page of quotes*/
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import com.example.prova1.Database;
import com.example.prova1.EditQuote;
import com.example.prova1.AddQuote;
import com.example.prova1.R;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.ListView;
public class Quote extends ListActivity {
Intent intent;
TextView quoteId;
Database quotedatabase = new Database(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quote_main);//define that the interface used is quote_main
//Store data from database in an array list
ArrayList<HashMap<String, String>> quoteList = quotedatabase.getAllItems();
//Check if there are quotes to display
if(quoteList.size()!=0) {
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
quoteId = (TextView) view.findViewById(R.id.quoteId);
String itemIdValue = quoteId.getText().toString();
Intent theIndent = new Intent(getApplication(),ShowQuote.class);
theIndent.putExtra("quoteId", itemIdValue);
finish();
startActivity(theIndent);
}
});
// Here we use ListAdapter as a bridge between ListView and the data of ListView
ListAdapter adapter = new SimpleAdapter(
Quote.this,quoteList,
R.layout.quote_entry,
new String[] {
"quoteId",
"textQuote".substring(0, 4)
},
new int[] {
R.id.quoteId,
R.id.textQuote}
);
setListAdapter(adapter);
}
}
}
I don't have the SDK installed at the moment to test your code, but I do have a couple suggestions.
First: Try the code without the substring, and see if that works.
Second: If that works, then move the substring operation to the line before and pass in the result to the SimpleAdapter.
I say this because I have the feeling that the substring is not actually your problem. And this is just a good way to test that.
I'd also check your layout "R.layout.quote_entry" and make sure there isn't anything weird going on with that. Such as you having "R.id.textQuote" actually being 'gone' and another TextView being visibly shown, etc. I had this problem once. I had two EditText fields, and only one of them was being shown, so the Activity looked right, but wasn't behaving properly.
I am trying to understand where to save the selected list view items from this code. With a dialog box you have an "ok/cancel" button option, is this possible with lists? Ideally I will be storing data from four different lists into a database on submit. In the below picture I would like to save the first three items into a database, or even an array just to start.
package com.example.lifebyfourlists;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity{
String [] seven = {
"Dark Leafy Greens" ,
"Nuts",
"Carrots",
"Green Tea",
"Whole Grains",
"Fruits"};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lstView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, seven));
}
public void onListItemClick(ListView parent, View v, int position, long id){
Toast.makeText(this, "You have selected " + seven[position], Toast.LENGTH_SHORT).show();
}
}
I am trying to understand where to save the selected list view items from this code. With a dialog box you have an "ok/cancel" button option, is this possible with lists?
You can use getCheckedItemIds() or you can write a custom Adapter to track the selected rows.
I am trying to create a ListView with CheckBox's...Th ListView should allow the user to both select an item or open that item to select other choice inside it. In other words, the ListView should be able to distinguish between the click on the checkbox and the click on the item itself.
I tried to implement it using android.R.layout.simple_list_item_multiple_choice but this one allows me to only check the checkbox even if I click outside the checkbox (on the item).
anyone can help?
Here's my code,
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewActivity extends Activity implements OnItemClickListener {
ListView directoryList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> contacts = new ArrayList<String>();
contacts.add("zaid");
contacts.add("hazem");
contacts.add("Oubai");
directoryList= (ListView) findViewById(R.id.directoryList);
final ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
contacts);
directoryList.setAdapter(arrayAdapter);
directoryList.setOnItemClickListener(this);
directoryList.setClickable(true);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
You need a customized array adapter. In your getView(), get a hold of your checkbox and set the OnCheckedChangeListener()
Here is a tutorial on how you can make your own custom adapter. The example has a clickable textbox but you can change it to work for a checkbox.
http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items
I'm developing an simple application on Android, where some items are shown on a list. The user may click on one, taking him to a further activity. Basics...
But my OnItemClickListener does not get called! I've found this very similar question, but the solution (disallow the list item view to get focus) does not work for me. However, a long click gets catched - my OnItemLongClickListener gets called. Please have a look at the following code and try it yourself. This is a simplified version of my code, showing the buggy behavior. Btw: I'm using Andriod SDK 2.0 with Eclipse 3.5.1.
package de.sacherkhoudari.listtest;
import java.util.LinkedList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
public class ListTest extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final List<ListEntry> lEntries = new LinkedList<ListEntry>();
for ( int ii = 0; ii < 10; ii++ )
lEntries.add( new ListEntry( "Entry " + ii ) );
setListAdapter( new ArrayAdapter<ListEntry>( this, R.layout.list_item, lEntries ) );
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
Toast.makeText( ListTest.this, "ItemClick at item " + lEntries.get(position).toString(), Toast.LENGTH_LONG ).show();
}
});
lv.setOnItemLongClickListener( new OnItemLongClickListener() {
public boolean onItemLongClick( AdapterView<?> parent, View view, int position, long id ) {
Toast.makeText( ListTest.this, "ItemLongClick at item " + lEntries.get(position).toString(), Toast.LENGTH_LONG ).show();
return false;
}
});
setContentView(lv);
}
}
class ListEntry {
private String name;
public ListEntry( String s ) {
name = s;
}
public String toString() {
return name;
}
}
So far the Java code... here comes the layout list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:focusable="false" >
</TextView>
Note: android:focusable="false" has no effect.
Thanks!
Sacher
When you add layouts with setContentView, views within that layouts get freshly instanciated. The ListActivity has a very simple Layout by default (read about it here), even if you don't add your own layout. So basically in your first example:
First you add a listener to the default ListView within the ListActivity
Then you throw that ListView away by using setContentView to instanciate a new layout with a new ListView
Then you never register a new listener to the new ListView from your new layout.
Whereas when you pull setContentView up in your code you only ever work with your own ListView and everything works as expected.
Move your setContentView(lv); right after retrieving the ListView
ListView lv = getListView();
setContentView(lv);
lv.setTextFilterEnabled(true);