I'm trying to use PullToRefreshListView instead my standard ListView however, it did not work for me as a beginner, the listView is using an adapter and inflating xml item file into the listview,
when i replaced my listview wih
</com.handmark.pulltorefresh.library.PullToRefreshListView>
the listview becomes empty, no items are loaded
onItemClickListener()
when i replace the listview from ListView to PullToRefreshListView, i don't get the getItemAtPosition option,
so how can i add an onItemClickListener() if i can't get the position?
list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="#+id/CarsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
my Activity.xml extends Activity
public class MainActivity extends Activity implements AsyncCallBack {
PullToRefreshListView myList;
CustomList cl;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (PullToRefreshListView) findViewById(R.id.CarsList);
...
That's the part of the code... getApiJsonArray is a listener which gets excuted on onPostExecute()
#Override
public JSONArray getApiJsonArray(JSONArray results) {
if (dialog != null) {
dialog.dismiss();
}
ArrayList<CarsItems> carsItems;
try {
carsItems = getCarsList(results);
cl = new CustomList(this, carsItems);
myList.setAdapter(cl);
myList.setOnItemClickListener(new OnItemClickListener() {
problem is in dimensions, the fill_parent is the problem, i check and it worked
By default when you copy and paste the pulltorefresh code into your project you get this :
<com.markupartist.android.widget.PullToRefreshListView
android:id="#+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
i hesitated to ask someone on stackoverflow cuz i frustrated with importing it and updating the support version.....etc
however when you copy the code directly into your project you get a blank list or no list at all
simply change the height and width to something else like match_parent, this should be a straight forward answer for beginners like me.
and if you get a problem not finding the getItemAtPosition() for the setOnItemClickListener() method in the list try to call it inside that method (the setOnItemClickListener())
like that
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object item = a.getItemAtPosition(position);
..
hope this will help beginners like me! :)
try to trace your code
Log.i("MyApp","Results = "+results.toString());
carsItems = getCarsList(results);
inside CustomList after you set the items inside the adapter add
notifyDataSetChanged(); <-- without this no elements will appear
Related
Below is my code and image. I am populating the listview with the arraylist in runOnUIThread() in onpostexecute() which has values gotten from the remote server in the doInBackground(). But the thing is elements are visible only when the focus is on particular item. I have been trying with different things to set the elements visible, but all have gone vain. Can someone please suggest me how to get the items visible. Note: I can't extend the ListActivity, as I have another class that needs to be extended which is the subclass of an activity.
runOnUiThread(new Runnable() {
public void run() {
//Updating parsed json data to Listview
ListView listView = (ListView)findViewById(R.id.listsubcategory);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, subCategoryList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedSubcategory = subCategoryList.get(position);
Toast.makeText(getApplicationContext(), "You clicked on "+selectedSubcategory, Toast.LENGTH_SHORT).show();
}
});
}
});
The problem is the styling of the list items. In normal state your items have white background and white text color, therefore you can't see them. When the state changes to focused the colors change. You can easily solve the problem by using your custom list item instead of the system's android.R.layout.simple_list_item_1.
Define a layout for the item, it can be something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/my_item_background"
android:textColor="#color/my_text_color"
/>
Now, if the item's layout is res/layout/my_list_item.xml, create the adapter this way:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(),
R.layout.my_list_item, subCategoryList);
I am developing an Android app and one of its use cases is to display the following situation.
I have a list of links (L). Each of these links is the URL for a
picture in the Internet;
I have to download each picture of (L) and display it in a ListView. There should be two rows in the ListView(s), where I can insert the pictures. I want to do something similar to this app;
I have to display the pictures in a HorizontalScrollView;
The pictures have to be downloaded on demand, in other words, I just
download the picture using a Thread when the HorizontalScrollView is
in a position that shows this picture (similar to this
situation).
My questions:
Is it possible to insert an ListView in a HorizontalScrollView? (If yes, how do I do it?)
How do I use HorizontalScrollView? I mean, is there any difference on how I use a ListView inside a ScrollView?
Do you know any plugin/project that has the same purposes?
Question 1 - Perhaps you should re-think your design to use a list of HorizonzalListView.
Question 2 - You can created a list of HorizontialListView programmatically, place them inside a LinearLayout wrapped by a vertical scroll view.
Your myhlist.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/list_of_hlist_placeholder"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"/>
</ScrollView>
You activity:
public class ListOfHListlActivity extends Activity {
/** List of ArrayAdapter with each bind to a HorizontialListView created programmatically */
private List<MyAdapter> myAdapters = new ArrayList<MyAdapter>();
/** List of your data model */
private List<Object> myDataList;
/**
* Worker thread running in background doing dirty job.
*/
private class DoDirtyJobAsyncTask extends AsyncTask<Void, MyAdapter, Void> {
#Override
protected Void doInBackground(Void... params) {
// do your dirty job here, to populate myDataList
for (Object myData : myDataList) {
MyAdapter myAdapter = new MyAdapter(myData);
myAdapters.add(myAdapter);
publishProgress(myAdapter);
}
return null;
}
#Override
protected void onProgressUpdate(MyAdapter... myAdapters) {
int currViewId = 1;
for (final MyAdapter myAdapter: myAdapters) {
HorizontialListView listview = new HorizontialListView(getApplicationContext(), null);
listview.setId(currViewId);
listview.setAdapter(myAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// I am clickable.
}
});
RelativeLayout listOfHListLayout = (RelativeLayout) findViewById(R.id.list_of_hlist_placeholder);
// don't forget set height here, you know the height issue in HorizontialListView
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, 40);
listOfHListLayout.addView(listview, layoutParams);
currViewId++;
}
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new DoDirtyJobAsyncTask().execute();
setContentView(R.layout.myhlist);
}
}
You might be interested in this project http://www.dev-smart.com/archives/34
It talks about implementing the Horizontal ListView in Android without the center locking of the Gallery widget
May be its too late but now android has RecylerView which is way more efficient than Listview and also supports horizontal scroll.
My application consists of 2 activities.The first activity contains a TextView,if you click on it, you move to the second activity that consists of a ListView and a button Done. The ListView contains TextViews with a CheckBox. If you click on button then it finish activity and returns selected text item. If you go back to the list view the selected checkboxes restored.
Code would be appreciated.
First of all I suggest you to use CheckedTextView control instead of a CheckBox & TextView. CheckedTextView serves as the combination of checkbox and textview and is easy to handle and implement.
Secondly, you should have an ArrayList of boolean of the exact size as the ListView no. of Items. Then you can set the ArrayList items accordingly in the OnListItemClick function of ListView. At any time and anywahere in your code, you can get the reference of your selection of the ListView. Its simple and efficient.
Here is a sample OnListItemClick code:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
arrCheckBox.set(position, !arrCheckBox.get(position));
CheckedTextView ctvListItem = (CheckedTextView)v.findViewById(R.id.ctvCustomLVRowID);
ctvListItem.setChecked(arrCheckBox.get(position));
}
Here arrCheckBox is a boolean ArrayList which is keeping record of our selection and size of this array is same as no. of ListItems. I hope now you can figure it out.
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
Java code:
public class ListViewMultipleChoiceExample extends Activity {
private ListView lView;
private String lv_items[] = { "Android", "iPhone", "BlackBerry",
"AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
"Us", "UK", "India" };
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.ListView01);
// Set option as Multiple Choice. So that user can able to select more
// the one option from list
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, lv_items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
I'm having trouble with the setListAdapter(). It tells me to just create the method because it doesn't know anything about it. I'm, for now, just trying to get a list to populate and I don't even know what this code is doing.
public class PassScreen extends Activity {
TextView selection;
ArrayList<String> items = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.passwordscreen);
selection=(TextView)findViewById(R.id.selection);
try {
InputStream in=getResources().openRawResource(R.raw.words);
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in,null);
NodeList words=doc.getElementsByTagName("word");
for (int i =0;i<words.getLength();i++){
items.add(((Element)words.item(i)).getAttribute("value"));
}
in.close();
}
catch (Throwable t){
Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
}
public void onListItemClick(ListView parent, View v, int position, long id){
selection.setText(items.get(position).toString());
}
}
As you can see I have and xml file that I'm using. That looks just like what the book looks like but then again I copy and pasted the setListAdapter() so I guess that's not all that helpful.
If you could also show me what the setListAdapter() is doing that would be great. I can't seen to understand what google is talking about.
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
And some of the words xml:
<words>
<word value="lorem"/>
<word value="ipsom"/>
<word value="dolor"/>
</words>
Also can you explain what's going on? I don't understand the setListAdapter() at all. Or just point to Googles white paper. I can't find it. It's not like I know how to look that stuff up anyway.
setListAdapter() isn't a method you can call on an Activity (it is available on ListActivity but you're not using that). You're going to have to add a ListView into your layout (/res/layout/passwordscreen) and then find it and call setAdapter on that.
e.g.
ListView lv = (ListView) findViewById(R.id.mylistview);
lv.setAdapter(.....);
Hey That's because of base class, your base class should be:
public class yourClassName extends ListActivity{
Not class yourClassName extends Activity{
When you call setListAdapter this must extend ListActivity probably you class just extends Activity.
I saw it on some other post and it works for me!!
I'm displaying some sql data in a list via a ListAdapter. Everything works fine except when I try to set a click listener to each item in the list. Nothing happens when I click any of the items; no error messages, it just silently fails.
public class Notes extends ListActivity implements OnClickListener {
private static final String TAG = "Notes";
private NotesData notes;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notes = new NotesData(this);
try {
Cursor cursor = getNotes();
showNotes(cursor); /* set the cursor to the listadapter */
ListView ls = (ListView) findViewById(android.R.id.list);
ls.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v,
int position,long id) {
Toast.makeText(getApplicationContext(),"clicked",
Toast.LENGTH_SHORT).show();
}
});
} finally {
notes.close();
}
}
main.xml, containing the listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/new_note_button"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/new_note"/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/empty"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/empty"/>
</LinearLayout>
Is there something I'm missing?
Nothing is missing, your code is completely correct, just replace the getApplicationContext() to "this" could probably solve the problem, as what you need to pass is a reference to the context of the current component rather than the context of the current process.
Hope it helps.
Hass.
You need to add android:focusable="false" to the button you have in your main.xml file. The button is taking the focus away from the list item.
This is solve your problem for sure.
I guess your list item is set clickable. (view.setClickable(true))
onItemClick will to be called when the list item is clickable.
I'd recommend throwing something like this in your try statement
try{
//yourstuff
} catch (Exception e) {
e.printStackTrace();
}
And then check to see what error message is thrown. It's possible that it's throwing the error when it sets the onClickListener, maybe there's something invalid, but as long as all of that is in a try statement, it's not going to give you any helpful error message. So you could also just pull as much out of the try statement as possible until you find the problem. :-)