I have a ListView with two TextView that fill from DataBase but show me just one row :
MainActivity.java :
public class MainActivity extends ActionBarActivity {
public static final String DIR_SDCARD =Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE = DIR_SDCARD +"/Android/data/";
EditText editText;
DB db = new DB(MainActivity.this);
public String Titel_Drawer;
public String Titel_Drawert;
public String messageCursor;
public SQLiteDatabase sql;
public Cursor cursor;
public Cursor cursort;
public ArrayList<String> array;
public ArrayList<String> arrayt;
public static String PACKAGE_NAME;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PACKAGE_NAME = getApplicationContext().getPackageName();
File file= new File(DIR_DATABASE + PACKAGE_NAME + "/DB");
file.mkdirs();
db.GetPackageName(PACKAGE_NAME);
db.CreateFile();
try {
db.CreateandOpenDataBase();
} catch (IOException e) {
e.printStackTrace();
}
sql = db.openDataBase();
cursor = sql.rawQuery("SELECT Comment FROM WebSite_MetaDataDBBack", null);
cursort = sql.rawQuery("SELECT Title FROM WebSite_CategoryBack", null);
array = new ArrayList<String>();
if(cursor!=null)
{
if(cursor.moveToFirst())
{
do {
Titel_Drawer = cursor.getString(cursor.getColumnIndex("Comment"));
array.add(Titel_Drawer);
} while (cursor.moveToNext());
}
}
cursor.close();
arrayt = new ArrayList<String>();
if(cursort!=null)
{
if(cursort.moveToFirst())
{
do {
Titel_Drawert = cursort.getString(cursort.getColumnIndex("Title"));
arrayt.add(Titel_Drawert);
} while (cursort.moveToNext());
}
}
cursort.close();
final List<Contact> listOfContact = new ArrayList<Contact>();
listOfContact.add(new Contact(Titel_Drawert,Titel_Drawer));
ContactAdapter ConAdapter = new ContactAdapter(this,listOfContact);
ListView list = (ListView) findViewById(R.id.mainListView);
list.setAdapter(ConAdapter);
}
}
Here is my get and set in Contact.java:
public class Contact {
private String title;
private String description;
public Contact(String title, String description) {
super();
this.title = title;
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
And here is ContactAdapter.java :
public class ContactAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Contact> listContact;
public ContactAdapter(Context context, List<Contact> listContact) {
this.context = context;
this.listContact = listContact;
}
public int getCount() {
return listContact.size();
}
public Object getItem(int position) {
return listContact.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Contact entry = listContact.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_layout_main, null);
}
TextView tvContact = (TextView) convertView.findViewById(R.id.rowTextView_Main);
tvContact.setText(entry.getTitle());
TextView tvPhone = (TextView) convertView.findViewById(R.id.rowTextView_Main2);
tvPhone.setText(entry.getDescription());
return convertView;
}
#Override
public void onClick(View view) {
}
}
My activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="300dp">
</ListView>
</LinearLayout>
My drawer_layout_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/rowTextView_Main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<TextView
android:id="#+id/rowTextView_Main2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
Your listOfContact only has 1 item:
final List<Contact> listOfContact = new ArrayList<Contact>();
listOfContact.add(new Contact(Titel_Drawert,Titel_Drawer));
ContactAdapter ConAdapter = new ContactAdapter(this,listOfContact);
So your getCount() returns 1.
public int getCount() {
return listContact.size();
}
Replace your listContact in the adapter with a List that has one element for each item you want a row for.
Related
I am trying to build a listview which contains outlet address in 5 lines,along with a status indicator image and a button. The following is the functionality I am trying to build.
When the list text is clicked - I want to open a activity.
When the button (COMP) is clicked - I want to open a different activity.
The image (tickmark) is a status indicator to inform the user whether the activity in a outlet is complete or not.
The list is populated from a SqliteDatabase. On clicking the button, I want to take the reference of the outlet and show different attributes of the outlet.
How do I integrate the button into the list view.
I have written the following code so far:
LISTVIEW.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:background="#FFFFFF"
android:layout_marginLeft="2dp"
android:layout_marginBottom="2dp"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewFlag"
android:layout_rowSpan="2"
android:layout_width="25dp"
android:layout_height="25dp"
android:scaleType="fitXY"
android:layout_marginTop="20dp"
android:foregroundGravity="center"
android:layout_gravity="center"
android:background="#drawable/compliance_foreground"
android:layout_row="0"
android:layout_column="0" />
<Button
android:id="#+id/complianceButton"
android:layout_width="70dp"
android:layout_height="35dp"
android:text="COMP"
android:textSize="12sp"
></Button>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5dp"
>
<TextView
android:id="#+id/tvOutletNamealt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Outlet :"
android:textSize="15sp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#04031A"
android:textAllCaps="true"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_column="1" />
<TextView
android:id="#+id/tvOutletAddress1alt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address1 :"
android:textSize="12sp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_column="1" />
<TextView
android:id="#+id/tvOutletAddress2alt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address2 :"
android:textSize="12sp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_column="1" />
<TextView
android:id="#+id/tvOutletCityalt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City :"
android:textSize="12sp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:layout_columnWeight="1"
android:layout_row="3"
android:layout_column="1" />
<TextView
android:id="#+id/tvOutletPhonealt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PhoneNumber :"
android:textSize="13sp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:layout_columnWeight="1"
android:layout_row="4"
android:layout_column="1" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class show_outlets_alt extends AppCompatActivity {
SQLiteHelper sqLiteHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
AltOutletAdapter listAdapterOutlets ;
String lati;
String longi;
String projectName;
String usrname;
Button cButton;
ListView LISTVIEWOUTLETS;
ArrayList<String> Id_Array;
ArrayList<String> OutletName_Array;
ArrayList<String> OutletAddress1_Array;
ArrayList<String> OutletAddress2_Array;
ArrayList<String> City_Array;
ArrayList<String> Phone_Array;
ArrayList<String> ListViewClickItemArrayOutlets = new ArrayList<String>();
private String projectID;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_show_outlets_alt );
// Set toolbar for this screen
Toolbar sectoolbar = (Toolbar) findViewById( R.id.secondary_toolbar );
sectoolbar.setTitle("");
sectoolbar.setBackground(new ColorDrawable(getResources().getColor(R.color.primary_welcome_color)));
TextView main_title = (TextView) findViewById(R.id.secondary_toolbar_title);
main_title.setText("PROJECT LOCATIONS");
main_title.setTextColor( this.getResources().getColor( R.color.white ) );
setSupportActionBar( sectoolbar );
usrname = SaveSharedPreference.getUserName( show_outlets_alt.this );
projectID = SaveSharedPreference.getProjName( show_outlets_alt.this );
LISTVIEWOUTLETS = this.findViewById( R.id.listView2alt );
imageView = this.findViewById( R.id.imageViewFlag );
cButton = findViewById( R.id.complianceButton );
Id_Array = new ArrayList<>();
OutletName_Array = new ArrayList<>();
OutletAddress1_Array = new ArrayList<>();
OutletAddress2_Array = new ArrayList<>();
City_Array = new ArrayList<>();
Phone_Array = new ArrayList<>();
sqLiteHelper = new SQLiteHelper(this);
LISTVIEWOUTLETS.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
projectName = SaveSharedPreference.getProjName( show_outlets_alt.this );
String outletName = ListViewClickItemArrayOutlets.get(position).toString();
SaveSharedPreference.setOutletName( getApplicationContext(),outletName );
Intent intent = new Intent(getApplicationContext(), ShowOutletParams.class);
startActivityForResult( intent,1 );
}
} );
} //end of oncreate
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult( requestCode, resultCode, data );
if (resultCode == RESULT_OK) {
Intent refresh = new Intent( this, ShowDataActivity.class );
startActivityForResult(refresh,1);
this.finish();
}
}
#Override
protected void onResume() {
ShowSQLiteOutletdata();
super.onResume();
}
private void ShowSQLiteOutletdata() {
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
projectID = SaveSharedPreference.getProjName( show_outlets_alt.this );
cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME1+" where pid = (select projectId from "+SQLiteHelper.TABLE_NAME+ " where ProjectName = '"+projectID+"'"+") and Status=1;", null);
Id_Array.clear();
OutletName_Array.clear();
OutletAddress1_Array.clear();
OutletAddress2_Array.clear();
City_Array.clear();
Phone_Array.clear();
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
Id_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_ID)));
ListViewClickItemArrayOutlets.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_OutletName)));
OutletName_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_OutletName)));
OutletAddress1_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_outletAddress1)));
OutletAddress2_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_outletAddress2)));
City_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_city)));
Phone_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table1_Column_phone)));
} while (cursor.moveToNext());
}
}
listAdapterOutlets = new AltOutletAdapter(show_outlets_alt.this,
Id_Array,
OutletName_Array,
OutletAddress1_Array,
OutletAddress2_Array,
City_Array,
Phone_Array
);
LISTVIEWOUTLETS.setAdapter(listAdapterOutlets);
cursor.close();
} //end of ShowSqliteOutletData
#Override
public void onBackPressed() {
super.onBackPressed();
Intent backIntent = new Intent(getApplicationContext(), ShowDataActivity.class);
startActivity(backIntent);
}
}//end of public class
AltOutletAdapter.java
public class AltOutletAdapter extends BaseAdapter {
Context context;
ArrayList<String> ID;
ArrayList<String> O_Name;
ArrayList<String> O_Address1;
ArrayList<String> O_Address2;
ArrayList<String> City;
ArrayList<String> Phone;
ArrayList<String> iButton;
public AltOutletAdapter(Context context, ArrayList<String> ID, ArrayList<String> o_Name, ArrayList<String> o_Address1, ArrayList<String> o_Address2, ArrayList<String> city, ArrayList<String> phone) {
this.context = context;
this.ID = ID;
O_Name = o_Name;
O_Address1 = o_Address1;
O_Address2 = o_Address2;
City = city;
Phone = phone;
}
#Override
public int getCount() {
return ID.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View child, ViewGroup parent) {
AltOutletAdapter.Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.alt_list_view, null);
holder = new AltOutletAdapter.Holder();
holder.O_Name = (TextView) child.findViewById(R.id.tvOutletNamealt);
holder.O_Address1 = (TextView) child.findViewById(R.id.tvOutletAddress1alt);
holder.O_Address2 = (TextView) child.findViewById(R.id.tvOutletAddress2alt);
holder.City = (TextView) child.findViewById(R.id.tvOutletCityalt);
holder.Phone = (TextView) child.findViewById(R.id.tvOutletPhonealt);
child.setTag(holder);
} else {
holder = (AltOutletAdapter.Holder) child.getTag();
}
holder.O_Name.setText(O_Name.get(position));
holder.O_Address1.setText(O_Address1.get(position));
holder.O_Address2.setText(O_Address2.get(position));
holder.City.setText(City.get(position));
holder.Phone.setText(Phone.get(position));
return child;
}
private class Holder {
TextView O_Name;
TextView O_Address1;
TextView O_Address2;
TextView City;
TextView Phone;
Button iButton;
ImageView imageView;
}
}
first I have question.
why don't you make Model for Items?
Anyway if you want to make click event by element, just add click listener in your adapter.
so Example is here
add id in your whole layout.
( android:id="#+id/layout" )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:background="#FFFFFF"
android:layout_marginLeft="2dp"
android:layout_marginBottom="2dp"
android:descendantFocusability="blocksDescendants">
...
add click listener in your adapter
public class AltOutletAdapter extends BaseAdapter {
Context context;
ArrayList<String> ID;
ArrayList<String> O_Name;
ArrayList<String> O_Address1;
ArrayList<String> O_Address2;
ArrayList<String> City;
ArrayList<String> Phone;
ArrayList<String> iButton;
public LinearLayout linearLayout;
public AltOutletClickListener altOutletClickListener;
public AltOutletAdapter(Context context, ArrayList<String> ID, ArrayList<String> o_Name, ArrayList<String> o_Address1, ArrayList<String> o_Address2, ArrayList<String> city, ArrayList<String> phone, AltOutletClickListener listener) {
this.context = context;
this.ID = ID;
O_Name = o_Name;
O_Address1 = o_Address1;
O_Address2 = o_Address2;
City = city;
Phone = phone;
altOutletClickListener = listener;
}
#Override
public int getCount() {
return ID.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View child, ViewGroup parent) {
AltOutletAdapter.Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.alt_list_view, null);
holder = new AltOutletAdapter.Holder();
holder.O_Name = (TextView) child.findViewById(R.id.tvOutletNamealt);
holder.O_Address1 = (TextView) child.findViewById(R.id.tvOutletAddress1alt);
holder.O_Address2 = (TextView) child.findViewById(R.id.tvOutletAddress2alt);
holder.City = (TextView) child.findViewById(R.id.tvOutletCityalt);
holder.Phone = (TextView) child.findViewById(R.id.tvOutletPhonealt);
holder.linearLayout = (LinearLayout) child.findViewById(R.id.layout);
holder.iButton = (Button) child.findViewById(R.id.complianceButton);
child.setTag(holder);
} else {
holder = (AltOutletAdapter.Holder) child.getTag();
}
holder.O_Name.setText(O_Name.get(position));
holder.O_Address1.setText(O_Address1.get(position));
holder.O_Address2.setText(O_Address2.get(position));
holder.City.setText(City.get(position));
holder.Phone.setText(Phone.get(position));
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
altOutletClickListener.itemClickListener(v, position);
}
});
holder.iButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
altOutletClickListener.imageClickListener(v, position);
}
});
return child;
}
private class Holder {
TextView O_Name;
TextView O_Address1;
TextView O_Address2;
TextView City;
TextView Phone;
Button iButton;
ImageView imageView;
LinearLayout linearLayout;
}
public interface AltOutletClickListener {
void itemClickListener(View v, int position);
void imageClickListener(View v, int position);
}
}
when you call adapter in java, add click event as params.
ArrayList<String> test = new ArrayList<>();
AltOutletAdapter altOutletAdapter = new AltOutletAdapter(MainActivity.this, test, test, test, test, test, test, new AltOutletAdapter.AltOutletClickListener() {
#Override
public void itemClickListener(View v, int position) {
// when click layout
}
#Override
public void imageClickListener(View v, int position) {
// when click button
}
});
I just tested and use this.
But I think make Model is better.
ArrayList<String> test = new ArrayList<>();
ArrayList<Model> test = new ArrayList<>(); // it is better
**UPDATE : make Model **
simply, I make your Model Example.
make Model file.
public class AltOutlet {
private String name;
private String address1;
private String address2;
private String city;
private String phone;
public AltOutlet(String name, String address1, String address2, String city, String phone) {
this.name = name;
this.address1 = address1;
this.address2 = address2;
this.city = city;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
change your adapter
public class AltOutletAdapter extends BaseAdapter {
Context context;
ArrayList<AltOutlet> altOutletArrayList;
public LinearLayout linearLayout;
public AltOutletClickListener altOutletClickListener;
public AltOutletAdapter(Context context, ArrayList<AltOutlet> altOutletList, AltOutletClickListener listener) {
this.context = context;
altOutletArrayList = altOutletList;
altOutletClickListener = listener;
}
#Override
public int getCount() {
return altOutletArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View child, ViewGroup parent) {
AltOutletAdapter.Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.alt_list_view, null);
holder = new AltOutletAdapter.Holder();
holder.O_Name = (TextView) child.findViewById(R.id.tvOutletNamealt);
holder.O_Address1 = (TextView) child.findViewById(R.id.tvOutletAddress1alt);
holder.O_Address2 = (TextView) child.findViewById(R.id.tvOutletAddress2alt);
holder.City = (TextView) child.findViewById(R.id.tvOutletCityalt);
holder.Phone = (TextView) child.findViewById(R.id.tvOutletPhonealt);
holder.linearLayout = (LinearLayout) child.findViewById(R.id.layout);
holder.iButton = (Button) child.findViewById(R.id.complianceButton);
child.setTag(holder);
} else {
holder = (AltOutletAdapter.Holder) child.getTag();
}
holder.O_Name.setText(altOutletArrayList.get(position).getName());
holder.O_Address1.setText(altOutletArrayList.get(position).getAddress1());
holder.O_Address2.setText(altOutletArrayList.get(position).getAddress2());
holder.City.setText(altOutletArrayList.get(position).getCity());
holder.Phone.setText(altOutletArrayList.get(position).getPhone());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
altOutletClickListener.itemClickListener(v, position);
}
});
holder.iButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
altOutletClickListener.imageClickListener(v, position);
}
});
return child;
}
private class Holder {
TextView O_Name;
TextView O_Address1;
TextView O_Address2;
TextView City;
TextView Phone;
Button iButton;
ImageView imageView;
LinearLayout linearLayout;
}
public interface AltOutletClickListener {
void itemClickListener(View v, int position);
void imageClickListener(View v, int position);
}
}
when you add item in java file, use Model
AltOutlet altOutlet = new AltOutlet("test", "address1", "address2", "city", "phone");
altOutlets.add(altOutlet);
AltOutletAdapter altOutletAdapter = new AltOutletAdapter(MainActivity.this, altOutlets, new AltOutletAdapter.AltOutletClickListener() {
#Override
public void itemClickListener(View v, int position) {
// when click layout
}
#Override
public void imageClickListener(View v, int position) {
// when click button
}
});
I have a listview, from database (category list) and i want to load gridview (product gridview) when i click any button in the category listview, i'm able to make the listview and gridview, but cannot connect them, since im still new at android programming and have no clue to do it.
my main purpose is:
List category
on click on the category button will load gridview of product button
on click product button will add the product id to table cart
ps: if you notice i do SQL query in the mainActivity class, i still new at the code so i tried my best first before convert into better practice.
Any suggestion to do it?
Here i include my snippet:
Category.java (setter and getter)
public class Category {
private int _id;
private String _name;
public Category() {
}
public Category(int id, String name) {
this._id = id;
this._name = name;
}
public Category(String name) {
this._name = name;
}
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String get_name()
{
return this._name;
}
public void set_name(String name)
{
this._name = name;
}
}
CategoryListAdapter.java
public class CategoryListAdapter extends BaseAdapter {
private ArrayList<Category> listData;
private LayoutInflater layoutInflater;
public CategoryListAdapter(Context aContext, ArrayList<Category> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
CategoryListAdapter.ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_category, null);
holder = new CategoryListAdapter.ViewHolder();
holder.btnCategory = (Button) convertView.findViewById(R.id.btnCategory);
convertView.setTag(holder);
} else {
holder = (CategoryListAdapter.ViewHolder) convertView.getTag();
}
holder.btnCategory.setText(listData.get(position).get_name());
return convertView;
}
static class ViewHolder {
Button btnCategory;
}
}
listview_category.xml (layout that will be repeated)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:textSize="16dp"
android:id="#+id/btnCategory"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="#drawable/button_category"/>
</LinearLayout>
listview inside MainActivity
<ListView
android:id="#+id/listviewCategory"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
java inside MainActivity
protected void onCreate(Bundle savedInstanceState) {
...
/* LIST CATEGORY TO LOAD PRODUCT */
ArrayList list_category = getListCategory();
final ListView listview_category = (ListView) findViewById(R.id.listviewCategory);
listview_category.setAdapter(new CategoryListAdapter(this, list_category));
listview_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Log.d(TAG, "click cat");
//Give product list an id from category_id get from here
}
});
...
}
/* List Category for selecting category product */
private ArrayList getListCategory() {
SQLiteDatabase mydatabase = openOrCreateDatabase("posDb",MODE_PRIVATE,null);
Cursor resultSet = mydatabase.rawQuery("Select * from categories",null);
ArrayList<Category> results = new ArrayList<Category>();
if (resultSet.moveToFirst()) {
do {
Category categoriesData = new Category();
categoriesData.set_name(resultSet.getString(1));
results.add(categoriesData);
} while (resultSet.moveToNext());
}
return results;
}
The grid of product mostly the same as category, but load product from database which later i want to onclick the product button to save record to cart table
on Click on button will add the product id in to table_cart
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ArrayList list_category = getListCategory();
int id[]={1, 2, 3, 4, 5};
String categoryName[]={"category1","category2","category3","category4","category5"};
ArrayList<Category> listData=new ArrayList<>();
Category category;
for (int i=0; i<id.length; i++)
{
category=new Category();
category.set_id(id[i]);// setting Id
category.set_name(categoryName[i]);// setting category name
listData.add(category);
}
final ListView listview_category = (ListView) findViewById(R.id.listviewCategory);
listview_category.setAdapter(new CategoryListAdapter(this, listData ));
}
}
CategoryListAdapter.java
public class CategoryListAdapter extends BaseAdapter {
private ArrayList listData;
private LayoutInflater layoutInflater;
ArrayList table_cart = new ArrayList();
private static final String TAG = "CategoryListAdapter";
public CategoryListAdapter(Context aContext, ArrayList<Category> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final CategoryListAdapter.ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_category, null);
holder = new CategoryListAdapter.ViewHolder();
holder.btnCategory = (Button) convertView.findViewById(R.id.btnCategory);
convertView.setTag(holder);
} else {
holder = (CategoryListAdapter.ViewHolder) convertView.getTag();
}
holder.btnCategory.setText(listData.get(position).get_name());
holder.btnCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// getting product id
int product_id = listData.get(position).getID();
Log.e(TAG, "product_id: " + product_id);
// now you can get category name as well
String category_name = listData.get(position).get_name();
Log.e(TAG, "category_name: " + category_name);
// now adding product id in to table_cart
table_cart.add(listData.get(position).getID());
}
});
return convertView;
}
static class ViewHolder {
Button btnCategory;
}
}
Category.java
public class Category {
private int _id;
private String _name;
public int getID() {
return this._id;
}
public void setID(int id) {
this._id = id;
}
public String get_name() {
return this._name;
}
public void set_name(String name) {
this._name = name;
}
public void set_id(int _id) {
this._id = _id;
}
}
there is no need to change layouts
Add on click listener for category button, add grid view in fragment and open fragment on button click.
holder.btnCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProductFragment nextFrag= new ProductFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag,TAG_FRAGMENT)
.commit();
}
});
I want to add quantity when the add button is clicked and subtract quantity when sub button is clicked here i am not showing my setter/getter class(RowItem) and my xml files
(main.xml,item_details_view)
This is my Adapter class
public class ItemListBaseAdapter extends ArrayAdapter<RowItem> {
Context context;
public ItemListBaseAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
Button add,sub;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_details_view, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
holder.add = (Button) convertView
.findViewById(R.id.button1);
holder.sub = (Button) convertView
.findViewById(R.id.button2);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
holder.add.setTag(position);
holder.sub.setTag(position);
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Adds 1 to the counter
int counter=0;
counter = counter + 1;
}
});
holder.sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Subtract 1 from counter
int counter=0;
counter = counter - 1;
}
});
return convertView;
}
}
Thisi is my Activity class
public class ListViewImagesActivity extends Activity implements
OnItemClickListener {
public static final String[] titles = new String[] { "EggBurger",
"cheesBurger", "KingBurger", "Mixed" };
public static final String[] descriptions = new String[] {"Select 0 Item"
};
public static final Integer[] images = { R.drawable.burger1,
R.drawable.burger2, R.drawable.burger3, R.drawable.burger4 };
ListView listView;
List<RowItem> rowItems;
Button add,sub;
int counter=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[0]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
ItemListBaseAdapter adapter = new ItemListBaseAdapter(this,
R.layout.item_details_view, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
this is my RowItem class
public class RowItem {
private int imageId;
private String title;
private String desc;
public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return title + "\n" + desc;
}
}
this is my main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
this is my item_details_view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16dp" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/icon"
android:paddingLeft="10dp"
android:textColor="#3399FF"
android:textSize="14dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/button2"
android:text="Add" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/desc"
android:layout_alignParentRight="true"
android:text="Sub" />
</RelativeLayout>
First of all, if you want to change the quantity value, you should have a variable representing quantity which you can change. You have included quantity value in the description text, which is bad design. What you should do is have a quantity variable int in the RowItem class as shown. You desc should just return a string from the quantity value as shown:
public class RowItem {
private int imageId;
private String title;
private int quantity = 0;
public RowItem(int imageId, String title, int quantity) {
this.imageId = imageId;
this.title = title;
this.quantity = quantity;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return "Select " + quantity + " Item";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
#Override
public String toString() {
return title + "\n" + quantity;
}
}
In your activity, instead of storing descriptions, store quantities:
public static final int[] quantities = new int[]{ 0 };
In onCreate initialize the rowItems as follows
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], quantities[0]);
rowItems.add(item);
}
Finally, in your OnClickListener, get the quantity value, update it and set the new description.
final ViewHolder viewHolderFinal = holder;
final RowItem finalRowItem = rowItem;
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int quantity = finalRowItem.getQuantity(); // get the quantity for this row item
finalRowItem.setQuantity(quantity + 1); // update it by adding 1
viewHolderFinal.txtDesc.setText(finalRowItem.getDesc()); // set the new description (that uses the updated qunatity)
}
});
holder.sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int quantity = finalRowItem.getQuantity(); // get the quantity for this row item
finalRowItem.setQuantity(quantity - 1); // update it by subtracting 1
viewHolderFinal.txtDesc.setText(finalRowItem.getDesc()); // set the new description (that uses the updated qunatity)
}
});
*Note: * I have compiled and run this code. So if you do everything as I mentioned it will work.
I have this class:
public class Bookmark extends ArrayList<Bkm> {
public static Bookmark getBookmark(Context context) {
Bookmark bookmarks = new Bookmark ();
String[] titles = context.getResources().getStringArray(R.array.bookmark_titles);
String[] urls = context.getResources().getStringArray(R.array.bookmark_urls);
TypedArray icons = context.getResources().obtainTypedArray(R.array.bookmark_icons);
for (int i = 0; i < titles.length; i ++) {
bookmarks.add(titles[i], urls[i], icons.getDrawable(i));
}
return bookmarks;
}
}
The class has the "getBookmark" method that returns a "bookmarks" object, it contains the fields "titles", "urls" and "icons". How can I get these fields in my main class? I want to create a ListView with the "titles" items and access the corresponding url in a WebView.
In my main class I have this ListView
ListView lv = (ListView) findViewById(R.id.favoritos_listView);
I am trying to get the data that way
Context context = getApplicationContext();
ArrayList<Bookmark> my_array = BookmarkCollection.getTestBookmarks(context);
ArrayAdapter<Bookmark> aa = new ArrayAdapter<Bookmark>(context, R.array.bookmark_titles, android.R.layout.simple_list_item_1, my_array);
ListView lv = (ListView) findViewById(R.id.favoritos_listView);
lv.setAdapter(aa);
But the ListView does not appears.
Boomark doesn't need to extend ArrayList. What you need to do is create a custom Adapter for your Bookmark objects and use that adapter on your ListView.
First create your Bookmark model class.
public class Bookmark {
private String title;
private String url;
private Drawable icon;
public Bookmark(String title, String url, Drawable icon) {
this.title = title;
this.url = url;
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}
Then create your Bookmark Adapter
public class BookmarkAdapter extends BaseAdapter{
private ArrayList<Bookmark> bookmarks;
private Context context;
public BookmarkAdapter(Context context, ArrayList<Bookmark> bookmarks) {
this.context = context;
this.bookmarks = bookmarks;
}
public int getCount() {
return bookmarks.size();
}
public Object getItem(int position) {
return bookmarks.get(position);
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if ( row == null ) {
row = View.inflate( context, R.layout.row_bookmark, null );
}
Bookmark bookmark = (Bookmark)getItem(position);
if ( bookmark!= null ) {
TextView name = (TextView) row.findViewById( R.id.title );
if ( name != null ) {
name.setText( bookmark.getTitle() );
}
}
return row;
}
}
The row_bookmark.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Put this in your Activity xml
<ListView
android:id="#+id/bookmark_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
To populate the list in your activity
String[] titles = getResources().getStringArray(R.array.bookmark_titles);
String[] urls = getResources().getStringArray(R.array.bookmark_urls);
TypedArray icons = getResources().obtainTypedArray(R.array.bookmark_icons);
ArrayList<Bookmark> bookmarks = new ArrayList<Bookmark>();
for (int i = 0; i < titles.length; i ++) {
bookmarks.add(new Bookmark(titles[i], urls[i], icons.getDrawable(i)));
}
ListView bookmarkList = (ListView)findViewById(R.id.bookmark_list);
bookmarkList.setAdapter(new BookmarkAdapter(this, bookmarks));
To get the URL when a list item is selected you need to set the item click listener
bookmarkList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = ((Bookmark)parent.getAdapter().getItem(position)).getUrl();
}
});
After i click "more" button, it suppose will extend the listview from 10 to 20.
However after clicked, the list view did not extend and remain original size.
It only can keep the position of scroll bar. This is half of what i need.
newsid = new int[webservice.news.size()];
title = new String[webservice.news.size()];
date = new String[webservice.news.size()];
imagepath = new String[webservice.news.size()];
for (int i = 0; i < webservice.news.size(); i++) {
newsid[i] = webservice.news.get(i).getID();
title[i] = webservice.news.get(i).getNtitle();
date[i] = webservice.news.get(i).getNArticalD();
imagepath[i] = webservice.news.get(i).getImagePath();
}
adapter = new CustomAdapter_ParticularCategoryAllNews(this, title,
date, imagepath);
lv.addFooterView(footermore);
if (constant.isOnline()) {
lv.addFooterView(constant.AdMob());
}
TextView titletext = (TextView) findViewById(R.id.text_pagetitle);
titletext.setText(pagetitletext.toString());
lv.setAdapter(adapter);
This is call when launch activity.
btnmore.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
for (int i = 0; i < webservice.news.size(); i++) {
newsid[i] = webservice.news.get(i).getID();
title[i] = webservice.news.get(i).getNtitle();
date[i] = webservice.news.get(i).getNArticalD();
imagepath[i] = webservice.news.get(i).getImagePath();
}
adapter.setTitle(title);
adapter.setDate(date);
adapter.setImagepath(imagepath);
position = lv.getFirstVisiblePosition();
lv.smoothScrollToPosition(position);
adapter.notifyDataSetChanged();
This is what i call after i click "more button". However the list did not extend from 10 to 20 items.
public class CustomAdapter_ParticularCategoryAllNews extends BaseAdapter {
private Activity activity;
private String[] title, date, imagepath;
private static LayoutInflater inflater = null;
private ImageLoader_Loader imageLoader;
private WindowManager wm = null;
private Display display;
private Config_ConstantVariable constant;
public CustomAdapter_ParticularCategoryAllNews(Activity a, String[] title,
String[] date, String[] imagepath) {
activity = a;
this.title = title;
this.date = date;
this.imagepath = imagepath;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
imageLoader = new ImageLoader_Loader(activity.getApplicationContext());
constant = new Config_ConstantVariable(activity);
}
public int getCount() {
return title.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public void setTitle(String[] title) {
this.title = title;
}
public void setDate(String[] date) {
this.date = date;
}
public void setImagepath(String[] imagepath) {
this.imagepath = imagepath;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.main_particularcategoryallnewslist,
parent, false);
LinearLayout linear = (LinearLayout) vi.findViewById(R.id.layout_image);
ImageView imageview = (ImageView) vi
.findViewById(R.id.image_categoryallnewstitle);
TextView titletext = (TextView) vi
.findViewById(R.id.text_categoryallnewstitle);
TextView datetext = (TextView) vi.findViewById(R.id.text_newsdate);
if (!imagepath[position].toString().equals("no picture")) {
imageview.setVisibility(View.VISIBLE);
linear.setVisibility(View.VISIBLE);
imageLoader.DisplayImage(imagepath[position], imageview);
} else {
imageview.setVisibility(View.GONE);
imageview.setImageDrawable(null);
linear.setVisibility(View.GONE);
display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
titletext.setWidth(screenWidth);
}
if (constant.getscreenresolution() >= 800 && constant.ScreenOrientation() == 1) {
titletext.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
datetext.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
}
titletext.setText(title[position].toString());
datetext.setText(date[position].toString());
return vi;
}
}
This is the CustomAdapter class.
In your adapter add:
public void setTitle(String[] title) {
this.title = title;
}
public void setDate(String[] date) {
this.date = date;
}
public void setImagepath(String[] imagepath) {
this.imagepath = imagepath;
}
When the more button is pressed call the three methods above with arrays containing 20 objects. Then call notifyDataSetChanged on your adapter.
Update:
This is working for me:
Activity:
public class HelpProjectActivity extends Activity {
private ArrayList<Item> items;
private boolean extend = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createArray();
ListView lv = (ListView)findViewById(R.id.list);
lv.setAdapter(new HelpListAdapter());
}
private void createArray() {
items = new ArrayList<Item>();
for(int i = 0; i < 20; i++) {
Item item = new Item();
item.title = "Title " + i;
item.subtitle = "Subtitle " + i;
item.image = "default";
items.add(item);
}
}
public void morePressed(View v) {
extend = !extend;
Button b = (Button) findViewById(R.id.button);
b.setText(extend ? R.string.less_button : R.string.more_button);
ListView lv = (ListView)findViewById(R.id.list);
((BaseAdapter)lv.getAdapter()).notifyDataSetChanged();
}
private class HelpListAdapter extends BaseAdapter {
#Override
public int getCount() {
if (extend) {
return items.size();
}
return items.size()/2;
}
#Override
public Object getItem(int pos) {
return items.get(pos);
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = View.inflate(getApplicationContext(), R.layout.litst_item, null);
}
Item item = items.get(pos);
TextView titleText = (TextView) v.findViewById(R.id.list_item_title);
titleText.setText(item.title);
TextView subtitleText = (TextView) v.findViewById(R.id.list_item_subtitle);
subtitleText.setText(item.subtitle);
ImageView image = (ImageView) v.findViewById(R.id.list_image);
if (item.image.equalsIgnoreCase("default")) {
image.setImageResource(R.drawable.default_list_image);
} else {
// what ever
}
return v;
}
}
}
main.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"
android:orientation="vertical" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:onClick="morePressed"
android:text="#string/more_button" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/list_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_gravity="center_vertical"
android:contentDescription="#string/list_image" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/list_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="#+id/list_item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="italic" />
</LinearLayout>
</LinearLayout>
Item.java:
public class Item {
String image;
String title;
String subtitle;
}
adapter.notifyDataSetChanged(); try this