我有一个方法从csv文件中读取数据,然后创建一个包含这些数据的对象。然后,数据用于使用自定义适配器对自定义行视图进行充气,以填充列表视图。在将下一行添加到列表之前,应显示第一行,然后短暂延迟。
问题在于,当调用该方法时,屏幕在总延迟时间内变黑(如果延迟时间为1秒,5条消息= 5秒黑度),则ListView将显示所有行,而不是显示它一次只能填充一个。
playChat方法:
public void playChat(){
View row = null;
int delay = 1000;
try{
InputStream is = this.getAssets().open("chat.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while((line = reader.readLine())!= null){
String[] rowData = line.split(",");
ChatText current = new ChatText();
current.setUser(rowData[0]);
current.setChatText(rowData[1]);
chatTexts.add(current);
playView.invalidateViews();
try {
Thread.sleep(delay);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
我的自定义适配器:
public ChatAdapter(Context context, ArrayList<ChatText> chatTexts){
super(context, R.layout.chat_layout_left,chatTexts);
this.ctx = context;
}
public View getView(final int pos, View convertView, final ViewGroup parent) {
View row = convertView;
String user = getItem(pos).getUser();
if (null == row){
LayoutInflater inflater = ((Activity)ctx).getLayoutInflater();
holder = new DetailHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtUser = (TextView)row.findViewById(R.id.txtUser);
holder.txtChatText = (TextView)row.findViewById(R.id.txtChatText);
row.setTag(holder);
}
else{
holder = (DetailHolder)row.getTag();
}
holder.imgIcon.setImageResource(R.drawable.icon_one);
holder.txtUser.setText(getItem(pos).getUser());
holder.txtChatText.setText(getItem(pos).getChatText());
return row;
}
帮助将不胜感激
答案 0 :(得分:1)
问题是看起来你可能在主线程上这样做了,这意味着你可以使用sleep()来锁定整个UI,直到循环完成。
你会想要这样的东西(我的语法可能略有偏差,但应该很接近):
public synchronized void playChat(){
InputStream is = this.getAssets().open("chat.csv");
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
new Thread(new Runnable() {
View row = null;
int delay = 1000;
Handler handler = new Handler(Looper.getMainLooper());
@Override
public synchronized void run() {
try{
String line;
while((line = reader.readLine())!= null){
String[] rowData = line.split(",");
ChatText current = new ChatText();
current.setUser(rowData[0]);
current.setChatText(rowData[1]);
addChatText(current);
try {
Thread.sleep(delay);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
private synchronized void addChatText(final ChatText current){
handler.post(new Runnable() {
@Override
public void run() {
chatTexts.add(current);
playView.invalidateViews();
}
});
}
}).start();
}
另外,不要忘记在以后关闭所有内容:)
答案 1 :(得分:0)
问题是你永远不应该在主线程中加入“阻塞代码”。例如,代码$cursor = r\table('games')->changes()->run($conn);
foreach ($update in $cursor) {
printr($update);
})
将在这段时间内停止主线程。主线程是Android更新UI的地方。这就是为什么你的屏幕变黑了。
您可以在此处详细了解:Link
实现您想要做的事情的一种方法是使用AssyncTask类。以下是我认为您的代码可能是:
$productID = $_POST['productID'];
$sql_product="SELECT * FROM cc_menu_customizations WHERE cust_asoc='$productID'";
$stmt_product = $conn->prepare($sql_product);
$stmt_product->execute();
$result_product = $stmt_product->setFetchMode(PDO::FETCH_ASSOC);
if($result_product > 0) {
while($row = $stmt_product->fetch()) {
$menuArr[$row['cust_title']][] = '<li class="item-inmodal-extra-wrap-options"><p>'. $row['cust_desc'] .'<p></li>';
}
foreach($menuArr as $menuTitle => $productArr) {
echo '<div class="item-inmodal-extra"><h3>'. $menuTitle .'</h3><ul class="item-inmodal-extra-wrap">';
foreach($productArr as $key =>$productname) {
echo $productname;
}
echo '</ul></div>';
}
}