gpt4 book ai didi

java - 从 CursorWindow 读取时出错

转载 作者:行者123 更新时间:2023-12-02 12:02:59 26 4
gpt4 key购买 nike

我试图用 recyclerview 显示我的 sqlite 数据,但它给了我这个错误:

无法从 CursorWindow 读取第 0 行第 9 列。在访问游标中的数据之前,请确保游标已正确初始化。

CursorWindow 有 6 行、9 列。

我检查了表格列,但找不到问题!请帮忙!!

我的代码:

public class LinesC {
private String BackgroundColor;
private int id;
private String City_ID;
private int FirstStationNumber;
private int Line_ID;
private int LastSTationNumber;
private int LineNumber;
private int Status;
private String TextColor;
private String Title;

public LinesC(String backgroundColor, int id, String city_ID, int firstStationNumber, int line_ID, int lastSTationNumber, int lineNumber, int status, String textColor, String title) {

BackgroundColor = backgroundColor;
this.id = id;
City_ID = city_ID;
FirstStationNumber = firstStationNumber;
Line_ID = line_ID;
LastSTationNumber = lastSTationNumber;
LineNumber = lineNumber;
Status = status;
TextColor = textColor;
Title = title;
}


public String getBackgroundColor() {
return BackgroundColor;
}

public int getId() {
return id;
}

public String getCity_ID() {
return City_ID;
}

public int getFirstStationNumber() {
return FirstStationNumber;
}

public int getLine_ID() {
return Line_ID;
}

public int getLastSTationNumber() {
return LastSTationNumber;
}

public int getLineNumber() {
return LineNumber;
}

public int getStatus() {
return Status;
}

public String getTextColor() {
return TextColor;
}

public String getTitle() {
return Title;
}
}

我的适配器:

public class LinesAdapter extends RecyclerView.Adapter <LinesAdapter.RecyclerViewHolder> {
SharedPreferences shared;
SharedPreferences.Editor editor;
ArrayList<LinesC> arrayList = new ArrayList<>();
Context context;

public LinesAdapter(ArrayList<LinesC> arrayList, Context context) {
this.arrayList = arrayList;
this.context = context;
}

public LinesAdapter(ArrayList<LinesC> arrayList) {
this.arrayList = arrayList;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_lines,parent,false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
context = parent.getContext();
return recyclerViewHolder;
}


@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
final LinesC linesC = arrayList.get(position);
holder.title.setText(String.valueOf(arrayList.get(position).getId()));


}

@Override
public int getItemCount() {
return arrayList.size();
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder
{
TextView title;
LinearLayout linearLayout;
RecyclerViewHolder(View view)
{
super(view);
linearLayout = (LinearLayout) itemView.findViewById(R.id.click_layout);
title = (TextView) itemView.findViewById(R.id.title);
}
}

}

我的DBHelper类的三个方法:

  // Create Table of Lines
public static final String CREATE_LINES_TABLE = "create table "+ LinesContract.LinesEntry.TABLE_NAME+
" ("+LinesContract.LinesEntry.BACKGROUND_COLOR+" text,"+LinesContract.LinesEntry.CITY_ID+" int,"+
LinesContract.LinesEntry.FIRSTSTATIONNUMBER+" int,"+LinesContract.LinesEntry.LINE_ID+" int,"+
LinesContract.LinesEntry.LASTSTATIONNUMBER+" int,"+LinesContract.LinesEntry.LINENUMBER+" int,"+
LinesContract.LinesEntry.STATUS+" int,"+ LinesContract.LinesEntry.TEXTCOLOR+" text," +
LinesContract.LinesEntry.TITLE+" text);";


public void putLineInformation(String backgroundColor, String city_ID, int firstStationNumber, int line_ID, int lastSTationNumber, int lineNumber, int status, String textColor, String title,SQLiteDatabase sqLiteDatabase)
{
ContentValues contentLineValues = new ContentValues();
contentLineValues.put(LinesContract.LinesEntry.BACKGROUND_COLOR,backgroundColor);
contentLineValues.put(LinesContract.LinesEntry.CITY_ID,city_ID);
contentLineValues.put(LinesContract.LinesEntry.FIRSTSTATIONNUMBER,firstStationNumber);
contentLineValues.put(LinesContract.LinesEntry.LINE_ID,line_ID);
contentLineValues.put(LinesContract.LinesEntry.LASTSTATIONNUMBER,lastSTationNumber);
contentLineValues.put(LinesContract.LinesEntry.LINENUMBER,lineNumber);
contentLineValues.put(LinesContract.LinesEntry.STATUS,status);
contentLineValues.put(LinesContract.LinesEntry.TEXTCOLOR,textColor);
contentLineValues.put(LinesContract.LinesEntry.TITLE,title);
long ll = sqLiteDatabase.insert(LinesContract.LinesEntry.TABLE_NAME,null,contentLineValues);
Log.d("Database Operations","****** One Row Inserted ******** "+contentLineValues);
}



public Cursor getLineInformation(SQLiteDatabase sqLiteDatabaseLine)
{
String [] projectionLine = {LinesContract.LinesEntry.BACKGROUND_COLOR, LinesContract.LinesEntry.CITY_ID, LinesContract.LinesEntry.FIRSTSTATIONNUMBER,
LinesContract.LinesEntry.LINE_ID, LinesContract.LinesEntry.LASTSTATIONNUMBER,
LinesContract.LinesEntry.LINENUMBER, LinesContract.LinesEntry.STATUS,
LinesContract.LinesEntry.TEXTCOLOR, LinesContract.LinesEntry.TITLE
};
Cursor lineCursor = sqLiteDatabaseLine.query(LinesContract.LinesEntry.TABLE_NAME,projectionLine,null,null,null,null,null);
return lineCursor;
}

我的异步任务:

 String json_url = "http://192.168.1.3/MetroRun/MRS.svc/GetLines/d5d23f68-e760-4561-b96c-d35cc3928d42/1/0";
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while((line=bufferedReader.readLine())!=null)
{
stringBuilder.append(line+"\n");
Thread.sleep(5000);
}
httpURLConnection.disconnect();
String json_data = stringBuilder.toString().trim();
JSONArray jsonarray = new JSONArray(json_data);
DBHelper dbHelper = new DBHelper(ctx);
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
int count = 0;
while(count<jsonarray.length())
{
JSONObject o = jsonarray.getJSONObject(count);
count++;
dbHelper.putLineInformation(o.getString("BackgroundColor"),o.getString("CityId"),
o.getInt("FirstStationNumber"),o.getInt("Id"),o.getInt("LastStationNumber")
,o.getInt("LineNumber"),o.getInt("Status"),o.getString("TextColor"),o.getString("Title"),sqLiteDatabase);
}
dbHelper.close();

我的 fragment :

public class Lines_fragment extends Fragment{
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
public Context context;
ArrayList<LinesC> arrayList = new ArrayList<>();
public Lines_fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_lines_fragment, container, false);

recyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setHasFixedSize(true);

adapter = new LinesAdapter(arrayList,getActivity());
DBHelper dbHelper = new DBHelper(getActivity());
SQLiteDatabase sqLiteDatabaseLine = dbHelper.getReadableDatabase();
Cursor cursor = dbHelper.getLineInformation(sqLiteDatabaseLine);
while(cursor.moveToNext())
{
LinesC linesC = new LinesC(cursor.getString(0), cursor.getInt(1), cursor.getString(2), cursor.getInt(3),cursor.getInt(4),cursor.getInt(5),cursor.getInt(6),cursor.getInt(7),cursor.getString(8),cursor.getString(9));
arrayList.add(linesC);
adapter = new LinesAdapter(arrayList,getActivity());
}
dbHelper.close();
adapter = new LinesAdapter(arrayList);
recyclerView.setAdapter(adapter);
return v;
}
}

最佳答案

getLineInformation() 正在检索 9 列:

String [] projectionLine = {LinesContract.LinesEntry.BACKGROUND_COLOR, LinesContract.LinesEntry.CITY_ID, LinesContract.LinesEntry.FIRSTSTATIONNUMBER,
LinesContract.LinesEntry.LINE_ID, LinesContract.LinesEntry.LASTSTATIONNUMBER,
LinesContract.LinesEntry.LINENUMBER, LinesContract.LinesEntry.STATUS,
LinesContract.LinesEntry.TEXTCOLOR, LinesContract.LinesEntry.TITLE
};

onCreateView() 正在尝试读取 10 列:

        LinesC linesC = new LinesC(cursor.getString(0), cursor.getInt(1), cursor.getString(2), cursor.getInt(3),cursor.getInt(4),cursor.getInt(5),cursor.getInt(6),cursor.getInt(7),cursor.getString(8),cursor.getString(9));

您的 LinesC 逻辑需要与您的 getLineInformation() 逻辑同步。

关于java - 从 CursorWindow 读取时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47113705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com