Need help for android app

0 pulak malhotra · March 8, 2015
Hey guys i am newbie at making android apps and  i am planning to make a calender app in which there is different list views(with string array adapter) for each month and the the list changes to the next month when i click a button.Which methods/functions do i need to use to make this work?Guys please guide me.

p.s. sorry for the bad English or any typos

Edit-
i searched a lot n i think changing adapter  with setAdapter can do it...
can someone give me a code of  sample program which has a simplelist(even of two items which changes text on button click with setAdapter?
Please i am not able to get it work

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 8, 2015
package com.myteams.myteamsapp;

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.myteams.myteamsapp.R;
import com.myteams.myteamsapp.DatabaseHandler;

public class MySearchView extends SherlockListActivity
{
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.my_search_view_menu, menu);
return (super.onCreateOptionsMenu(menu));
}

public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.action_settings:
changeDisplaySettings();
break;
case R.id.homeIcon:
callMainActivity();
break;
case R.id.addNewTeamIcon:
addNewTeam();
break;
}
return super.onOptionsItemSelected(item);
}
ImageView abs__search_button;
TextView abs__search_badge;
EditText searchQuery;
ImageView abs__search_close_btn;
TextView teamId;
ListView listView; //this refers to the listview in the xml layout file
private String searchterm;
private ArrayList<Team> searchResults;
private OnClickListener listener=new OnClickListener
()
{

@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.abs__search_button:
search();
break;
case R.id.abs__search_close_btn:
clearSearchTerm();
break;
default:
break;
}

}

};
DatabaseHandler databaseHandler = new DatabaseHandler(this);

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.my_search_view);
abs__search_button=(ImageView)findViewById(R.id.abs__search_button);
abs__search_badge=(TextView)findViewById(R.id.abs__search_badge);
searchQuery=(EditText)findViewById(R.id.searchQuery);
abs__search_close_btn=(ImageView)findViewById(R.id.abs__search_close_btn);
listView=getListView(); //this initializes the listview
initializeListeners();
}

public void callMainActivity()
{
Intent theIntent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(theIntent);
}

public void initializeListeners()
{
abs__search_button.setOnClickListener(listener);
abs__search_close_btn.setOnClickListener(listener);
}

public void search()
{
setListAdapter(null); //this resets the adapter since it will be needing a new list
searchterm=searchQuery.getText().toString();
if(searchResults!=null) //this makes sure that all entries are removed
searchResults.removeAll(searchResults);
if(isSearchable())
{
searchResults = databaseHandler.getAllSearchResults(searchterm);

if (searchResults.size() >= 0)
{

listView.setOnItemClickListener(new OnItemClickListener() //this is what executes when an entry on the listview is clicked
{

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) //this is the actual part that overwrites this method
{

   teamId= (TextView) view.findViewById(R.id.teamName);
   teamId.setText("Blue"); //I changed the code to show what you need to do. 
}
});

ListAdapter adapter = new TeamAdapter(this,searchResults,R.layout.team_entry); //this adds the list to the adapter
setListAdapter(adapter); //this sets the adapter of the listview
}
}
}

public boolean isSearchable()
{
for(int position=0;position<searchterm.length();position++)
{
Character testedCharacter=searchterm.charAt(position);
testedCharacter=Character.toLowerCase(testedCharacter);
switch(testedCharacter)
{
case ' ':
break;
default:
return true;
}
}
return false;
}

public void changeDisplaySettings()
{
Intent theIntent = new Intent(getApplicationContext(),TeamDisplaySettings.class);
startActivity(theIntent);
}

public void addNewTeam()
{
Intent theIntent = new Intent(getApplicationContext(),addNewTeam.class);
startActivity(theIntent);
}

public void clearSearchTerm()
{
searchterm="";
searchQuery.setText(searchterm);
}
}





//This is the code used to implement TeamAdapter which creates an adapter for the listview to take in and customize

TeamAdapter.java

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RatingBar;
import android.widget.TextView;

public class TeamAdapter extends ArrayAdapter<Team> //done to make it work for all listviews using inheritance
{
private final Context m_context;
private final ArrayList<Team> m_teamArrayList;
private int m_layoutId;

public TeamAdapter(Context context, ArrayList<Team> teamArrayList, int layoutId) {

super(context,layoutId, teamArrayList);
m_layoutId=layoutId; //gets the layout id
m_context= context; //gets the context
m_TeamArrayList=teamArrayList; //gets the list of entries
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Team team=m_teamArrayList.get(position); //think about this part as essentially populating each team entry in the list where this code is essentially implemented by the superclass using inflation
LayoutInflater inflater = (LayoutInflater) m_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //this creates a layout inflator
View view = inflater.inflate(m_layoutId, parent, false); //this inflates the view
if(m_layoutId==R.layout.team_checkable_entry) //this determines the specific view I'm using the adapter for
{
TextView teamId= (TextView) view.findViewById(R.id.teamId); //this populates the view that was created for this entry
TextView teamName= (TextView) view.findViewById(R.id.teamName);
}
return view; //this returns the view created for each entry
}
}
  • 1

Java / Android Development

107,178 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator