Android onClickListener options and help on creating a non-static array adapter
Posted
by
CoderInTraining
on Stack Overflow
See other posts from Stack Overflow
or by CoderInTraining
Published on 2012-07-06T15:13:20Z
Indexed on
2012/07/06
15:15 UTC
Read the original article
Hit count: 197
I am trying to make an application that gets data dynamically and displays it in a listView. Here is my code that I have with static data. There are a couple of things I want to try and do and can't figure out how.
MainActivity.java
package text.example.project;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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 MainActivity extends ListActivity {
//declarations
private boolean isItem;
private ArrayAdapter<String> item1Adapter;
private ArrayAdapter<String> item2Adapter;
private ArrayAdapter<String> item3Adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Collections.sort(ITEM1);
Collections.sort(ITEM2);
Collections.sort(ITEM3);
item1Adapter = new ArrayAdapter<String>(this, R.layout.list_item, ITEM1);
item2Adapter = new ArrayAdapter<String>(this, R.layout.list_item, ITEM2);
item3Adapter = new ArrayAdapter<String>(this, R.layout.list_item, ITEM3);
setListAdapter(item1Adapter);
isItem = true;
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
if (isItem) {
//ITEM1.add("another\n\t" + Calendar.getInstance().getTime());
Collections.sort(ITEM1);
item2Adapter.notifyDataSetChanged();
setListAdapter(item2Adapter);
isItem = false;
} else {
item1Adapter.notifyDataSetChanged();
setListAdapter(item1Adapter);
isItem = true;
}
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
// need to turn dynamic
static ArrayList<String> ITEM1 = new ArrayList<String> (Arrays.asList( "ITEM1-1", "ITEM1-2" ));
static ArrayList<String> ITEM2 = new ArrayList<String> (Arrays.asList( "ITEM2-1", "ITEM2-2" ));
static ArrayList<String> ITEM3 = new ArrayList<String> (Arrays.asList("ITEM3-1", "ITEM3-2"));
}
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" >
</TextView>
What I want to do is first pull from a dynamic source. I need to do what is almost exactly like this tutorial... http://androiddevelopement.blogspot.in/2011/06/android-xml-parsing-tutorial-using.html ... however, I can't get the tutorial to work... I also would like to know how I can make the list item clicks so that if I click on "ITEM1-1" it goes to the menu "ITEM2-1" and "ITEM2-2". and if "ITEM1-2" is clicked, then it goes to the menu with "ITEM3-1" and "ITEM3-2"...
I am totally a noob at this whole android development thing. So any suggestions would be great!
© Stack Overflow or respective owner