I'm new here so i will try to explain my problems as good as i can.
I am trying to inflate a ListView into a View in my main activity. My main activity has some buttons and texts on the top of the Activity and there is enough space left for the listView. The listview is consisted of categories, represented as an imageView and a textview.
The problem im facing is that when i inflate the category_list_activity, the activity i created for the category list, two things happen:
The ListView takes over all the screen, which means i cannot touch neither the buttons nor the edittext, and also the ListView is empty.
I have created the Adapters needed and i have searched for some info here in stackof but i couldn't find any right answer.
here is the parts of code i wrote: MainActivity.java
package com.example.aggro.activities;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.aggro.R;
import com.example.aggro.myclasses.StaticValues;
import com.example.test_guide_classes.TestValues;
public class MainActivity extends ActionBarActivity {
Activity a;
Button toogle_button;
Button go_button;
Button login_button;
EditText search_text;
View inflating_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
a = this;
ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Loading values");
pd.show();
TestValues tv = new TestValues(MainActivity.this);
pd.dismiss();
toogle_button = (Button) findViewById(R.id.toggle_button);
toogle_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (toogle_button.getText().equals("List")){
toogle_button.setText("Map");
}else{
toogle_button.setText("List");
}
Toast t = Toast.makeText(a, "Toogle button pressed", Toast.LENGTH_SHORT);
t.show();
}
});
search_text = (EditText) findViewById(R.id.search_text);
go_button = (Button) findViewById(R.id.search_go);
go_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast t = Toast.makeText(a, "Searching for " + search_text.getText(), Toast.LENGTH_LONG);
t.show();
}
});
login_button = (Button) findViewById(R.id.login_button);
login_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast t = Toast.makeText(a, "Login pressed",Toast.LENGTH_SHORT);
t.show();
}
});
inflating_view = findViewById(R.id.inflating_view);
ViewGroup parent =(ViewGroup) inflating_view.getParent();
int index = parent.indexOfChild(inflating_view);
parent.removeView(inflating_view);
inflating_view = getLayoutInflater().inflate(R.layout.activity_category_list, parent, false);
parent.addView(inflating_view, index);
inflating_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast t = Toast.makeText(getApplicationContext(), "ListView clicked", Toast.LENGTH_SHORT);
t.show();
}
});
inflating_view = n
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setCostAllowed(true);
String provider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(provider);
LocationListener ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Initialize the location fields
StaticValues.my_latitude = location.getLatitude();
StaticValues.my_longitude = location.getLongitude();
Toast.makeText(a.getApplicationContext(), ""+location.getLatitude()+location.getLongitude(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(a.getApplicationContext(), provider + "'s status changed to "+status +"!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(a.getApplicationContext(), "Provider " + provider + " enabled!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(a.getApplicationContext(), "Provider " + provider + " disabled!",
Toast.LENGTH_SHORT).show();
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CategoryAdapter.java
package com.example.aggro.myclasses;
import java.util.ArrayList;
import com.example.aggro.R;
import com.example.test_guide_classes.Question;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CategoryAdapter extends BaseAdapter{
private Context context;
private ArrayList<Category> categories;
public CategoryAdapter(Context context, ArrayList<Category> categories){
this.context = context;
this.categories = categories;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return categories.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return categories.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return categories.get(arg0).getId();
}
private class Viewholder{
TextView category_text;
ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder holder = null;
Category category = categories.get(position);
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = (View) inflater.inflate(R.layout.activity_category, null);
holder = new Viewholder();
holder.category_text = (TextView) convertView.findViewById(R.id.category_text);
holder.image = (ImageView) convertView.findViewById(R.id.category_image);
convertView.setTag(holder);
}else{
holder = (Viewholder) convertView.getTag();
}
holder.category_text.setText(category.getName());
holder.image.setImageURI(category.getImageUri());
return convertView;
}
}
CategoryListActivity.java
package com.example.aggro.activities;
import com.example.aggro.R;
import com.example.aggro.R.id;
import com.example.aggro.R.layout;
import com.example.aggro.R.menu;
import com.example.aggro.myclasses.CategoryAdapter;
import com.example.test_guide_classes.TestValues;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class CategoryListActivity extends ActionBarActivity {
ListView category_list_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_list);
Log.d("Category List View", "Category list view is called");
category_list_view = (ListView) findViewById(R.id.category_list_view);
CategoryAdapter ca = new CategoryAdapter(getApplicationContext(),
TestValues.categories);
category_list_view.setAdapter(ca);
category_list_view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Clicked Category" + parent.getItemIdAtPosition(position), Toast.LENGTH_LONG);
toast.show();
}
});
Log.d("Category List View", "Everything is loaded");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
style="@android:style/Theme.Black.NoTitleBar"
tools:context="com.example.aggro.activities.MainActivity" >
<Button
android:id="@+id/toggle_button"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="@string/toogle_list_en"
android:textSize="10dp"
/>
<TextView
android:id="@+id/search_text_view"
android:layout_toRightOf="@+id/toggle_button"
android:layout_alignParentTop="true"
android:text="@string/search_text_en"
android:layout_height="30dp"
android:paddingTop="7dp"
android:textSize="10dp"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/search_text"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:textColor="#FFFFFF"
android:textSize="15dp"
android:background="#000000"
android:layout_toLeftOf="@+id/search_go"
android:layout_toRightOf="@+id/search_text_view" />
<Button
android:id="@+id/search_go"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toLeftOf="@+id/login_button"
android:layout_alignParentTop="true"
android:text="@string/search_go_en"
android:textSize="10dp" />
<Button
android:id="@+id/login_button"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/login_en"
android:textSize="10dp" />
<View
android:id="@+id/inflating_view"
android:layout_below="@+id/toggle_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
activity_category_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.aggro.activities.CategoryListActivity" >
<ListView
android:id="@+id/category_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
activity_category.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aggro.activities.CategoryActivity" >
<ImageView
android:id="@+id/category_image"
android:layout_height="35dp"
android:layout_width="35dp"
android:layout_toLeftOf="@+id/category_text_en"
android:background="#FFFFFF"
/>
<TextView
android:id="@+id/category_text"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="20dp"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:paddingTop="3dp"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
I have checked the CategoryListActivity.java alone and it works as it was supposed to, so i think the adapter works right.
If you need any other information please let me know. Thanks in advance.
0 comments:
Post a Comment