I'm writing a news-app and I'm having some trouble with displaying a custom list. All I want is that list items have 2 TextViews in them:
News-Title and
News-Description
These are contained in 2 static arrays: homeScreen.title[] and homeScreen.descriptionLong[].
Lower you have my code for the HashMap and the Adapter:
final static ArrayList> data = new ArrayList>();
static{
HashMap<String, String> row = new HashMap<String, String>();
for (int i = 0; i<HomeScreen.arrayLength; i++){
row.put("Title", HomeScreen.title[i]);
row.put("Description", HomeScreen.descriptionLong[i]);
data.add(row);
}
}
SimpleAdapter adapter = new SimpleAdapter(this,
data,
R.layout.mainmenu,
new String[] {"Title", "Description"},
new int[] { R.id.textView1, R.id.textView2});
setListAdapter(adapter);
}
public void onItemClick(SimpleAdapter arg0, View arg1, int position,
long id) {
selectedNews = position;
Toast.makeText(getApplicationContext(), "This is: " + selectedNews, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainMenu.this, ReadNews.class);
startActivity(intent);
}
The problem I'm having is that it only displays my 20'th (last) news information and also the default OnItemClick isn't working anymore. I'd appreciate your help...
You should put the row instantiation inside the for loop:
for (int i = 0; i<HomeScreen.arrayLength; i++){
HashMap<String, String> row = new HashMap<String, String>();
row.put("Title", HomeScreen.title[i]);
row.put("Description", HomeScreen.descriptionLong[i]);
data.add(row);
}
Update:
Your OnItemClickListener registration should look something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// your code here
}
});
Related
this is the script :
public class LecturAct extends ListActivity {
private static final String AR_ID = "id_lec";
private static final String AR_LC = "lec_nm";
private static final String AR_CLS = "class";
private static final String EXTRA_DATA = "idr";
JSONArray lec = null;
ArrayList<HashMap<String, String>> lec_list = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_lec);
Intent quiz = getIntent();
String id = quiz.getStringExtra(EXTRA_DATA);
String link_url = "http://xxxx/xxxx.php?id="+id;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.gtJson(link_url);
try {
lec = json.getJSONArray("lec");
for(int i = 0; i < lec.length(); i++){
JSONObject ar =lec.getJSONObject(i);
String lectid = ar.getString(AR_ID);
String lecname = ar.getString(AR_LC);
String class = ar.getString(AR_CLS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(AR_ID, lectid);
map.put(AR_LC, lecname);
map.put(AR_CLS, class);
lec_list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.adapter_listview();
}
ive tried to make a clickable item on my listview, this is the script:
public void adapter_listview() {
ListAdapter adapter = new SimpleAdapter(this, lec_list,
R.layout.list_item,
new String[] { AR_ID, AR_CLS, AR_LC}, new int[] {
R.id.title, R.id.content, R.id.code});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cd = ((TextView) view.findViewById(R.id.code)).getText().toString();
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
});
}
but when i click an item, it keep take me to the wrong direction, always take me to the last item. if the items on the list are
physic
biology
sport
social
math
then when i click biology or whatever i choose, it will always lead me to the "math" page. so what should i do? am i using a wrong adapter? please help me, give me solution to make this right :(
This answer is like Amin's but with few differences. Sample code:
#Override
protected void onListItemClick (ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mlecture p = (String) l.getItemAtPosition(position);
...
in.putExtra(AR_ID, lecture);
startActivity(in);
}
Notes:
Get the ListView parameter (l), and get the row/item position to begin with.
You cannot use view.findViewById method since ListView (l) already has the UI object.
Use the override onListItemClick since ListActivity is used.
Just override protected void onListItemClick (ListView l, View v, int position, long id) in LecturAct for example
protected void onListItemClick (ListView l, View v, int position, long id) {
String cd = getListAdapter().getItem(position);
Intent in = new Intent(LecturAct.this, Quis.class);
in.putExtra(AR_ID, cd);
startActivity(in);
}
I reviewed your code more closely. I suspect the data is not arranged correctly.
Change FROM:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.title, R.id.content, R.id.code});
TO:
new String[] { AR_ID, AR_CLS, AR_LC},
new int[] { R.id.code, R.id.title, R.id.content });
I switched R.id.code. I think this is compatible with in.putExtra(AR_ID, cd). There is no good way for me to make sure since I don't know Quiz.class.
My example like yours.Just change your object casting.Or String.
catalogListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object obj=(Object)((String) parent.getAdapter().getItem(position));
String ss=obj.toString();
Intent i = new Intent(getActivity(), PdfReader.class);
i.putExtra("catalogPdfUrl", "" +ss;
getActivity().startActivity(i);
}
});
So I'm trying to make a basic foreground for another app I'm making.
I'm just trying to get a listView to populate on screen within my fragment. (To create swipe-able tab views)
I keep getting a nullPointerException on this line here:
List.setAdapter(Adapter);
Here's my entire class:
public class List_View extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listviewtest, container, false);
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
List<HashMap<String, String>> Data = new ArrayList<HashMap<String, String>>();
{
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put("Item", "Item 1");
Data.add(temp1);
HashMap<String, String> temp2 = new HashMap<String, String>();
temp2.put("Item", "Item 2");
Data.add(temp2);
HashMap<String, String> temp3 = new HashMap<String, String>();
temp3.put("Item", "Item 3");
Data.add(temp3);
}
SimpleAdapter Adapter = new SimpleAdapter(this.getActivity(),
Data, R.layout.custom_row_view, new String[] {
"Item" }, new int[] { R.id.text1});
List.setAdapter(Adapter);
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 1:
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
getActivity());
dlgAlert.setMessage("Set Message Text");
dlgAlert.setTitle("Set Message Title");
dlgAlert.setNeutralButton("View info",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();
break;
}
}
});
return rootView;
}
}
So far as I can tell this shouldn't be returning null..
I'm thinking your inflate operation is returning null. Check this line
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
You should be inflating your ListView from your parent view of your fragment, not from the parent activity.
Should be something like this instead
ListView List = (ListView) rootView.findViewById(R.id.listView1); and make sure that
I am using onItemClickListener() with a List. I want to pass name of the item clicked on, and an arbitrary number to next instance of the list. How can this be done?
Edit: here is the class:
public class ListViewA extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"col_1"};
int[] to = new int[] {R.id.item2};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("col_1", "col_1_item_" + i);
fillMaps.add(map);
lv.setOnItemClickListener(onListClick);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
Intent i=new Intent(ListViewA.this,ListViewA.class);
startActivity(i);
}
};
}
Following example shows you how to pass data onItemClick for particular position.
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
HERE YOU GOT POSITION FOR PERTICULAR ITEM
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("position", fillMaps.get(position));
startActivity(i);
}
});
And yes you need to declare fillMaps as globally.
public class ListViewA extends Activity{
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);......
Intent i=new Intent(ListViewA.this,ListViewA.class);
i.putExtra("Key", fillMaps.get(position));
startActivity(i);
You can use above code in your onItemClickListner.
In this method the third agrument is position. So you have to use this eg. The Arraylist you are passing to your listView contains the data shown on listView. so use this kind of code
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String data= mArrlist.get(position);
}
If you are using the arralist of String type otherwise If you are using arraylist of getter setter class then use
mArralist.get(potion).getName();
You should get item #paticular position
HashMap<String, String> item = adapterview.getAdapter().getItem(position);
check your condition according to requirement and by getting data from item and process further.
Intent i=new Intent(ListViewA.this,ListViewA.class);
i.putExtra("item", item);
startActivity(i);
For adding arbitrary number you can use the Random class of java as below
Random mRandom = new Random();
int random = mRandom.nextInt(mMoveToList.size());
random variable of int type return every time random value.
you can use getItem(int position) of SimpleAdapter
put your SimpleAdapter variable adapter as global
in your ClickListener call adapter.getItem(position)
Hi I have a question. Wondering how do I get certain values from the OnItemClick method. I can get position but seen as I have more then 1 textview in this position is it possible to get the first textview?
Heres my code
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0; i<poi.length; i++){
map = new HashMap<String, String>();
map.put("first", poi[i][0]);
map.put("second",poi[i][1]);
mylist.add(map);
}
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"first", "second"}, new int[] {R.id.FIRST, R.id.SECOND});
list.setAdapter(mSchedule);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.i("owen", Integer.toString(position));
//my code to get "first" string on click
}
Thanks
If you know the resource identifier for the desired textview, you can take the view passed into the onitemClick() method and find that child:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
TextView myText = (TextView) v.findViewById(R.id.my_textview);
Log.i("owen", myText.getText().toString());
}
The position relates to the row position in the AdapterView list.
I am new to Android development. I am trying to populate a spinner by using the SimpleAdapter. But spinner's list is showing blank element. When I click any element, its text is shown properly in Toast. Please tell me what is the problem in my code below.
public void onCreate(Bundle savedInstanceState) {
private List<Map<String, String>> data = new ArrayList<Map<String, String>>();
String[] from = new String[] { "colorsData" };
int[] to = new int[] { R.id.spinner };
String[] colors = getResources().getStringArray(R.array.colorsData);
for (int i = 0; i < colors.length; i++) {
data.add(addData(colors[i]));
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, android.R.layout.simple_spinner_item, from, to);
simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(simpleAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(
parent.getContext(),
"Selected Color:- "
+ parent.getItemAtPosition(position),
Toast.LENGTH_LONG).show();
}
});
}
private Map<String, String> addData(String colorName) {
Map<String, String> mapList = new HashMap<String, String>();
mapList.put("colorsData", colorName);
return mapList;
}
I'm about 95% sure that your to array should be declared as:
int[] to = new int[] { android.R.id.text1 };
Give that a try.
EDIT (based on comments below):
It seems there was a bug in older versions of AndroidOS which caused that IllegalStateException. (I didn't see the exception in 2.2, but I did see it in 1.5 in the emulator.) The bug can be worked around by adding a ViewBinder to the SimpleAdapter. ViewBinder isn't hard to implement; here's an example:
SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// We configured the SimpleAdapter to create TextViews (see
// the 'to' array), so this cast should be safe:
TextView textView = (TextView) view;
textView.setText(textRepresentation);
return true;
}
};
simpleAdapter.setViewBinder(viewBinder);
I blogged about this here.