add item in to Listview Android? - android

I am trying to add an item to ListView:
correct?
public class Main extends Activity {
ArrayAdapter <String> listAdapter ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.listView);
String[] S= new String[]{"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(S) );
listAdapter = new ArrayAdapter<String>(this,R.layout.main,planetList);
list.setAdapter(listAdapter);
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Main layout
String[] s = {"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA- Ali","YA ali","YA-Ali"};
list = (ListView)findViewById(R.id.listView); // Your activity should have listview with id listView
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); //android.R.layout.simple_list_item_1 is built-in xml file
list.setAdapter(listAdapter);
}

Related

Listview adding items from otherActivity

I'm trying to fetch the data from the other activity and adding it to the listview which is in my main acitivity
ArrayAdapter<String> arrayAdapter;
ArrayList<String> fetchList= new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.imglist);
arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.custom_textview,R.id.listtext,
myStringList );
Bundle extras = getIntent().getExtras();
if (extras != null ) {
fetchList= getIntent().getStringArrayListExtra("data");
for(int i = 0 ; i < fetchList.size() ; i++) {
filepath = fetchList.get(i);
imgfilename=filepath.substring(filepath.lastIndexOf("/")+1);
myStringList.add(imgfilename);
lv.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
}
Now my problem is when I fetch the data and the items for the first time it adds perfectly but when I go and add it for the second time the previously added items are no more seen(ie..the old items are deleted and new items are added)
I want to add the newly items to the existing items and I'm not storing any kind of listview items data at present.
Can anyone help me with this ?
ArrayAdapter arrayAdapter;
public static ArrayList fetchList= new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.imglist);
arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.custom_textview,R.id.listtext,
myStringList );
ArrayAdapter<String> arrayAdapter;
ArrayList<String> fetchList= new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.imglist);
arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.custom_textview,R.id.listtext,
myStringList );
Bundle extras = getIntent().getExtras();
if (extras != null ) {
//get some flag to notify adpter refresh
if(getIntent().getBoolean("refresh"))
arrayAdapter.notifyDataSetChanged();
}
}
}
in second activity add data to static list
MainActivity.fetchList.add(your data);
and pass bundle with flag to first activity on back
put those two lines out of loop ..
lv.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();

Android Application gets crashed

The app crash because of the following code lines.
I have two string-array in a XML file named as Telephones in a folder named values and two TextView in text.xml.
I can not figure out what's going wrong here.
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1= new ArrayAdapter<String>(this, R.layout.text, number);
listView.setAdapter(arrayAdapter1);
}
}
Your help would be appreciated.
Thank You!
I forgot to add TextView id in MainActivity java but after adding the names_txt won't show up the only one show up is numbers_txt, how fix it please?
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, R.id.names_txt, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, R.layout.text, R.id.numbers_txt, number);
listView.setAdapter(arrayAdapter1);
}
}

sqlite Viewlist error

i trying tu put some elements from database to a List view,
my problem is that when i starrt my activity, I get this:
**com.example.restaurant.Restaurant#2be2d1d0
com.example.restaurant.Restaurant#2be3d3a8**
instead of database objects.
public class Liste extends Activity{
private ListView listview;
private Button boutonAjouter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_restaurant);
listview = (ListView)findViewById(R.id.listView1);
Restaurant mcdo = new Restaurant("mcdo","brossard","take-out","fastfood","450-555-5555");
Restaurant burger = new Restaurant("burger","longueuil","take-out","fastfood","450-999-9999");
RestaurantBDD restaurantBDD = new RestaurantBDD(this);
restaurantBDD.openForWrite();
restaurantBDD.insertRestaurant(mcdo);
restaurantBDD.insertRestaurant(burger);
ArrayList <Restaurant> restaurantlist = restaurantBDD.getAllRestaurants();
restaurantBDD.close();
ArrayAdapter <Restaurant> adapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, restaurantlist);
listview.setAdapter(adapter);
boutonAjouter = (Button) findViewById(R.id.btn_nouveau);
boutonAjouter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Liste.this, Formulaire.class);
startActivity(intent);
}
});
}
}
If you want to show a list with the names of the restaurants, supposing that your Restaurant class contains a string attribute "name", you should first create an array containing the names of the restaurants and then use an ArrayAdapter <String>:
String[] values = new String[restaurantlist.size()];
for(int i=0;i<restaurantlist.size();i++) {
values[i] = restaurantlist.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);

Android: create CheckBox in activity.java

Hi i want to create dynamical number of checkboxes (it depends on the length of array "genre") in an Android app then i did:
CheckBox [] checkbox;
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
checkbox=new CheckBox[genre.length];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
for(int i=0;i<genre.length;i++){
CheckBox c=new CheckBox(this);
c.setText(genre[i]);
checkbox[i]=c;
}
ArrayAdapter<CheckBox>adapter= new ArrayAdapter<CheckBox>(this, android.R.layout.simple_list_item_1,checkbox);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setAdapter(adapter);
}
but in the listview it shows numbers of the checkboxes in memory(I guess), why?
like : Android.wiget.CheckBox#44f03160
Android.wiget.CheckBox#44f03d60
...
You'd rather use the android.R.simple_list_item_checked item layout and enable multiple-choice on the list:
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This way you don't have to create the checkboxes manually by yourself
Because of this line
c.setText(genre[i]);
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This looks like checkboxes.

How to add an Array of TextViews into a ListView?

I got this piece of code that creates new TextView, then adds it to ArrayList<View> and when it is finished adding TextViews to Array it sets adds that Array into ListView. But somehow my ListView is appearing empty. Any idea what am I doing wrong?
Here is the code:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayList<View> textvs = new ArrayList<View>();
for (int i=0; i<10;i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(""+i);
textvs.add(tv);
}
lv.addTouchables(portit); // lv is my listview
You should use ArrayAdapter. You did this the wrong way. Here is a sample:
public class ArrayAdapterDemo extends ListActivity {
String[] items = { "this", "is", "a", "really", "silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,
items));
}

Categories

Resources