我对我最近在android studio上的项目有疑问。我在互联网上看到了此代码,并出于自己的目的对其进行了重建。一切似乎都正常,但如下所示,我的列表视图有问题。我的应用程序看起来像这样 https://i.stack.imgur.com/TagXL.jpg https://i.stack.imgur.com/XA9Dc.jpg
并在这里添加一些变量 https://i.stack.imgur.com/wjYHV.jpg
这是我来自应用程序的数据库。如您所见,它有效
https://i.stack.imgur.com/0mTfe.png
我的问题是来自数据库的数据不会在列表视图中显示,因为它必须是 https://i.stack.imgur.com/QrTgY.png, 并且在某些设备中,我的弹出菜单没有以正确的方式显示,例如 https:/ /i.stack.imgur.com/FxusX.png 如果您可以帮助我解决这2个问题,我将不胜感激
码:
贝辛
package com.example.besindegerleri;
import static android.R.attr.name;
public class Besin{
int id;
String Besin;
Double Kalori;
Double Protein;
Double Yag;
Double Karbon;
public Besin() {
super();
}
public Besin(int i, String Besin, double Kalori, double Protein, double Yag, double Karbon) {
super();
this.id = i;
this.Besin = Besin;
this.Kalori=Kalori;
this.Protein=Protein;
this.Yag=Yag;
this.Karbon=Karbon;
}
// constructor
public Besin(String Besin, Double Kalori, Double Protein, Double Yag, Double Karbon){
this.Besin = Besin;
this.Kalori=Kalori;
this.Protein=Protein;
this.Yag=Yag;
this.Karbon=Karbon;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getBesin() { return Besin; }
public void setBesin(String Besin) { this.Besin = Besin; }
public Double getKalori() { return Kalori; }
public void setKalori(Double Kalori) { this.Kalori = Kalori; }
public Double getProtein() { return Protein; }
public void setProtein(Double Protein) { this.Protein = Protein;}
public Double getYag() { return Yag; }
public void setYag(Double Yag) { this.Yag = Yag;}
public Double getKarbon() { return Karbon;}
public void setKarbon(Double Karbon) { this.Karbon = Karbon; }
}
CustomBesinList.Java
package com.example.besindegerleri;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
public abstract class CustomBesinList extends BaseAdapter {
private Activity context;
ArrayList<Besin>besins;
private PopupWindow pwindo;
SQLiteDatabaseHandler db;
BaseAdapter ba;
public CustomBesinList(Activity context, ArrayList<Besin> besins,SQLiteDatabaseHandler db) {
this.context = context;
this.besins =besins;
this.db=db;
}
public static class ViewHolder
{
TextView textViewId;
TextView textViewBesin;
TextView textViewProtein;
TextView textViewKalori;
TextView textViewYag;
TextView textViewKarbon;
Button editButton;
Button deleteButton;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = context.getLayoutInflater();
ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
row = inflater.inflate(R.layout.row_item, null, true);
vh.textViewId = (TextView) row.findViewById(R.id.textViewId);
vh.textViewBesin = (TextView) row.findViewById(R.id.textViewBesin);
vh.textViewProtein = (TextView) row.findViewById(R.id.textViewProtein);
vh.textViewKalori = (TextView) row.findViewById(R.id.textViewKalori);
vh.textViewYag = (TextView) row.findViewById(R.id.textViewYag);
vh.textViewKarbon = (TextView) row.findViewById(R.id.textViewKarbon);
vh.editButton = (Button) row.findViewById(R.id.edit);
vh.deleteButton = (Button) row.findViewById(R.id.delete);
// store the holder with the view.
row.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.textViewBesin.setText(besins.get(position).getBesin());
vh.textViewId.setText("" + besins.get(position).getId());
vh.textViewProtein.setText("" + besins.get(position).getProtein());
vh.textViewKalori.setText("" + besins.get(position).getKalori());
vh.textViewYag.setText("" + besins.get(position).getYag());
vh.textViewKarbon.setText("" + besins.get(position).getKarbon());
final int positionPopup = position;
vh.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Save: ", "" + positionPopup);
editPopup(positionPopup);
}
});
vh.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Last Index", "" + positionPopup);
// Integer index = (Integer) view.getTag();
db.deleteBesin(besins.get(positionPopup));
// countries.remove(index.intValue());
besins = (ArrayList) db.getAllBesin();
Log.d("Besin size", "" + besins.size());
notifyDataSetChanged();
}
});
return row;
}
public long getItemId(int position) {
return position;
}
public Object getItem(int position) {
return position;
}
public int getBesi() {
return besins.size();
}
public void editPopup(final int positionPopup)
{
LayoutInflater inflater = context.getLayoutInflater();
View layout = inflater.inflate(R.layout.edit_popup,
(ViewGroup) context.findViewById(R.id.popup_element));
pwindo.setWidth(RelativeLayout.LayoutParams.WRAP_CONTENT);
pwindo.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
final EditText BesinEdit = (EditText) layout.findViewById(R.id.editTextBesin);
final EditText ProteinEdit = (EditText) layout.findViewById(R.id.editTextProtein);
final EditText KaloriEdit = (EditText) layout.findViewById(R.id.editTextKalori);
final EditText YagEdit = (EditText) layout.findViewById(R.id.editTextYag);
final EditText KarbonEdit = (EditText) layout.findViewById(R.id.editTextKarbon);
BesinEdit.setText(besins.get(positionPopup).getBesin());
ProteinEdit.setText("" + besins.get(positionPopup).getProtein());
KaloriEdit.setText("" + besins.get(positionPopup).getKalori());
YagEdit.setText("" + besins.get(positionPopup).getYag());
KarbonEdit.setText("" + besins.get(positionPopup).getKarbon());
Log.d("Name: ", "" + besins.get(positionPopup).getProtein());
Button save = (Button) layout.findViewById(R.id.save_popup);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String besinStr = BesinEdit.getText().toString();
String protein = ProteinEdit.getText().toString();
String kalori = KaloriEdit.getText().toString();
String yag = YagEdit.getText().toString();
String karbon = KarbonEdit.getText().toString();
Besin besin = besins.get(positionPopup);
besin.setBesin(besinStr);
besin.setProtein((double) Long.parseLong(protein));
besin.setKalori((double) Long.parseLong(kalori));
besin.setYag((double) Long.parseLong(yag));
besin.setKarbon((double) Long.parseLong(karbon));
db.updateBesin(besin);
besins = (ArrayList) db.getAllBesin();
notifyDataSetChanged();
for (Besin besin1 : besins) {
String log = "Id: " + besin1.getId() + " ,Besin: " + besin1.getBesin() + " ,Protein: " + besin1.getProtein()
+ " ,Kalori: " + besin1.getKalori()+ " ,Yag: " + besin1.getYag()+ " ,Karbon: " + besin1.getKarbon();
// Writing Countries to log
Log.d("Name: ", log);
}
pwindo.dismiss();
}
});
}
}
MainActivity.Java
package com.example.besindegerleri;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.example.besindegerleri.CustomBesinList;
import java.util.ArrayList;
import static java.lang.Long.parseLong;
public class MainActivity extends AppCompatActivity {
ArrayList<Besin> besins;
SQLiteDatabaseHandler db;
Button btnSubmit;
PopupWindow pwindo;
Activity activity;
ListView listView;
CustomBesinList customBesinList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity=this;
db= new SQLiteDatabaseHandler(this);
listView = (ListView) findViewById(R.id.list);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addPopUp();
}
});
Log.d("MainActivity: ", "Before reading mainactivity");
besins = (ArrayList) db.getAllBesin();
for (Besin besin : besins) {
String log = "Id: " + besin.getId() + " ,Besin: " + besin.getBesin() + " ,Protein: " + besin.getProtein()
+ " ,Kalori: " + besin.getKalori()+ " ,Yag: " + besin.getYag()+ " ,Karbon: " + besin.getKarbon();
// Writing Countries to log
Log.d("Name: ", log);
}
CustomBesinList customBesinList = new CustomBesinList(this, besins, db) {
@Override
public int getCount() {
return 0;
}
};
listView.setAdapter(customBesinList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(), "You Selected " + besins.get(position).getBesin() + " as Besin", Toast.LENGTH_SHORT).show();
}
});
}
public void addPopUp() {
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.edit_popup,
(ViewGroup) activity.findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 600, 670, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
final EditText besinEdit = (EditText) layout.findViewById(R.id.editTextBesin);
final EditText proteinEdit = (EditText) layout.findViewById(R.id.editTextProtein);
final EditText kaloriEdit = (EditText) layout.findViewById(R.id.editTextKalori);
final EditText yagEdit = (EditText) layout.findViewById(R.id.editTextYag);
final EditText karbonEdit = (EditText) layout.findViewById(R.id.editTextKarbon);
Button save = (Button) layout.findViewById(R.id.save_popup);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String besin = besinEdit.getText().toString();
String protein = proteinEdit.getText().toString();
String kalori = kaloriEdit.getText().toString();
String yag = yagEdit.getText().toString();
String karbon = karbonEdit.getText().toString();
Besin besinl = new Besin(besin,Double.parseDouble(protein), Double.parseDouble(kalori), Double.parseDouble(yag), Double.parseDouble(karbon));
db.addBesin(besinl);
if(customBesinList==null)
{
customBesinList = new CustomBesinList(activity, besins, db) {
@Override
public int getCount() {
return 0;
}
};
listView.setAdapter(customBesinList);
}
customBesinList.besins = (ArrayList) db.getAllBesin();
((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
for (Besin besin1 : besins) {
String log = "Id: " + besin1.getId() + " ,Besin: " + besin1.getBesin() + " ,Protein: " + besin1.getProtein()
+ " ,Kalori: " + besin1.getKalori()+ " ,Yag: " + besin1.getYag()+ " ,Karbon: " + besin1.getKarbon();
// Writing Countries to log
Log.d("Name: ", log);
}
pwindo.dismiss();
}
});
}
}
SQLiteDatabaseHandler.Java
package com.example.besindegerleri;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.example.besindegerleri.Besin;
import java.util.ArrayList;
import java.util.List;
public class SQLiteDatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "besinData";
// Country table name
private static final String TABLE_BESIN = "Besin";
// Country Table Columns names
private static final String KEY_ID = "id";
private static final String BESIN = "Besin";
private static final String PROTEIN = "Protein";
private static final String KALORI = "Kalori";
private static final String YAG = "Yag";
private static final String KARBON = "Karbon";
public SQLiteDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_COUNTRY_TABLE = "CREATE TABLE " + TABLE_BESIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + BESIN + " TEXT," +PROTEIN + " DOUBLE,"
+ KALORI + " DOUBLE," + YAG + " DOUBLE," + KARBON + " DOUBLE" +")";
db.execSQL(CREATE_COUNTRY_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BESIN);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new country
void addBesin(Besin besin) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BESIN, besin.getBesin());
values.put(PROTEIN, besin.getProtein());
values.put(KALORI, besin.getKalori());
values.put(YAG, besin.getYag());
values.put(KARBON, besin.getKarbon());
// Inserting Row
db.insert(TABLE_BESIN, null, values);
db.close(); // Closing database connection
}
// Getting single country
Besin getBesin(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_BESIN, new String[] { KEY_ID,
BESIN,KALORI, PROTEIN,YAG,KARBON }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Besin besin = new Besin(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getDouble(2),cursor.getDouble(3),cursor.getDouble(4),cursor.getDouble(5));
// return country
return besin;
}
// Getting All Countries
public List getAllBesin() {
List besinList = new ArrayList();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_BESIN;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Besin besin = new Besin();
besin.setId(Integer.parseInt(cursor.getString(0)));
besin.setBesin(cursor.getString(1));
besin.setProtein(cursor.getDouble(2));
besin.setKalori(cursor.getDouble(3));
besin.setYag(cursor.getDouble(4));
besin.setKarbon(cursor.getDouble(5));
// Adding country to list
besinList.add(besin);
} while (cursor.moveToNext());
}
// return country list
return besinList;
}
// Updating single country
public int updateBesin(Besin besin) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BESIN, besin.getBesin());
values.put(PROTEIN, besin.getProtein());
values.put(KALORI, besin.getKalori());
values.put(YAG, besin.getYag());
values.put(KARBON, besin.getKarbon());
// updating row
return db.update(TABLE_BESIN, values, KEY_ID + " = ?",
new String[] { String.valueOf(besin.getId()) });
}
// Deleting single country
public void deleteBesin(Besin besin) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_BESIN, KEY_ID + " = ?",
new String[] { String.valueOf(besin.getId()) });
db.close();
}
// Deleting all countries
public void deleteAllBesin() {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_BESIN,null,null);
db.close();
}
// Getting countries Count
public int getCountriesBesin() {
String countQuery = "SELECT * FROM " + TABLE_BESIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
activity_main.xml
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.besindegerleri.MainActivity"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Country" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnSubmit" />
</LinearLayout>
</LinearLayout>
edit_popup.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="wrap_content"
android:background="#FFE4C4"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<EditText
android:id="@+id/editTextBesin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Enter Besin" />
<EditText
android:id="@+id/editTextProtein"
android:hint="Enter Protein"
android:layout_width="wrap_content"
android:layout_below="@id/editTextBesin"
android:layout_marginTop="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editTextKalori"
android:hint="Enter Kalori"
android:layout_width="wrap_content"
android:layout_below="@id/editTextProtein"
android:layout_marginTop="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editTextYag"
android:hint="Enter Yag"
android:layout_width="wrap_content"
android:layout_below="@id/editTextKalori"
android:layout_marginTop="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editTextKarbon"
android:hint="Enter Karbon"
android:layout_width="wrap_content"
android:layout_below="@id/editTextYag"
android:layout_marginTop="10dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextKarbon"
android:layout_marginLeft="150dp"
android:layout_marginTop="-108dp"
android:text="Save" />
</RelativeLayout>
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginLeft="10dp"
android:textSize="30dp"
android:textColor="#1E90FF"
android:id="@+id/textViewId"
android:layout_row="0"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="#4B0082"
android:layout_below="@+id/textViewId"
android:id="@+id/textViewBesin"
android:layout_row="1"
android:layout_column="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="#4B0082"
android:layout_below="@+id/textViewBesin"
android:id="@+id/textViewProtein"
android:layout_row="1"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="#4B0082"
android:layout_below="@+id/textViewProtein"
android:id="@+id/textViewKalori"
android:layout_row="1"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="#4B0082"
android:layout_below="@+id/textViewKalori"
android:id="@+id/textViewYag"
android:layout_row="1"
android:layout_column="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="#4B0082"
android:layout_below="@+id/textViewYag"
android:id="@+id/textViewKarbon"
android:layout_row="1"
android:layout_column="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginRight="10dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="30dp"
android:id="@+id/edit"
android:text="Edit"
android:layout_toRightOf="@+id/textViewId"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:id="@+id/delete"
android:text="Delete"
android:layout_toRightOf="@+id/edit"
/>
</RelativeLayout>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。