Problem/bug with a listview - android

I just want to store a data in a listview then displaying it with a textview, i identified the problem, it's in fact this ligne:
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
Operations made:
1-if i delete the first or the second "\n" the listview indicates that there is not any datas to display.
2- if i let the two "\n" then the value1 and value2 are not displayed !
The oncreate function of the class that show the datas:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.histo);
context = getApplicationContext();
activity = this;
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName);
updatelv(this);
ListView L = (ListView) findViewById(R.id.lv);
L.setTextFilterEnabled(true);
the code when tapping the calculate button in the main class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
Toast.makeText(carburant.this, "Bienvenue!", Toast.LENGTH_LONG).show();
data = "";
final EditText vol;
final EditText kil;
final EditText cons;
vol = (EditText) findViewById(R.id.volume2);
kil= (EditText) findViewById(R.id.kilometrage2);
cons= (EditText) findViewById(R.id.consom2);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value1 = (((EditText) findViewById(R.id.volume2))
.getText()).toString();
String value2 = (((EditText) findViewById(R.id.kilometrage2)).getText())
.toString();
if (value1 .equalsIgnoreCase("") && value2.equalsIgnoreCase(""))
{
Toast.makeText(carburant.this, "Veuillez vérifier les deux champs", Toast.LENGTH_SHORT).show();
}
else{
float q1=Float.parseFloat(vol.getText().toString());
float q2=Float.parseFloat(kil.getText().toString());
float x=((q1 / q2)* 100);
String y= Float.toString(x);
cons.setText(y);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String date = format.format(new Date());
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
if (data != "" ) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context, fileDir + fileName, data);
data = "";
The list_item XML:
<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" >
Histo.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:id="#+id/ViewHisto">
<ListView android:id="#+id/lv" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>
Main.XML:
<LinearLayout android:id="#+id/layoutPrincipal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/volume1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Volume(en Litres):">
</TextView>
<EditText android:id="#+id/volume2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="#string/vol"
>
</EditText>
<TextView android:id="#+id/kilometrage1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Kilometrage(en Kilomètres) :">
</TextView>
<EditText android:id="#+id/kilometrage2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="#string/kil">
</EditText>
<TextView android:id="#+id/consom1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Consommation moyenne(en Litres/100KM) :">
</TextView>
<EditText android:id="#+id/consom2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="#string/cons">
</EditText>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="Calculer"
android:id="#+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<Button
android:text="Reset"
android:id="#+id/button2"
android:layout_marginLeft="280px"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Did you consider creating a View to through an Adapter containing two TextViews to you display it correctly? It's the best approach.

Related

Why my textview is show in single line when I start the app firstly?

I have a very wonderful problem in my app. I have a text view in my activity which get a text from sqlite like this:
Hello
World!
But when I start my app for first time, My text view show the text like this:
Hello word!
When I go out from app and clear my app form RAM and then comeback to the app my text view show the text currectly like this:
Hello
World!
In fact, when onCreate method run for first time my text view text show in single line. I tried very ways like use recreate method:
public void recreate() {
this.recreate();
}
And in onCreate method:
recreate();
But my app crashed!
Is there a way to solve this problem? Thanks
My Ativity1:
String pattern = "'%" + inputSearch.getText().toString() + "%'";
final String sql = "SELECT * FROM " + TABLE + " WHERE " + MAANY + " LIKE " + pattern;
final SQLiteDatabase mydb = new MyDatabase2(EnglishActivity.this).getWritableDatabase();
final Cursor c = mydb.rawQuery(sql, null);
Cursor cT = mydb.rawQuery(" SELECT * FROM "+ TABLE + " WHERE "
+ TITLE + " LIKE ? " ,new String []{inputSearch.getText()+"%"});
Cursor c2=mydb.rawQuery(" SELECT * FROM "+ TABLE + " WHERE "
+ MAANY + " LIKE ? " ,new String []{inputSearch.getText()+"%"});
lv=(ListView)findViewById(R.id.enlist);
listItems=new ArrayList<String>();
lv2=(ListView)findViewById(R.id.enlist2);
listItems2=new ArrayList<String>();
array = new String[cT.getCount()];
array2 = new String[cT.getCount()];
array3 =new int[cT.getCount()];
int i = 0;
while(cT.moveToNext()){
String uname = cT.getString(cT.getColumnIndex(TITLE));
array[i] = uname;
String maany = cT.getString(cT.getColumnIndex(MAANY));
array2[i] = maany;
array3[i]=cT.getInt(cT.getColumnIndex(ID));
i++;
}
arrayMaany = new String[c.getCount()];
arrayMaany2 = new String[c.getCount()];
arrayMaany3 =new int[c.getCount()];
int ii = 0;
while(c.moveToNext()){
String uname = c.getString(c.getColumnIndex(TITLE));
arrayMaany[ii] = uname;
String maany = c.getString(c.getColumnIndex(MAANY));
arrayMaany2[ii] = maany;
arrayMaany3[ii]=c.getInt(c.getColumnIndex(ID));
ii++;
}
final ArrayList<String> forNum1 = new ArrayList<String>(Arrays.asList(array));
if(forNum1.size()==0){
lv2.setVisibility(View.VISIBLE);
} else if(forNum1.size()>0) {
lv2.setVisibility(View.GONE);
}
Typeface tf = Typeface.createFromAsset(getAssets(),"font/Harir Bold.otf");
lv.setAdapter(new EnCustomAdapter(EnglishActivity.this,array,array2,tf));
lv2.setAdapter(new
EnCustomAdapter(EnglishActivity.this,arrayMaany,arrayMaany2,tf));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
String i=array[position];
String ii=array2[position];
int iii=array3[position];
SharedPreferences en = getApplicationContext().getSharedPreferences("enn1", 1);
final SharedPreferences.Editor editorEn = en.edit();
String enn = en.getString("enn2" , i);
enn=i;
editorEn.putString("enn2", i);
editorEn.commit();
SharedPreferences fa = getApplicationContext().getSharedPreferences("faa1", 1);
final SharedPreferences.Editor editorFa = fa.edit();
String faa = fa.getString("faa2" , ii);
faa=ii;
editorFa.putString("faa2", ii);
editorFa.commit();
SharedPreferences idd = getApplicationContext().getSharedPreferences("id1", 1);
final SharedPreferences.Editor editorId = idd.edit();
int iddd = idd.getInt("id2" , iii);
iddd=iii;
editorId.putInt("id2", iii);
editorId.commit();
startActivity(new Intent(EnglishActivity.this,ArMaany.class));
overridePendingTransition(R.anim.animation_zoom_in_speed, R.anim.animation_zoom_out_speed);
}
});
lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
String i=arrayMaany[position];
String ii=arrayMaany2[position];
int iii=arrayMaany3[position];
SharedPreferences en = getApplicationContext().getSharedPreferences("enn1", 1);
final SharedPreferences.Editor editorEn = en.edit();
String enn = en.getString("enn2" , i);
enn=i;
editorEn.putString("enn2", i);
editorEn.commit();
SharedPreferences fa = getApplicationContext().getSharedPreferences("faa1", 1);
final SharedPreferences.Editor editorFa = fa.edit();
String faa = fa.getString("faa2" , ii);
faa=ii;
editorFa.putString("faa2", ii);
editorFa.commit();
SharedPreferences idd = getApplicationContext().getSharedPreferences("id1", 1);
final SharedPreferences.Editor editorId = idd.edit();
int iddd = idd.getInt("id2" , iii);
iddd=iii;
editorId.putInt("id2", iii);
editorId.commit();
startActivity(new Intent(EnglishActivity.this,ArMaany.class));
overridePendingTransition(R.anim.animation_zoom_in_speed, R.anim.animation_zoom_out_speed);
}
});
c.close();
c2.close();
mydb.close();
}});
My Activity 2:
final String enmaany = getSharedPreferences("enn1", 1).getString("enn2", "");
final String famaany = getSharedPreferences("faa1", 1).getString("faa2", "");
final int id = getSharedPreferences("id1", 1).getInt("id2", 1);
tt.setText(enmaany);
ttt.setText(famaany);
My activity 1 layout:
<?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"
android:padding="5dp"
android:background="#drawable/background_word">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/arabic_et">
<ImageView
android:layout_height="40dp"
android:layout_width="40dp"
android:gravity="center"
android:id="#+id/bSearchEn"
android:src="#drawable/search_edit"
android:background="#drawable/search_selector"/>
<EditText
android:inputType="text|textNoSuggestions"
android:layout_height="match_parent"
android:ems="10"
android:layout_width="match_parent"
android:id="#+id/inputSearchEn"
android:background="#FFFFFF"
android:textSize="20dp"
android:gravity="center_vertical|start"
android:hint="#string/searchEn"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/arabic_list"
android:layout_marginTop="5dp"
android:orientation="vertical">
<ListView
android:scrollbarSize="10dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fastScrollEnabled="false"
android:divider="#88D4D4D4"
android:dividerHeight="1dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/enlist"/>
<ListView
android:scrollbarSize="10dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fastScrollEnabled="false"
android:divider="#88D4D4D4"
android:dividerHeight="1dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/enlist2"/>
</LinearLayout>
</LinearLayout>
My activity 2 layout:
<?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="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:id="#+id/lf"
android:gravity="right">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".5"
android:gravity="left|center_vertical">
<ImageView
android:layout_width="40dp"
android:gravity="center"
android:id="#+id/fa_img"
android:layout_height="40dp"
android:padding="3dp"/>
<ImageView
android:layout_height="33dp"
android:layout_width="33dp"
android:background="#drawable/email_selector"
android:id="#+id/pronounce"/>
<ImageView
android:layout_height="40dp"
android:layout_width="40dp"
android:id="#+id/copy"
android:src="#drawable/copy_selector"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".5"
android:gravity="right">
<ImageView
android:layout_height="40dp"
android:layout_width="40dp"
android:src="#drawable/unlearn"
android:id="#+id/img_close"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#88D4D4D4"
android:layout_marginTop="5dp"/>
<ScrollView
android:scrollbars="none"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:id="#+id/tv_help"
android:gravity="center"
android:textColor="#color/c1"
android:textSize="25dp"
android:textStyle="bold"
android:typeface="monospace"/>
<com.mshrapps.bs.ArialMTBoldRegularTextView2
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:id="#+id/tv_fa"
android:gravity="right"
android:textColor="#color/c1"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Try add this to your TextView in the xml file
android:singleLine="false"

removeAllView() and removeAllViewsInLayout() is not removing

Thanks in advance and here is my problem. I have a database and i have to show its content whenever the "SHOW DATA" button is clicked (on the same activity (layout)). so when the user clicks again that button i want to remove the views in the layout so that the content of the database is not shown twice. I've have used removeAllView() and removeAllViewsInLayout() but it seems it's not working (for sure i'm not using correctly this method).
Here is the java code :
public void onClickShowData(){
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor cursor = myDB.getAllData();
if (cursor.getCount() == 0) {
//Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show();
showData("Error", "Nothing found");
return;
}
StringBuffer tmp = new StringBuffer();
LinearLayout ll = (LinearLayout)findViewById(R.id.cardViewContainer);
Context context = getApplicationContext();
LinearLayout ll2 = new LinearLayout(context);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(context);
CardView cv = new CardView(context);
cv.setId(R.id.cardView);
while (cursor.moveToNext()){
tmp.append("name : " + cursor.getString(0) + "\n" +
"calory : " + cursor.getString(1) + "\n" +
"protein : " + cursor.getString(2) + "\n" +
"lipid : " + cursor.getString(3) + "\n" +
"clucid: " + cursor.getString(4));
ll.addView(cv);
cv.addView(ll2);
tv.setText(tmp.toString());
ll2.addView(tv);
}
}
}
);
}
and this is the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_database"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="gafood.mobiledeviceproject.arianchitgar.foodmakerwithga.databaseActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scrollViewLL">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Name"
android:hint="Food name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/Name"
android:layout_centerHorizontal="true"
android:id="#+id/Calory"
android:hint="Calory"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/Calory"
android:layout_alignRight="#+id/Calory"
android:layout_alignEnd="#+id/Calory"
android:id="#+id/protein"
android:hint="Protein" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/protein"
android:layout_centerHorizontal="true"
android:id="#+id/lipid"
android:hint="Lipid" />
<Button
android:text="Add food"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lipid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="22dp"
android:id="#+id/addFood" />
<Button
android:text="Update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addFood"
android:layout_centerHorizontal="true"
android:id="#+id/updateFood" />
<Button
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/updateFood"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/delete" />
<Button
android:text="Food DB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/addFood"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/showAll" />
<Button
android:text="search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/updateFood"
android:layout_alignLeft="#+id/updateFood"
android:layout_alignStart="#+id/updateFood"
android:id="#+id/search" />
<Button
android:text="Advanced"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/delete"
android:layout_alignLeft="#+id/delete"
android:layout_alignStart="#+id/delete"
android:id="#+id/advancedSearch" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardViewContainer"></LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
enter code here
When the app starts
When i click 3 times on show DB
You have to remove
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
This line from inside onClickShowData() method and, add
LinearLayout ll;
To the top of your activity class.
then in onCreate(Bundle savedInstanceState) method add,
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
In your current code, you are adding views to an ll and calling ll.removeAllViews() in a different object.
the final code should be something like,
public class YourActivityName extends Activity {
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
// .... other code //
}
public void onClickShowData(){
ll.removeAllViews();
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor cursor = myDB.getAllData();
if (cursor.getCount() == 0) {
//Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show();
showData("Error", "Nothing found");
return;
}
StringBuffer tmp = new StringBuffer();
Context context = getApplicationContext();
LinearLayout ll2 = new LinearLayout(context);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(context);
CardView cv = new CardView(context);
cv.setId(R.id.cardView);
while (cursor.moveToNext()){
tmp.append("name : " + cursor.getString(0) + "\n" +
"calory : " + cursor.getString(1) + "\n" +
"protein : " + cursor.getString(2) + "\n" +
"lipid : " + cursor.getString(3) + "\n" +
"clucid: " + cursor.getString(4));
ll.addView(cv);
cv.addView(ll2);
tv.setText(tmp.toString());
ll2.addView(tv);
}
}
}
);
}
}
You are setting your onClick() listener with showAll.setOnClickListener but the code that gets executed does not include ll.removeAllViews(). Change your click listener to something like the following to remove views when the button is clicked.
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
...etc.

More than 3 items per sub items GridView Android

I want to display the data from content provider in gridview more than 3 sub items or line
here is my java source code
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
players.clear();
// First Check if cursor is not null
if(cursor != null ) {
StringBuilder result = new StringBuilder();
// Now loop to all items and append it to string builder
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("ID"));
String name = cursor.getString(cursor.getColumnIndex("ITEM_NAME"));
String qty = cursor.getString(cursor.getColumnIndex("QUANTITY"));
String inst = cursor.getString(cursor.getColumnIndex("INSTRUCTION"));
String pid = cursor.getString(cursor.getColumnIndex("POS_ID"));
players.add("Items :" + name + "\n" + "Qty :" + qty + "\n" +
"Details :" + inst + "\n" + " Pos ID :" + pid);
gv.setAdapter(adapter);
resultView.setText(result);
}
} else {
// If cursor is null then display toast amd set empty data.
resultView.setText("Empty data");
Toast.makeText(MainActivity.this, "May be there is no app corresponding to your provider or there is null data.",
Toast.LENGTH_LONG).show();
}
}
the gv.setAdapter(adapter) is where i display the data coming from the content provider
and here is my xml source code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#ffffff"
android:fillViewport="true"
tools:context="com.condorpos.kitchen_bumpbar.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<Button
android:id="#+id/btnRetrieve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:text="#string/show_data" />
<TextView
android:id="#+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="false"
android:padding="5dp"
android:textColor="#000000"
android:textSize="15sp" />
<GridView
android:layout_width="match_parent"
android:layout_height="420dp"
android:numColumns="5"
android:verticalSpacing="140dp"
android:id="#+id/gridView1"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/expListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible" />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="#string/empty_list"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"/>
</LinearLayout>
</LinearLayout>
the gridView1 is equals to gv on the java source code i declare it like
Griview gv;
gv=(GridView) findViewById(R.id.gridView1);
from the image below it shows that the data is not fully display looked per column i want to display more than 3 lines but it gives me this result
Any idea or help will do
Thanks a lot guys
You need make height of GridView is match_parent/wrap_content (based on your layout), don't fix size 420dp

android Set text is not working for me

I am trying get the weather details like temp and condition and I am unable to Display them But I am able to see them in my log cat code.
Here is my code follows
cond = (TextView) findViewById(R.id.condDescr);
temp = (TextView) findViewById(R.id.tempText);
temp.setText("HI How are you");
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{city});
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
#Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0]));
try {
weather = JSONWeatherParser.getWeather(data);
// Let's retrieve the icon
//weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
#Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
if (weather.iconData != null && weather.iconData.length > 0) {
Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
//imgView.setImageBitmap(img);
}
String str1 = (weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
//temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");**(I have tried this one too)**
String str2 = ("" + weather.currentCondition.getHumidity() + "%");
Log.w("myApp", str1);
Log.w("mytemp", str2);
cond.setText(str1);
temp.setText(str2);
//temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");
}
My XML File
<?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" >
<ImageButton
android:id="#+id/but_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:onClick="onButtonClick"
android:src="#drawable/ic_start_wifi_on" />
<ImageButton
android:id="#+id/but_start_gps"
android:src="#drawable/ic_start_gps_on" />
<View
android:id="#+id/view_mid"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_centerVertical="true" />
<Button
android:id="#+id/but_chooseTrack"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/but_go"
android:layout_width="190dp"
android:text="#string/bt_txt_go"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/dateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/but_start_gps"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tempText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/dateText"
android:layout_below="#+id/dateText"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/but_wifi"
android:src="#drawable/weather" />
</RelativeLayout>
And I have also tried like manual data set but it is not working for me. I dont know what was my mistake could any one tell me how to solve this.
In order to call the method doInBackground of your ASyncTask, you need to add task.execute(params).
params can be either a String or multiple Strings separated by ,
In your case, you should pass to execute the data needed by the method getWeatherData().

Android: Dynamically Populate a SlidingDrawer (or a small list) within the Main Activity

I am working on an Android mapping app that allows a user to type a search parameter into an EditText box, and then the closest destinations matching the query are plotted with pins on the MapView.
Clicking on a pin will give info about the location, but I would like to provide more information to the user up front. I thought a SlidingDrawer could populate and open up to show the user the names of the destinations/the distances away.
Having spent a lot of time on this, having done much reading, and having made little progress, I am now asking the community for assistance.
The problem I'm having is that I can't figure out how to populate the contents of the SlidingDrawer within the main activity. I am able to launch a new activity, but that takes my MapView off the screen. I need to be able to see the map as well as the SlidingDrawer. And I can't figure out how to populate the SlidingDrawer without it being set up as an Activity.
(And I should say also that it doesn't necessarily need to be a SlidingDrawer that contains the list of destinations. I could modify my layout to make a small (max 3 items) list and take away some of the map's real estate if that is easier, but I'm not sure how to do that, either.)
Here is the code that calls my SlidingDrawer:
//call SlidingDrawer
Intent drawerIntent = new Intent(this, SlidingDrawerFinal.class);
drawerIntent.putExtra("lat", lat);
drawerIntent.putExtra("lon", lon);
int numberOfTargetsForList = numberOfLoops;
drawerIntent.putExtra("numberOfTargetsForList", numberOfTargetsForList);
for(int i = 0; i < target.length; i++){
drawerIntent.putExtra("target" + Integer.toString(i), target[i]);
}
this.startActivity(drawerIntent);
This is the code in my SlidingDrawer class (extends Activity) that fills the list inside the drawer:
View inflatedDrawerLayout = getLayoutInflater().inflate(R.layout.drawer_main, null);
int width = getWindow().getAttributes().width;
int height = 300;
LayoutParams params = new LayoutParams(width, height);
getWindow().addContentView(inflatedDrawerLayout, params);
// add some data
ArrayList<MyData> myDataList = new ArrayList<MyData>();
myData = new MyData[numberOfTargets];
for(int i = 0; i < numberOfTargets; i++){
String lineTwo = "";
if(target[i].getRating().equals("Not Yet Rated")){
lineTwo = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
lineTwo = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String lineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + lineTwo;
myData[i] = new MyData(lineOne, i);
myDataList.add(myData[i]);
}
mListView = (ListView) findViewById(R.id.listview_);
mListView.setAdapter(new MyListAdapter(this, R.layout.drawer_row, myDataList));
drawer = (SlidingDrawer) findViewById(R.id.drawer);
handleButton = (Button) findViewById(R.id.handle);
drawer.animateOpen();
drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
handleButton.setBackgroundResource(R.drawable.handle_down_white);
}
});
My layout files:
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" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/goLabel" />
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="#+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="my_API_Key"/>
</LinearLayout>
<include layout="#layout/drawer_main"/>
</FrameLayout>
</LinearLayout>
and, finally, drawer_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer"
android:handle="#+id/handle"
android:content="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#null"
android:layout_gravity="bottom">
<LinearLayout
android:id="#id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold|italic"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:text="#string/top_search_results" >
</TextView>
<View
android:background="#android:drawable/divider_horizontal_bright"
android:layout_width="fill_parent"
android:layout_height="2dp" >
</View>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listview_"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:divider="#android:color/transparent"
android:dividerHeight="2dp" >
</ListView>
</LinearLayout>
<Button
android:id="#id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/handle_down_white">
</Button>
</SlidingDrawer>
</FrameLayout>
Sorry that this is so lengthy. Any assistance will be greatly appreciated.
You can use Popup Window.
View popupView = layoutInflater.inflate(R.layout.popup_window, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton btnCancel = (ImageButton) popupView
.findViewById(R.id.btnCancel);
popupWindow.showAtLocation(LayoutView, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL, (LayoutView.getWidth() - popupView
.getWidth()) / 2, LayoutView.getHeight()
- popupWindow.getHeight() - 10);
popupWindow.setAnimationStyle(Animation.RELATIVE_TO_SELF);
popupWindow.setFocusable(true);
popupWindow.update();
The solution I opted for is programmatically a bit less than ideal, but the layout works well. Since I knew I wouldn't have more than 3 results, I just changed my layout to include (up to) 3 small TextViews under my search bar and (sadly) got rid of the SlidingDrawer altogether. When the results come back, I populate as many TextViews as necessary, each with an onClickListener. It won't scale well, and everything is hard-coded; but it works. I may try to improve upon this solution as I learn more about Android.
new Layout:
<?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" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/goLabel" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dip"
android:textStyle="bold|italic"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/search_results_header" >
</TextView>
<View android:id="#+id/bar_over_one"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_one" >
</TextView>
<View android:id="#+id/bar_over_two"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_two" >
</TextView>
<View android:id="#+id/bar_over_three"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_three" >
</TextView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="#+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="0fmGmyFrFDUrPGrwXR_scZZxVx6CkSdK-sys-Qw"/>
</LinearLayout>
</LinearLayout>
new code:
private void populateList(int numberOfTargetsForList){
TextView header = (TextView) findViewById(R.id.search_results_header);
header.setText("Top Search Results");
int i = 0;
//first line in list
barOverOne.setVisibility(View.VISIBLE);
String ratingAndDistance = "";
if(target[i].getRating().equals("Not Yet Rated")){
ratingAndDistance = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
ratingAndDistance = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String detailsForLineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + ratingAndDistance;
TextView lineOne = (TextView) findViewById(R.id.line_one);
lineOne.setText(detailsForLineOne);
lineOne.setOnClickListener(this);
i++;
...(similar blocks handle cases of i = 1 and i =2)
Finally, in my onClick() method:
case R.id.line_one:{
String locForURL = "origin=" + Double.toString(lat/1000000.0) +
"," + Double.toString(lon/1000000.0) + "&destination=" +
Double.toString(target[0].getLat()/1000000.0) + "," +
Double.toString(target[0].getLon()/1000000.0);
Intent intent = new Intent(this, ParsingXMLDirectionsWithListView.class);
intent.putExtra("dirTitle", target[0].getName()+ " (" +
Double.toString((Math.round(target[0].getDistance()*100.0))/100.0) + " miles)");
intent.putExtra("locForURL", locForURL);
this.startActivity(intent);
break;
}//line_one
...(similar blocks for lines two and three)
Not perfect, but oh well. At least looks pretty good from the user's end, and I was never going to meet my deadline by continuing down the SlidingDrawer route. Maybe when I have some more free time I'll see if I can figure that out....

Categories

Resources