Difference between revisions of "Dynamic List Activity"

From John Freier
Jump to: navigation, search
(Created page with 'package com.johnfreier.dailyfactorcrap; import java.util.ArrayList; import com.johnfreier.utilities.URLUtils; import android.app.ListActivity; import android.app.ProgressDialo…')
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
package com.johnfreier.dailyfactorcrap;
+
This is some example code to use ArrayList and attach it to the List view using the ArrayAdapter.
  
import java.util.ArrayList;
+
One thing I didn't know, is you could use ArrayList.
  
import com.johnfreier.utilities.URLUtils;
+
When you change the data in the ArrayList use adapter.notifyDataSetChanged() to update the list.
  
import android.app.ListActivity;
+
public class TopActivity  extends ListActivity
import android.app.ProgressDialog;
+
{
import android.os.Bundle;
+
      private ArrayAdapter<String> adapter;
import android.os.Handler;
+
      private ArrayList<String> list;
import android.os.Message;
+
 +
      public void onCreate(Bundle savedInstanceState)
 +
      {
 +
          super.onCreate(savedInstanceState);
 +
 +
          this.list = new ArrayList<String>();
 +
          adapter = new ArrayAdapter<String>(this, R.layout.top, this.list);
 +
          setListAdapter(adapter);
 +
 +
          //not for sure if I need the below.
 +
          //ListView lv = getListView();
 +
          //lv.setTextFilterEnabled(true);
 +
 +
          addItem();
 +
 +
      }
 +
 +
      public void addItem()
 +
      {
 +
          this.list.add("Two");
 +
          adapter.notifyDataSetChanged();
 +
      }
 +
}
  
import android.util.Log;
 
import android.view.Menu;
 
import android.view.MenuInflater;
 
import android.view.MenuItem;
 
import android.widget.ArrayAdapter;
 
import android.widget.ListView;
 
  
 +
Your Layout should look like this...
 +
<?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:textStyle="bold" android:textSize="20sp" android:textColor="#ffffff">
 +
</TextView>
  
public class TopActivity  extends ListActivity 
+
'''Error''' - If you get the error, something along the lines of R.id.list is not in the content. Remove the setContentView method, you don't need it
{
+
private final static String BUNDLE_USER_STRING = "USERS_TOP_STRING";
+
+
private ArrayAdapter<String> adapter;
+
private ProgressDialog dialog;
+
private ArrayList<String> list;
+
+
    public void onCreate(Bundle savedInstanceState)
+
    {
+
        super.onCreate(savedInstanceState);
+
 
+
        this.list = new ArrayList<String>();
+
        adapter = new ArrayAdapter<String>(this, R.layout.top, this.list);
+
        setListAdapter(adapter);
+
       
+
        ListView lv = getListView();
+
        lv.setTextFilterEnabled(true);
+
       
+
        this.refresh();
+
 
+
    }
+
   
+
    public void refresh()
+
    {
+
    dialog = ProgressDialog.show(TopActivity.this, "","Refreshing...", true);
+
    new Thread(new Runnable()
+
    {
+
public void run()
+
{
+
        String query = URLUtils.getSource(getString(R.string.apiTopURL));        
+
Message myMessage=new Message();
+
Bundle resBundle = new Bundle();
+
resBundle.putString(BUNDLE_USER_STRING, query);
+
myMessage.obj=resBundle;
+
handlerRefresh.sendMessage(myMessage);
+
dialog.dismiss();
+
}
+
    }).start();
+
    }
+
   
+
    private Handler handlerRefresh = new Handler()
+
    {
+
    @Override
+
    public void handleMessage(Message msg)
+
    {
+
    try
+
    {
+
        Bundle resBundle = (Bundle) msg.obj;
+
        String query = resBundle.getString(BUNDLE_USER_STRING);
+
        String[] tops = query.split("[|][|]");
+
    list.clear();
+
    for(int x = 0; x<tops.length;x++)
+
    {
+
    list.add(tops[x]);
+
    }
+
    adapter.notifyDataSetChanged();
+
    }
+
    catch(Exception e)
+
    {
+
    Log.d("refresher", e.toString());
+
    }
+
    }
+
    };
+
   
+
    @Override
+
    public boolean onCreateOptionsMenu(Menu menu)
+
    {
+
        MenuInflater inflater = getMenuInflater();
+
        inflater.inflate(R.menu.top_menu, menu);
+
        return true;
+
    }
+
   
+
    @Override
+
    public boolean onOptionsItemSelected(MenuItem item)
+
    {
+
        // Handle item selection
+
        switch (item.getItemId())
+
        {
+
        case R.id.refresh:
+
        this.refresh();
+
        return true;
+
        default:
+
        return super.onOptionsItemSelected(item);
+
        }
+
    }
+
   
+
}
+

Latest revision as of 10:21, 6 July 2011

This is some example code to use ArrayList and attach it to the List view using the ArrayAdapter.

One thing I didn't know, is you could use ArrayList.

When you change the data in the ArrayList use adapter.notifyDataSetChanged() to update the list.

public class TopActivity  extends ListActivity
{
     private ArrayAdapter<String> adapter;
     private ArrayList<String> list;

     public void onCreate(Bundle savedInstanceState) 
     {
          super.onCreate(savedInstanceState);

          this.list = new ArrayList<String>();
          adapter = new ArrayAdapter<String>(this, R.layout.top, this.list);
          setListAdapter(adapter);

          //not for sure if I need the below.
          //ListView lv = getListView();
          //lv.setTextFilterEnabled(true);

          addItem();

     }

     public void addItem()
     {
          this.list.add("Two");
          adapter.notifyDataSetChanged();
     }
}


Your Layout should look like this...

<?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:textStyle="bold" android:textSize="20sp" android:textColor="#ffffff">
</TextView>

Error - If you get the error, something along the lines of R.id.list is not in the content. Remove the setContentView method, you don't need it