【发布时间】:2013-10-04 21:58:22
【问题描述】:
我正在做一个安卓应用程序。应用程序将引用或表达式保存在文本字段中。用户有机会更改此文本。但我希望当引用出现在TextView 中时,它根本不会出现。我只希望出现引用的前 5 个字符。我尝试使用子字符串来执行此操作,但是当我打开 TextView 时,什么也没有出现。 TextView 为空。我能做什么?
谁能帮帮我,拜托。
提前致谢。
这是我使用子字符串的那一行:
ListAdapter adapter = new SimpleAdapter( Quote.this,quoteList, R.layout.quote_entry, new String[] { "quoteId", "textQuote".substring(0, 4)}, new int[] {R.id.quoteId, R.id.textQuote});
这是整个班级
package com.example.prova1;
/**This class is the page of quotes*/
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import com.example.prova1.Database;
import com.example.prova1.EditQuote;
import com.example.prova1.AddQuote;
import com.example.prova1.R;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.ListView;
public class Quote extends ListActivity {
Intent intent;
TextView quoteId;
Database quotedatabase = new Database(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quote_main);//define that the interface used is quote_main
//Store data from database in an array list
ArrayList<HashMap<String, String>> quoteList = quotedatabase.getAllItems();
//Check if there are quotes to display
if(quoteList.size()!=0) {
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
quoteId = (TextView) view.findViewById(R.id.quoteId);
String itemIdValue = quoteId.getText().toString();
Intent theIndent = new Intent(getApplication(),ShowQuote.class);
theIndent.putExtra("quoteId", itemIdValue);
finish();
startActivity(theIndent);
}
});
// Here we use ListAdapter as a bridge between ListView and the data of ListView
ListAdapter adapter = new SimpleAdapter(
Quote.this,quoteList,
R.layout.quote_entry,
new String[] {
"quoteId",
"textQuote".substring(0, 4)
},
new int[] {
R.id.quoteId,
R.id.textQuote}
);
setListAdapter(adapter);
}
}
}
【问题讨论】:
-
谁能帮我完成这项任务?
-
为什么要在字符串文字上加上子字符串?为什么不直接编码你想要的字符串:“text”?